Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.
Peter Griess edited this page Aug 31, 2010 · 2 revisions

Each of tokens shipped with node-strtok can be used in isolation to pack and unpack binary values directly to/from Buffer objects.

Each token type has a len property and supports get() and put() methods.

len

The size of this type in bytes.

get(<buf>, <off>)

Read a value of this type from the given buffer at the given offset. The buffer should be large enough to accommodate a read of len bytes from offset <off>. That is, get() should be invoked on a complete, contiguous region of memory that the type is packed in to.

put(<buf>, <off>, <val>, <flush>)

Write <val> to <buf> at offset <off>. If there is not enough space in the buffer (i.e. <buf>.<off> + len > <buf>.length), the <flush> callback is invoked. This callback is invoked as <flush>(<buf>, <off>) and is expected to do something with the Buffer (e.g. flush it to a net.Stream) such that its contents are completely free for use. After invoking <flush>, put() will serialize the value to offset 0.

The put() function returns the number of bytes consumed in the buffer. Note that this value can be negative if the flush callback was invoked. The intended usage of this function is as follows, assuming offset already has some value.

offset += <some type>.put(buf, offset, <some value>, <some flush>);
offset += <some type>.put(buf, offset, <some value>, <some flush>);
offset += <some type>.put(buf, offset, <some value>, <some flush>);

Note that if put() returns a negative value, offset will be moved backwards in the buffer so that the next put() invocation writing earlier in the buffer. This is OK since flush will have been called.

Clone this wiki locally