Buffer

Buffer class is a global type with various constructors and accessors.

IoT.js provides Buffer to manipulate binary data. Currently buffer has a pure ES5 compatible implementation, but this might be reworked to use UInt8Array in the future.

Example

  1. var Buffer = require('buffer');
  2. // Creates a zero-filled Buffer of length 10.
  3. var buf1 = new Buffer(10);
  4. // Creates a Buffer containing [0x1, 0x2, 0x3].
  5. var buf2 = new Buffer([1, 2, 3]);
  6. // Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].
  7. var buf3 = new Buffer('tést');

Class: Buffer

new Buffer(size)

  • size {integer} Size of the new buffer.

Creates a new buffer of size bytes and initialize its data to zero.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer(5);

new Buffer(buffer)

  • buffer {Buffer} Source buffer.

Creates a copy of an existing buffer. The buffer data is not shared between the two buffers.

Example

  1. var Buffer = require('buffer');
  2. var buffer1 = new Buffer(5);
  3. var buffer2 = new Buffer(buffer1);

new Buffer(str[, encoding])

  • str {string} Source string.
  • encoding {string} Encoding format.

Creates a new buffer which contains the CESU-8 representation of the str string argument. If encoding optional argument is present its value must be hex. When this encoding is specified the str argument must be a sequence of hexadecimal digit pairs, and these pairs are converted to bytes.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer(String.fromCharCode(65));
  3. // prints: 1
  4. console.log(buffer);
  5. var buffer = new Buffer(String.fromCharCode(128));
  6. // prints: 2
  7. console.log(buffer);
  8. var buffer = new Buffer(String.fromCharCode(2048));
  9. // prints: 3
  10. console.log(buffer);
  11. var buffer = new Buffer('4142', 'hex');
  12. // prints: AB
  13. console.log(buffer);

new Buffer(array)

  • array {Array} Array of numbers.

Creates a new Buffer from an array of numbers. The numbers are converted to integers first and their modulo 256 remainder is used for constructing the buffer.

Example

  1. var buffer = new Buffer([65, 256 + 65, 65 - 256, 65.1]);
  2. // prints: AAAA
  3. console.log(buffer);

Buffer.byteLength(str, encoding)

  • str {string} Source string.
  • encoding {string} String encoding.
  • Returns: {integer} Byte length of source string.

Returns the byte length of a buffer representing the value of the string argument encoded with encoding. The effect is the same as:

  1. return new Buffer(str, encoding).length;

Example

  1. var Buffer = require('buffer');
  2. // prints: 1
  3. console.log(Buffer.byteLength(String.fromCharCode(65)));
  4. // prints: 2
  5. console.log(Buffer.byteLength(String.fromCharCode(128)));
  6. // prints: 3
  7. console.log(Buffer.byteLength(String.fromCharCode(2048)));
  8. // prints: 2
  9. console.log(Buffer.byteLength('4142', 'hex'));

Buffer.concat(list)

  • list {Array} An array of Buffer objects.
  • Returns: {Buffer} Concatenated buffer.

Returns the concatenation of the Buffer objects provided in the list array.

Example

  1. var Buffer = require('buffer');
  2. var buffer = Buffer.concat([ new Buffer('He'),
  3. new Buffer('llo'),
  4. new Buffer(' wo'),
  5. new Buffer('rld') ])
  6. // prints: Hello world
  7. console.log(buffer);

Buffer.from(array)

  • array {Array} Array of numbers.
  • Returns: {Buffer} containing the elements from array

Creates a new Buffer from an array of numbers. The numbers are converted to integers first and their modulo 256 remainder is used for constructing the buffer.

Example

  1. var Buffer = require('buffer');
  2. var source = new Buffer[65, 66, 67];
  3. var buffer = Buffer.from(source);
  4. //prints: ABC
  5. console.log(buffer.toString());

Buffer.from(string[,encoding])

  • str {String} Source string.
  • encoding {String} Encoding format.
  • Returns: {Buffer} containing the elements from str

Creates a new buffer which contains the CESU-8 representation of the str string argument. If encoding optional argument is present its value must be hex. When this encoding is specified the str argument must be a sequence of hexadecimal digit pairs, and these pairs are converted to bytes.

Example

  1. var Buffer = require('buffer');
  2. var buffer = Buffer.from('4142','hex');
  3. //prints: AB
  4. console.log(buffer.toString());

Buffer.from(buffer)

  • buffer {Buffer} Source buffer.
  • Returns: {Buffer} which is the copy of buffer Creates a copy of an existing buffer. The buffer data is not shared between the two buffers.

