{"action":"created","issue":{"url":"https://api.github.com/repos/w3c/FileAPI/issues/83","repository_url":"https://api.github.com/repos/w3c/FileAPI","labels_url":"https://api.github.com/repos/w3c/FileAPI/issues/83/labels{/name}","comments_url":"https://api.github.com/repos/w3c/FileAPI/issues/83/comments","events_url":"https://api.github.com/repos/w3c/FileAPI/issues/83/events","html_url":"https://github.com/w3c/FileAPI/issues/83","id":237017796,"number":83,"title":"Add method to read file into user given Uint8Array","user":{"login":"petkaantonov","id":829734,"avatar_url":"https://avatars1.githubusercontent.com/u/829734?v=3","gravatar_id":"","url":"https://api.github.com/users/petkaantonov","html_url":"https://github.com/petkaantonov","followers_url":"https://api.github.com/users/petkaantonov/followers","following_url":"https://api.github.com/users/petkaantonov/following{/other_user}","gists_url":"https://api.github.com/users/petkaantonov/gists{/gist_id}","starred_url":"https://api.github.com/users/petkaantonov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petkaantonov/subscriptions","organizations_url":"https://api.github.com/users/petkaantonov/orgs","repos_url":"https://api.github.com/users/petkaantonov/repos","events_url":"https://api.github.com/users/petkaantonov/events{/privacy}","received_events_url":"https://api.github.com/users/petkaantonov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-06-19T20:41:50Z","updated_at":"2017-06-19T21:25:28Z","closed_at":null,"body":"Reading file contents into user supplied array avoids unnecessary copies. For example you can just get a pointer to an area in a WASM memory and directly read the file instead of first reading it into an useless ArrayBuffer and then doing a slow copy:\r\n\r\n```js\r\n// file is a File or Blob\r\n// wasm is a WebAssembly module instance wrapper with some additional methods like malloc added\r\nconst reader = new FileReader();\r\nconst ptr = wasm.malloc(file.size);\r\nconst array = new Uint8Array(wasm.memory.buffer, ptr, file.size);\r\ntry {\r\n    let bytesRead = await reader.readIntoUint8Array(file, array);\r\n    // Done\r\n} catch (e) {\r\n   // Error\r\n}\r\n```\r\n\r\nIt also allows reading files while using minimal memory and allocations:\r\n\r\n```js\r\nconst reader = new FileReader();\r\nconst bufferPtr = wasm.malloc(8192);\r\nconst buffer = new Uint8Array(wasm.memory.buffer, bufferPtr, 8192);\r\nlet totalBytesRead = 0;\r\n\r\nwhile (totalBytesRead < file.size) {\r\n    const start = totalBytesRead;\r\n    const end = Math.min(start + 8192, file.size);\r\n    const bytesRead = await readIntoUint8Array(file.slice(start, end), buffer);\r\n    wasm.process(bufferPtr, bytesRead);\r\n    totalBytesRead += bytesRead;\r\n}\r\n```\r\n\r\nDoing the same with current APIs would allocate a ton of small ArrayBuffers..."},"comment":{"url":"https://api.github.com/repos/w3c/FileAPI/issues/comments/309578263","html_url":"https://github.com/w3c/FileAPI/issues/83#issuecomment-309578263","issue_url":"https://api.github.com/repos/w3c/FileAPI/issues/83","id":309578263,"user":{"login":"domenic","id":617481,"avatar_url":"https://avatars3.githubusercontent.com/u/617481?v=3","gravatar_id":"","url":"https://api.github.com/users/domenic","html_url":"https://github.com/domenic","followers_url":"https://api.github.com/users/domenic/followers","following_url":"https://api.github.com/users/domenic/following{/other_user}","gists_url":"https://api.github.com/users/domenic/gists{/gist_id}","starred_url":"https://api.github.com/users/domenic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/domenic/subscriptions","organizations_url":"https://api.github.com/users/domenic/orgs","repos_url":"https://api.github.com/users/domenic/repos","events_url":"https://api.github.com/users/domenic/events{/privacy}","received_events_url":"https://api.github.com/users/domenic/received_events","type":"User","site_admin":false},"created_at":"2017-06-19T21:25:28Z","updated_at":"2017-06-19T21:25:28Z","body":"See also #40 "}}
{"actor":{"display_login":"domenic"}}