• Node.js Video Tutorials

Node.js - Buffer Module



A Buffer is a space in computer memory that is temporarily used to store binary data. The Node.js Buffer module takes care of dealing with streams of binary data. The binary data is the data that the computer understands. It is labeled with a combination of 0's and 1's as per the binary numeral system. For example: 110011, 001110 etc.

In Node.js the buffer module is available globally. You don't have to use require to import it.

Class properties

Following are the list of properties −

Sr.No Module & Description
1

buf.buffer

It will give you an object of array buffer and the buffer created from it will be the same as the array buffer.

2

buf.byteOffset

It will give you an byteOffset value of the given buffer.

3

buf.length

It will give you the no of bytes in the buffer.

4

Buffer.poolSize

It allocates the size in bytes towards Buffer instances which are later used by NodeJs for pooling.

5

buf[index]

It helps to set or return the octet value at the index position in a buffer.

Static Methods

Following are the list of methods −

Sr.No Module & Description
1

alloc()

Used to create a new buffer with the given size.

2

allocUnsafe()

Used to allocate a new buffer with size given. If size larger than buffer.constants.MAX_LENGTH or lesser than 0, ERR_INVALID_ARG_VALUE will be thrown.

3

allocUnsafeSlow()

It will allocate a new buffer with given size. If size is larger than buffer.constants.MAX_LENGTH or lesser than 0, ERR_INVALID_ARG_VALUE will be thrown.

4

byteLength()

Used to return the length in bytes for the buffer object.

5

compare()

Used to compare two given buffer objects and returns a value that states the difference in comparison.

6

concat()

Used to return a single buffer object by concatenating all the given buffer objects

7

from()

Used to create a new buffer using the array passed as bytes.

8

isBuffer()

It returns the true if the object given is a buffer.

9

isEncoding()

It returns true if the given encoding value is a supported character encoding and false if not.

Buffer Class Methods

Following are the list of methods −

Sr.No Module & Description
1

readBigInt64LE()

Used to read a signed 64-bit integer at a given offset from the buffer in little endian form.

2

compare()

Used to deal with the comparison of two buffer objects and returns a value that states the difference in comparison.

3

readBigInt64BE()

Used to read a signed big endian 64-bit integer at a given offset from a buffer object.

4

lastIndexOf()

It takes care of searching a value inside the buffer. It returns the position at which the value is present and -1 if the value is not present.

5

readBigUInt64LE()

Used to read an unsigned little-endian 64-bit integer at a given offset from the current buffer object.

6

readDoubleBE()

Used to read a big-endian double 64-bit number at a given offset from the current buffer object.

7

readDoubleLE()

Used to read a little-endian double 64-bit number at a given offset from the current buffer object.

8

readFloatBE()

Used to read a big-endian float 32-bit number at a given offset from the current buffer object.

9

readFloatLE()

Used to read a little-endian float 32-bit number at a given offset from the current buffer object.

10

readInt8()

Used to read a 8-bit signed number at a given offset from the current buffer object.

11

writeDoubleBE()

Used to write a big-endian double number at a given offset to the buffer created.

12

writeDoubleLE()

Used to write a little-endian double number at a given offset to the buffer created.

13

writeFloatBE()

Used to write a big-endian float 32-bit number at a given offset to the current buffer object.

14

writeFloatLE()

Used to write a little-endian float 32-bit number at a given offset to the current buffer object.

15

copy()

Used to copy the contents of the source buffer object into the target buffer.

16

entries()

Used to create an iterator object from the buffer given and returns back a pair with [index, byte] from the contents of the buffer.

17

equals()

Used to compare two buffers and returns true if it's equal and false if not.

18

fill()

Used to fill the buffer with the given value . If the range inside the buffer is not specified it will fill the entire buffer.

19

includes()

Used to check if the given value is available inside or included in the buffer. It returns true if present and false if not.

20

indexOf()

It takes care of searching a value inside the buffer. It returns the position at which the value starts and -1 if the value is not present.

21

keys()

Used to return an iterator object. When looped through that object , it will give the key for each byte in the buffer object.

22

readBigUInt64BE()

Used to read an unsigned big-endian 64-bit integer at a given offset from the current buffer object.

23

readInt16BE()

Used to read a signed 16-bit integer at a given offset from the buffer in big endian form.

24

readInt16LE()

Used to read a signed 16-bit integer at a given offset from the buffer in little endian form.

25

readInt32BE()

Used to read a signed 32-bit integer at a given offset from the buffer in big endian form.

26

readInt32LE()

Used to read a signed 32-bit integer at a given offset from the buffer in little endian form.

27

readIntBE()

Used to read a number of bytes in big endian format from a buffer at a given offset.

28

readIntLE()

Used to read a number of bytes in little endian format from a buffer at a given offset.

29

readUInt8()

Used to read a 8-bit unsigned number at a given offset from the current buffer object.

30

readUInt16BE()

Used to read an unsigned 16-bit integer at a given offset from the buffer in big endian form.

31

readUInt16LE()

Used to read an unsigned 16-bit integer at a given offset from the buffer in little endian form.

32

readUInt32BE()

Used to read an unsigned 32-bit integer at a given offset from the buffer in big endian form.

33

readUInt32LE()

Used to read an unsigned 32-bit integer at a given offset from the buffer in little endian form.

34

readUIntBE()

Used to read a number of bytes as per the byte length and offset and return the unsigned integer in big endian format from a buffer used.

35

readUIntLE()

Used to read a number of bytes as per the byte length and offset and return the unsigned integer in little endian format from a buffer used.

36

subarray()

Used to return a new buffer based on given start and end offset values on the original buffer.

37

swap16(), swap32(), swap64()

These considers the buffer as an array of unsigned 16 or 32 or 64 bit integers and swaps the byte order in-place and returns a reference to the buffer.

38

toJSON()

Used to return a JSON object for the given buffer.

39

toString()

Used to decode the string as per the encoding given.

40

values()

Used to return an iterator object that has details of all the bytes present in the buffer.

41

write()

Used to write the given string value to the buffer from the position given by the offset value.

42

writeBigInt64BE()

Used to write a signed 64-bit integer at a given offset to the buffer in big endian form.

43

writeBigInt64LE()

Used to write a signed 64-bit integer at a given offset to the buffer in little endian form.

44

writeBigUInt64LE()

Used to write an unsigned 64-bit integer at a given offset to the buffer in little endian form.

45

writeInt8()

Used to write an signed 8-bit integer of value at a given offset to the buffer.

46

writeInt16BE()

Used to write a signed 16-bit integer at a given offset to the buffer in big endian form.

47

writeInt16LE()

Used to write a signed 16-bit integer at a given offset to the buffer in little endian form.

48

writeInt32BE()

Used to write a signed 32-bit integer at a given offset to the buffer in big endian form.

49

writeInt32LE()

Used to write a signed 32-bit integer at a given offset to the buffer in little endian form.

50

writeIntBE()

Used to write a byte length bytes of value at a given offset to the buffer in big endian form.

51

writeIntLE()

Used to write a bytelength bytes of value at a given offset to the buffer in little endian form.

52

writeUInt8()

Used to write an unsigned 8-bit integer of value at a given offset to the buffer.

53

writeUInt16BE()

Used to write a bytelength of an unsigned 16-bit integer of value at a given offset to the buffer in big endian form.

54

writeUInt16LE()

Used to write an unsigned 16-bit integer of value at a given offset to the buffer in little endian form.

55

writeUInt32BE()

Used to write an unsigned 32-bit integer of value at a given offset to the buffer in big endian form.

56

writeUInt32LE()

Used to write an unsigned 32-bit integer of value at a given offset to the buffer in little endian form.

57

writeUIntBE()

Used to write a bytelength of unsigned bytes of value at a given offset to the buffer in big endian form.

58

writeUIntLE()

Used to write a bytelength of unsigned bytes of value at a given offset to the buffer in little endian form.

nodejs_buffer_module.htm
Advertisements