Nodejs buffers: Difference between revisions

From wikinotes
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Buffers are reserved sections of memory designed to store binary data.
Buffers are reserved sections of memory designed to store binary data.<br>
Interact with it like an array of bytes.


= Documentation =
= Documentation =
Line 15: Line 16:
<blockquote>
<blockquote>
Buffers are fixed size, reserved sections of memory.<br>
Buffers are fixed size, reserved sections of memory.<br>
You can <code>alloc()</code> the entire section of memory (filling it with 0s)<br>
* <code>alloc()</code> reserves memory, initializing every byte with a 0
Or you can <code>allocUnsafe</code> which simply reserves the memory (faster).
* <code>allocUnsafe()</code> only reserves memory (faster).


By default, buffers are are stored as a UTF-8 array of bytes.<br>
By default, buffers are are stored as a [[unicode|UTF-8]] array of bytes.<br>
<code>buffer.toString()</code> converts the buffer to a UTF-8 string.
<code>buffer.toString()</code> converts the buffer to a [[unicode|UTF-8]] string.


<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
import { Buffer } from 'buffer';
import { Buffer } from 'buffer';


// allocate buffer
const buf = Buffer.alloc(10);
const buf = Buffer.alloc(10);
const buf = Buffer.allocUnsafe(10);
const buf = Buffer.allocUnsafe(10);
// write
buf.write("abc");
// read
buf[0];                      // byte at pos 0
buf.toString()                // print buf as UTF-8
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Basics -->
</blockquote><!-- Basics -->
= write =
<blockquote>
<syntaxhighlight lang="javascript">
buf.fromString("abc");  // from UTF-8 string
buf.write("abc");      // from UTF-8 string
</syntaxhighlight>
</blockquote><!-- write -->
= read =
<blockquote>
<syntaxhighlight lang="javascript">
// read buffer
buf[0];                      // byte at pos 0
buf.toString()                // print buf as UTF-8
for (cont ch in buf) { ... }  // loop over bytes in buffer
</syntaxhighlight>
</blockquote><!-- read -->
= manipulate =
<blockquote>
<syntaxhighlight lang="javascript">
buf.subarray(0, 2);          // slice of bytes 0-2
buf.set(Buffer.from('foo')); // copy
</syntaxhighlight>
</blockquote><!-- manipulate -->

Latest revision as of 02:24, 6 August 2021

Buffers are reserved sections of memory designed to store binary data.
Interact with it like an array of bytes.

Documentation

buffer docs https://nodejs.org/api/buffer.html
buffer tutorial https://nodejs.dev/learn/nodejs-buffers

Basics

Buffers are fixed size, reserved sections of memory.

  • alloc() reserves memory, initializing every byte with a 0
  • allocUnsafe() only reserves memory (faster).

By default, buffers are are stored as a UTF-8 array of bytes.
buffer.toString() converts the buffer to a UTF-8 string.

import { Buffer } from 'buffer';

// allocate buffer
const buf = Buffer.alloc(10);
const buf = Buffer.allocUnsafe(10);

// write
buf.write("abc");

// read
buf[0];                       // byte at pos 0
buf.toString()                // print buf as UTF-8

write

buf.fromString("abc");  // from UTF-8 string
buf.write("abc");       // from UTF-8 string

read

// read buffer
buf[0];                       // byte at pos 0
buf.toString()                // print buf as UTF-8
for (cont ch in buf) { ... }  // loop over bytes in buffer

manipulate

buf.subarray(0, 2);          // slice of bytes 0-2
buf.set(Buffer.from('foo')); // copy