Example

  1. var Buffer = require('buffer');
  2. var source = new Buffer(12);
  3. var buffer = Buffer.from(source);

Buffer.from(arrayBuffer[, byteOffset[, length]])

  • arrayBuffer {ArrayBuffer} Arraybuffer, or a buffer of a TypedArray
  • byteOffset {Number} Index of first byte to expose. Default: 0.
  • length {Number} Number of bytes to expose. Default: arrayBuffer.length - byteOffset.
  • Returns: {Buffer} containing the data of arraybuffer from read offset with length

Example

  1. var source = new ArrayBuffer(12);
  2. var buffer = Buffer.from(source, 0, 2);
  3. //prints: 2
  4. console.log(buffer.length);
  1. var typed_source = new Uint8Array([65,66]);
  2. var arr_buff = Buffer.from(typed_source1.buffer, 0, 2);
  3. //prints: AB
  4. console.log(buff.toString('utf-8'));

Buffer.isBuffer(obj)

  • obj {Object}
  • Returns: {boolean}

Returns true if obj is an instance of Buffer. Returns false otherwise.

Example

  1. var Buffer = require('buffer');
  2. // prints: true
  3. console.log(Buffer.isBuffer(new Buffer(1)));
  4. // prints: false
  5. console.log(Buffer.isBuffer('str'));

buf.length

  • {integer}

Returns the capacity of the buffer in bytes. Note: when the buffer is converted to another type (e.g. String) the length of the converted value might be different from this value.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer([0xc8, 0x80]);
  3. // prints: 2
  4. console.log(buffer.length);
  5. var str = buffer.toString();
  6. // prints: 1
  7. console.log(str.length);

buf.compare(otherBuffer)

  • otherBuffer {Buffer} The right-hand side of the comparison.
  • Returns: {integer}

This function performs a lexicographic comparison between two buffers.

It returns with 0 if the two buffers are the same. Otherwise it returns with -1 if the first different byte is lower for buf, and 1 if the byte is higher. If the length of the two buffers are different, the comparison is performed until the lower length is reached. If all bytes are the same the function returns with -1 if buf.length is less than otherBuffer.length and 1 otherwise.

Example

  1. var Buffer = require('buffer');
  2. var buffer1 = new Buffer('AB');
  3. var buffer2 = new Buffer('A');
  4. var buffer3 = new Buffer('B');
  5. // prints: 0
  6. console.log(buffer1.compare(buffer1));
  7. // prints: 1
  8. console.log(buffer1.compare(buffer2));
  9. // prints: -1
  10. console.log(buffer1.compare(buffer3));

buf.copy(targetBuffer[, targetStart[, sourceStart[, sourceEnd]]])

  • targetBuffer {Buffer} The buffer to be modified.
  • targetStart {Integer} Default: 0
  • sourceStart {integer} Default: 0
  • sourceEnd {integer} Default: buf.length
  • Returns: {integer} The number of bytes copied.

Copy a sequence of bytes from buf buffer to targetBuffer buffer. The source byte range is specified by sourceStart and sourceEnd and the destination byte offset is specified by targetStart. Only the targetBuffer is modified.

Example

  1. var Buffer = require('buffer');
  2. var buffer1 = new Buffer('Hello XY world!');
  3. var buffer2 = new Buffer('<JS>');
  4. buffer2.copy(buffer1, 6, 1, 3);
  5. // prints: Hello JS world!
  6. console.log(buffer1);

buf.equals(otherBuffer)

  • otherBuffer {Buffer} The right-hand side of the comparison.
  • Returns: {boolean}

Returns true if this and otherBuffer have exactly the same bytes. Returns false otherwise. The effect is the same as:

  1. return buf.compare(otherBuffer) == 0;

Example

  1. var Buffer = require('buffer');
  2. var buffer1 = new Buffer('AB');
  3. var buffer2 = new Buffer('4142', 'hex');
  4. var buffer3 = new Buffer('A');
  5. // prints: true
  6. console.log(buffer1.equals(buffer2));
  7. // prints: false
  8. console.log(buffer1.equals(buffer3));

buf.fill(value)

  • value {integer} All bytes are set to this value.
  • Returns: {Buffer} The original buffer.

Set all bytes of the buffer to value. The value is converted to integer first and its modulo 256 remainder is used for updating the buffer. Returns with buf.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('Hello');
  3. buffer.fill(65);
  4. // prints: AAAAA
  5. console.log(buffer);
  6. buffer.fill(66 - 256);
  7. // prints: BBBBB
  8. console.log(buffer);

