node.js模組之Buffer模組

來源:互聯網
上載者:User

標籤:style   http   color   io   使用   java   ar   strong   for   

http://nodejs.org/api/buffer.html

Pure JavaScript is Unicode friendly but not nice to binary data. When dealing with TCP streams or the file system, it‘s necessary to handle octet streams. Node has several strategies for manipulating, creating, and consuming octet streams.

Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

The Buffer class is a global, making it very rare that one would need to ever require(‘buffer‘).

Converting between Buffers and JavaScript string objects requires an explicit encoding method. Here are the different string encodings.

 

‘asicc‘,‘binary‘等。

Creating a typed array from a Buffer works with the following caveats:caveat:缺陷

  1. The buffer‘s memory is copied, not shared.

  2. The buffer‘s memory is interpreted as an array, not a byte array. That is, new Uint32Array(new Buffer([1,2,3,4])) creates a 4-element Uint32Array with elements [1,2,3,4], not an Uint32Arraywith a single element [0x1020304] or [0x4030201].

NOTE: Node.js v0.8 simply retained a reference to the buffer in array.buffer instead of cloning it.

While more efficient, it introduces subtle incompatibilities with the typed arrays specification.ArrayBuffer#slice() makes a copy of the slice while Buffer#slice() creates a view.

new Buffer(str, [encoding])#
  • str String - string to encode.
  • encoding String - encoding to use, Optional.

Allocates a new buffer containing the given str. encoding defaults to ‘utf8‘.

new Buffer(size)#
  • size Number

Allocates a new buffer of size octets.

new Buffer(array)#
  • array Array

Allocates a new buffer using an array of octets.

> new Buffer([255,0,149]);
<Buffer ff 00 95>
>

如例4-24 所示的例子,我們用字串來建立Buffer,它預設是UTF-8 編碼的。如
果你沒有指定編碼格式,它就會認為是UTF-8 字串。這並不意味著Buffer 會
把字串補全成能夠存下任意Unicode 字元的大小(盲目地為每個字元分配4 個字
節),而是說明它不會截斷字元內容。在這個例子中,我們看到當輸入的字串是小
寫字母時,無論採用的是哪種編碼方式,Buffer 都使用同樣的位元組結構,因為每
個字母都落在同樣的區間裡。但是,當我們輸入“é”字元時,無論是預設的UTF-8
還是我們顯式指定為UTF-8,它都被編碼成2 個位元組大小。但是當我們指定編碼為
ASCII 時,字元被截斷成單個位元組。

你還可以往已經存在的Buffer 上寫入字串。Buffer.write() 會把字串寫到
Buffer 指定的位置上。如果從Buffer 指定位置開始有足夠空間的話,整個字串
都會被寫入。否則,字串的尾部會被截斷,好讓其大小能放入Buffer。在這兩
種情況下,Buffer.write() 都會返回一個數字,表示有多少位元組被成功寫入。對
於UTF-8 字串來說,如果一個完整字元無法寫入到Buffer 的話,就不會單獨寫
入該字元的某個位元組。如在例4-25 中,因為Buffer 太小了,以至於無法寫入一個
非ASCII 字元,所以它就是空的。

 

你還可以往已經存在的Buffer 上寫入字串。Buffer.write() 會把字串寫到Buffer 指定的位置上。如果從Buffer 指定位置開始有足夠空間的話,整個字串都會被寫入。否則,字串的尾部會被截斷,好讓其大小能放入Buffer。在這兩種情況下,Buffer.write() 都會返回一個數字,表示有多少位元組被成功寫入。對於UTF-8 字串來說,如果一個完整字元無法寫入到Buffer 的話,就不會單獨寫入該字元的某個位元組。如在例4-25 中,因為Buffer 太小了,以至於無法寫入一個非ASCII 字元,所以它就是空的。

 

node.js模組之Buffer模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.