Buffer - 401-advanced-javascript-v/seattle-javascript-401d29 GitHub Wiki
create a buffer
// Create an empty buffer of size 10.// A buffer that only can accommodate 10 bytes.const buf1 = Buffer.alloc(10);// Create a buffer with contentconst buf2 = Buffer.from("hello buffer");
interacting with buffer
// Examine the structure of a bufferbuf1.toJSON()// { type: 'Buffer', data: [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }// an empty bufferbuf2.toJSON()// { type: 'Buffer',data: [104, 101, 108, 108, 111, 32, 98, 117, 102, 102, 101, 114]}// the toJSON() method presents the data as the Unicode Code Points of the characters// Examine the size of a bufferbuf1.length // 10buf2.length // 12. Auto-assigned based on the initial content when created.// Write to a bufferbuf1.write("Buffer really rocks!")
// Decode a buffer
buf1.toString() // 'Buffer rea'
//oops, because buf1 is created to contain only 10 bytes, it couldn't accommodate the rest of the characters
// Compare two buffers