buf.slice([start[, end]])

  • start {integer} Default: 0
  • end {integer} Default: buf.length
  • Returns: {Buffer} A newly created buffer.

This function returns with a newly created buffer which contains the bytes of the buf buffer between start and end.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('This is JavaScript!!!');
  3. // prints: JavaScript
  4. console.log(buffer.slice(8, 18));

buf.toString([start[, end]])

  • start {integer} Default: 0
  • end {integer} Default: buffer.length
  • Returns: {string}

Returns a string created from the bytes stored in the buffer. By passing start and end the conversion can be limited to a subset of the buf buffer. If a single hex string is passed to the function, the whole buffer is converted to hexadecimal data.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('DEFG');
  3. // prints: EF
  4. console.log(buffer.toString(1, 3));
  5. // prints: 44454647
  6. console.log(buffer.toString('hex'));

buf.write(string[, offset[, length]])

  • string {string} Data to be written into buffer.
  • offset {integer} Start position of writing. Default: 0
  • length {integer} How many bytes to write. Default: buffer.length - offset.
  • Returns: {integer} Total number of bytes written.

Writes string into the buf buffer. The start position of the writing can be specified by offset and the maximum number of updated bytes can be limited by length. Returns total number of bytes written to the buffer.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('......');
  3. buffer.write('AB');
  4. buffer.write('XY', 3);
  5. // prints: AB.XY.
  6. console.log(buffer);
  7. var buffer = new Buffer('......');
  8. buffer.write('ABCDEF', 1, 3);
  9. // prints: .ABC..
  10. console.log(buffer);

buf.writeUInt8(value, offset[, noAssert])

  • value {integer} Number to be written into the buffer.
  • offset {integer} Start position of the writing.
  • noAssert {boolean} Skip argument validation. Default: false
  • Returns: {number} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position. The value must be a valid 8-bit unsigned integer.

If noAssert is set and the value is outside of the expected range or the offset is higher than the size of the buffer the operation is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('....');
  3. // prints: 3
  4. console.log(buffer.writeUInt8(65, 2));
  5. // prints: ..A.
  6. console.log(buffer);

buf.writeUInt16LE(value, offset[, noAssert])

  • value {integer} Number to be written into the buffer.
  • offset {integer} Start position of the writing.
  • noAssert {boolean} Skip argument validation. Default: false
  • Returns: {integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 16-bit unsigned integer.

If noAssert is set and the value is outside of the expected range or the offset is higher than the size of the buffer the operation is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('......');
  3. // prints: 3
  4. console.log(buffer.writeUInt16LE(0x4142, 1));
  5. // prints .BA...
  6. console.log(buffer);

buf.writeUInt32LE(value, offset[, noAssert])

  • value {integer} Number to be written into the buffer.
  • offset {integer} Start position of the writing.
  • noAssert {boolean} Skip argument validation. Default: false
  • Returns: {integer} Offset plus the number of bytes written.

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 32-bit unsigned integer.

If noAssert is set and the value is outside of the expected range or the offset is higher than the size of the buffer the operation is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('......');
  3. // prints: 5
  4. console.log(buffer.writeUInt32LE(0x41424344, 1));
  5. // prints: .DCBA.
  6. console.log(buffer);

buf.readInt8(offset[, noAssert])

  • offset {number} Start position of buffer for reading.
  • noAssert {boolean} Skip offset validation. Default: false
  • Returns: {number}

Reads a signed 8-bit integer from buf buffer starting from offset position.

If noAssert is set and the offset is higher than the size of the buffer the result is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('ABCDEF');
  3. // prints: 42
  4. console.log(buffer.readUInt8(1).toString(16));

buf.readUInt8(offset[, noAssert])

  • offset {integer} Start position of the reading.
  • noAssert {boolean} Skip argument validation. Default: false
  • Returns: {number}

Reads an unsigned 8-bit integer from buf buffer starting from offset position.

If noAssert is set and the offset is higher than the size of the buffer the result is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('ABCDEF');
  3. // prints: 42
  4. console.log(buffer.readUInt8(1).toString(16));

buf.readUInt16LE(offset[, noAssert])

  • offset {number} Start position of buffer for reading.
  • noAssert {boolean} Skip offset validation. Default: false
  • Returns: {number}

Reads an unsigned 16-bit integer from buf buffer starting from offset position with little endian format.

If noAssert is set and the offset is higher than the size of the buffer the result is undefined.

Example

  1. var Buffer = require('buffer');
  2. var buffer = new Buffer('ABCDEF');
  3. // prints: 4342
  4. console.log(buffer.readUInt16LE(1).toString(16));