ShadowNode

Use Node.js in your end devices(QQ: 796448809)

View the Project on GitHub yodaos-project/ShadowNode

Platform Support

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

var Buffer = require('buffer');

// Creates a zero-filled Buffer of length 10.
var buf1 = new Buffer(10);

// Creates a Buffer containing [0x1, 0x2, 0x3].
var buf2 = new Buffer([1, 2, 3]);

// Creates a Buffer containing UTF-8 bytes [0x74, 0xc3, 0xa9, 0x73, 0x74].
var buf3 = new Buffer('tést');

Class: Buffer

new Buffer(size)

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

Example

var Buffer = require('buffer');

var buffer = new Buffer(5);

new Buffer(buffer)

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

Example

var Buffer = require('buffer');

var buffer1 = new Buffer(5);
var buffer2 = new Buffer(buffer1);

new Buffer(str[, encoding])

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

var Buffer = require('buffer');

var buffer = new Buffer(String.fromCharCode(65));

// prints: 1
console.log(buffer);

var buffer = new Buffer(String.fromCharCode(128));

// prints: 2
console.log(buffer);

var buffer = new Buffer(String.fromCharCode(2048));

// prints: 3
console.log(buffer);

var buffer = new Buffer('4142', 'hex');

// prints: AB
console.log(buffer);

new Buffer(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

var buffer = new Buffer([65, 256 + 65, 65 - 256, 65.1]);

// prints: AAAA
console.log(buffer);

Buffer.byteLength(str, encoding)

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

return new Buffer(str, encoding).length;

Example

var Buffer = require('buffer');

// prints: 1
console.log(Buffer.byteLength(String.fromCharCode(65)));

// prints: 2
console.log(Buffer.byteLength(String.fromCharCode(128)));

// prints: 3
console.log(Buffer.byteLength(String.fromCharCode(2048)));

// prints: 2
console.log(Buffer.byteLength('4142', 'hex'));

Buffer.concat(list)

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

Example

var Buffer = require('buffer');

var buffer = Buffer.concat([ new Buffer('He'),
                             new Buffer('llo'),
                             new Buffer(' wo'),
                             new Buffer('rld') ])

// prints: Hello world
console.log(buffer);

Buffer.isBuffer(obj)

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

Example

var Buffer = require('buffer');

// prints: true
console.log(Buffer.isBuffer(new Buffer(1)));

// prints: false
console.log(Buffer.isBuffer('str'));

buf.length

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

var Buffer = require('buffer');

var buffer = new Buffer([0xc8, 0x80]);

// prints: 2
console.log(buffer.length);

var str = buffer.toString();

// prints: 1
console.log(str.length);

buf.compare(otherBuffer)

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

var Buffer = require('buffer');

var buffer1 = new Buffer('AB');
var buffer2 = new Buffer('A');
var buffer3 = new Buffer('B');

// prints: 0
console.log(buffer1.compare(buffer1));

// prints: 1
console.log(buffer1.compare(buffer2));

// prints: -1
console.log(buffer1.compare(buffer3));

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

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

var Buffer = require('buffer');

var buffer1 = new Buffer('Hello XY world!');
var buffer2 = new Buffer('<JS>');

buffer2.copy(buffer1, 6, 1, 3);

// prints: Hello JS world!
console.log(buffer1);

buf.equals(otherBuffer)

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

return buf.compare(otherBuffer) == 0;

Example

var Buffer = require('buffer');

var buffer1 = new Buffer('AB');
var buffer2 = new Buffer('4142', 'hex');
var buffer3 = new Buffer('A');

// prints: true
console.log(buffer1.equals(buffer2));

// prints: false
console.log(buffer1.equals(buffer3));

buf.fill(value)

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

var Buffer = require('buffer');

var buffer = new Buffer('Hello');

buffer.fill(65);

// prints: AAAAA
console.log(buffer);

buffer.fill(66 - 256);

// prints: BBBBB
console.log(buffer);

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

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

Example

var Buffer = require('buffer');

var buffer = new Buffer('This is JavaScript!!!');

// prints: JavaScript
console.log(buffer.slice(8, 18));

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

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

var Buffer = require('buffer');

var buffer = new Buffer('DEFG');

// prints: EF
console.log(buffer.toString(1, 3));

// prints: 44454647
console.log(buffer.toString('hex'));

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

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

var Buffer = require('buffer');

var buffer = new Buffer('......');
buffer.write('AB');
buffer.write('XY', 3);

// prints: AB.XY.
console.log(buffer);

var buffer = new Buffer('......');
buffer.write('ABCDEF', 1, 3);

// prints: .ABC..
console.log(buffer);

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

Writes value to buf at the specified offset with specified endian format (writeInt32BE() writes big endian, writeInt32LE() writes little endian). value should be a valid signed 32-bit integer. Behavior is undefined when value is anything other than a signed 32-bit integer.

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

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

var Buffer = require('buffer');

var buffer = new Buffer('....');

// prints: 3
console.log(buffer.writeUInt8(65, 2));

// prints: ..A.
console.log(buffer);

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

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

var Buffer = require('buffer');

var buffer = new Buffer('......');

// prints: 3
console.log(buffer.writeUInt16LE(0x4142, 1));

// prints .BA...
console.log(buffer);

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

Writes value into the buffer starting from offset position with little endian format. The value must be a valid 16-bit signed 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

var Buffer = require('buffer');

var buffer = new Buffer(4);

buffer.writeInt16LE(0x0102, 0);
buffer.writeInt16LE(0x0304, 2);

console.log(buffer);
// Prints: <Buffer 02 01 04 03>

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

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

var Buffer = require('buffer');

var buffer = new Buffer('......');

// prints: 5
console.log(buffer.writeUInt32LE(0x41424344, 1));

// prints: .DCBA.
console.log(buffer);

buf.readInt8(offset[, noAssert])

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

var Buffer = require('buffer');

var buffer = new Buffer('ABCDEF');

// prints: 42
console.log(buffer.readUInt8(1).toString(16));

buf.readUInt8(offset[, noAssert])

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

var Buffer = require('buffer');

var buffer = new Buffer('ABCDEF');

// prints: 42
console.log(buffer.readUInt8(1).toString(16));

buf.readUInt16LE(offset[, noAssert])

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

var Buffer = require('buffer');

var buffer = new Buffer('ABCDEF');

// prints: 4342
console.log(buffer.readUInt16LE(1).toString(16));

buf.readFloatLE(offset[, noAssert])

Reads a float number 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

var Buffer = require('buffer');

var buffer = new Buffer([1, 2, 3, 4]);
console.log(buffer.readFloatLE(1));

buf.readDoubleLE(offset[, noAssert])

Reads a double number 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

var Buffer = require('buffer');

var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]);
console.log(buffer.readDoubleLE(1);