IoT.js provides Buffer to compensate the lack of pure Javascript's capability of manipulating binary data, Javascript string is only suitable for Unicode string. Buffer allows you to handle sequence of binary data in Javascript world.
Buffer class is a global type. you can create a buffer object in several way.
size: Number
Allocate a new buffer of size
bytes.
buffer: Buffer
Allocate a new buffer copied from given buffer
.
str: String
Allocate a new buffer containing given str
.
string: String
encoding: String
- Return:
Number
Returns actual byte length of string
encoded by encoding
. UTF-8 encoding is by default.
Note that it may be different from String.prototype.length
since that returns the number of characters in a string.
list: Array of Buffer
- Return:
Buffer
Returns a Buffer
concatenating all the Buffers in the list
.
obj
: Object
Test if obj
is a Buffer
.
Once you get buffer object in some way, you can handle the buffer using the following API:
Number
The capacity of buffer in bytes.
Note that the value of length
is not necessarily the same to the size of contents.
otherBuffer: Buffer
- Return:
Bool
Returns whether this
and otherBuffer
have the same bytes.
otherBuffer: Buffer
- Return:
Number
Returns a number indicating which buffer comes first between this
and otherBuffer
when lexicographically sorted.
string: String
- data to be written to bufferoffset: Number
, Default:0
- start position of buffer for writing.length: Number
, Default:buffer.length - offset
- total length to be written.- Return:
Number
- total number of bytes written.
Writes string
to buffer starting from offset
. Returns total number of bytes written to the buffer.
start: Number
, Default:0
end: Number
, Default:buffer.length
- Return:
String
Returns a string from buffer.
targetBuffer: Buffer
targetStart: Number
, Default:0
sourceStart: Number
, Default:0
sourceEnd: Number
, Default:buf.length
start: Number
, Default: `0end: Number
, Default:buf.length
- Return:
Buffer
Returns new buffer containing the same bytes of original buffer cropped by the given indices.
Copies data from buf[sourceStart..sourceEnd-1]
to targetBuffer[targetStart..]
.