Skip to content

Instantly share code, notes, and snippets.

@matthewp
Last active August 5, 2021 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewp/fa948a4a025f228e7145c670d9603216 to your computer and use it in GitHub Desktop.
Save matthewp/fa948a4a025f228e7145c670d9603216 to your computer and use it in GitHub Desktop.
Convert a file to its byte source equivalent
const fileName = Deno.args[0];
if(!fileName) {
console.error('No file provided.');
Deno.exit(1);
}
let file = await Deno.open(fileName);
let encoder = new TextEncoder();
await Deno.stdout.write(encoder.encode('new Uint8Array(['));
let first = true;
let buf = new Uint8Array(100);
let numOfByteRead = await Deno.read(file?.rid, buf);
while(numOfByteRead) {
let text = '';
for(let i = 0; i < numOfByteRead; i++) {
if(!first) {
text += ', ';
} else {
first = false;
}
text += buf[i];
}
await Deno.stdout.write(encoder.encode(text));
buf = new Uint8Array(100);
numOfByteRead = await Deno.read(file?.rid, buf);
}
await Deno.stdout.write(encoder.encode(']);'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment