nodejs(三)Buffer module & Byte Order

nodejs(三)Buffer module & Byte Order

一。目录

➤ Understanding why you need buffers in Node

➤ Creating a buffer from a string

➤ Converting a buffer to a string

➤ Manipulating the bytes in a buffer

➤ Slicing and copying a buffer

二。Understanding why you need buffers in Node

 JavaScript is good at handling strings, but because it was initially designed to manipulate HTML documents, it is not very good at

handling binary data. JavaScript doesn’t have a byte type — it just has numbers — or structured types, or even byte arrays: It just

has strings.  

  Because Node is based on JavaScript, Node can handle text protocols like HTTP, but you can also use it to talk to databases,

manipulate images, and handle fi le uploads. As you can imagine, doing this using only strings is very difficult. In the early days,

Node handled binary data by encoding each byte inside a text character, but that proved to be wasteful, slow,unreliable, and hard

to manipulate.

  To make these types of binary-handling tasks easier, Node includes a binary buffer implementation, which is exposed as a JavaScript

API under the Buffer pseudo-class. A buffer length is specified in bytes, and you can randomly set and get bytes from a buffer.

  Another thing that is special about this buffer class is that the memory where the data sits is allocated outside of the JavaScript VM

memory heap. This means that these objects will not be moved around by the garbage-collection  algorithm: It will sit in this permanent

memory address without changing, which saves CPU cycles that would be wasted making memory copies of the buffer contents.

三。GETTING AND SETTING BYTES IN A BUFFER

  After you create or receive a buffer, you might want to inspect and change its contents. You can access the byte value on any

position of a buffer by using the [] operator like this:

var buf = new Buffer('my buffer content');
// accessing the 10th position of buf
console.log(buf[10]); // -> 99

  //You can also manipulate the content of any position:
  buf[20] = 125; // set the 21th byte to 125

 

四。注意 

NOTE When you create an initialized buffer, keep in mind that it will contain random bytes, not zeros.

var buf = new Buffer(1024);
console.log(buf[100]); // -> 5 (some random number)

  

NOTE In certain cases, some buffer operations will not yield an error. For instance:

➤ If you set any position of the buffer to a number greater than 255, that position will be assigned the 256 modulo value.

➤ If you assign the number 256 to a buffer position, you will actually be assigning the value 0.

➤ If you assign a fractional number like 100.7 to a buffer position, the buffer position will store the integer part — 100 in this case.

➤ If you try to assign a position that is out of bounds, the assignment will fail and the buffer will remain unaltered.

五。字节序

  又称端序,尾序英语:ByteOrder, or Endianness)。现代的计算机系统一般采用字节( 8 bit Byte)作为逻辑寻址单位。当物理单位的长度大于

1个字节时,就要区分字节顺序。典型的情况是整数在内存中的存放方式和网络传输的传输顺序。目前在各种体系的计算机中通常采用的字节存储机制主要

有两种:big-edian和little-endian。

nodejs(三)Buffer module & Byte Ordernodejs(三)Buffer module & Byte Order

  

六。字节序示例

1 var b = new Buffer('123456789');
2 console.log(b.length);           //9
3 console.log(b);          //<Buffer 31 32 33 34 35 36 37 38 39>
4 
5 var b2 = new Buffer(4);
6 b2.writeInt32LE(123456789,0);
7 console.log(b2.length);      //4
8 console.log(b2);          //<Buffer b5 e5 39 07>