阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js new Buffer(废弃)

      Node.js new Buffer(废弃)

      Buffer 类是一个全局变量,用于直接处理二进制数据。

      new Buffer(array) (废弃)

      • array < integer[]> An array of bytes to copy from.

      Allocates a new Buffer using an array of octets.

      // Creates a new Buffer containing the UTF-8 bytes of the string 'buffer'
      const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
      

      new Buffer(arrayBuffer[, byteOffset[, length]]) (废弃)

      • arrayBuffer|An ArrayBuffer, SharedArrayBuffer or the .buffer property of a TypedArray.
      • byteOffset < integer> Index of first byte to expose. Default: 0.
      • length < integer> Number of bytes to expose. Default: arrayBuffer.length - byteOffset.

      This creates a view of the ArrayBuffer or SharedArrayBuffer without copying the underlying memory. For example, when passed a reference to the .buffer property of a TypedArray instance, the newly created Buffer will share the same allocated memory as the TypedArray.

      The optional byteOffset and length arguments specify a memory range within the arrayBuffer that will be shared by the Buffer.

      const arr = new Uint16Array(2);
      
      arr[0] = 5000;
      arr[1] = 4000;
      
      // Shares memory with `arr`
      const buf = new Buffer(arr.buffer);
      
      console.log(buf);
      // Prints: <Buffer 88 13 a0 0f>
      
      // Changing the original Uint16Array changes the Buffer also
      arr[1] = 6000;
      
      console.log(buf);
      // Prints: <Buffer 88 13 70 17>
      

      new Buffer(buffer) (废弃)

      • buffer < Buffer> |An existing Buffer or Uint8Array from which to copy data.

      Copies the passed buffer data onto a new Buffer instance.

      const buf1 = new Buffer('buffer');
      const buf2 = new Buffer(buf1);
      
      buf1[0] = 0x61;
      
      console.log(buf1.toString());
      // Prints: auffer
      console.log(buf2.toString());
      // Prints: buffer
      

      new Buffer(size) (废弃)

      • size < integer> The desired length of the new Buffer.

      Allocates a new Buffer of size bytes. If size is larger than buffer.constants.MAX_LENGTH or smaller than 0, ERR_INVALID_OPT_VALUE is thrown. A zero-length Buffer is created if size is 0.

      Prior to Node.js 8.0.0, the underlying memory for Buffer instances created in this way is not initialized. The contents of a newly created Buffer are unknown and may contain sensitive data. Use Buffer.alloc(size) instead to initialize a Buffer with zeroes.

      const buf = new Buffer(10);
      
      console.log(buf);
      // Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
      

      new Buffer(string[, encoding]) (废弃)

      • string < string> String to encode.
      • encoding < string> The encoding of string. Default: ‘utf8’.

      Creates a new Buffer containing string. The encoding parameter identifies the character encoding of string.

      const buf1 = new Buffer('this is a tést');
      const buf2 = new Buffer('7468697320697320612074c3a97374', 'hex');
      
      console.log(buf1.toString());
      // Prints: this is a tést
      console.log(buf2.toString());
      // Prints: this is a tést
      console.log(buf1.toString('ascii'));
      // Prints: this is a tC)st
      

      全部都是废弃的


      更多内容请参考:Node.js Buffer 缓冲器,或者通过 点击对应菜单 进行查看;


      目录
      目录