node. JS buffer module buffered data for processing I/O data

Source: Internet
Author: User

First, the prequel

In the previous web-making, JS objects were often used to go to JSON and JSON to JS objects. Since it is node. js processing I/O data, I also write this down.

JSON to JS object: Json.parse (JSONSTR); You can convert a JSON string into a JSON object
JS Object Turn Json:JSON.stringify (jsonobj); You can convert a JSON object to a JSON string

var jsonstr= ' {' name ': ' Cuiyanwei ', ' sex ': ' Male ', ' blog ': ' http://www.cnblogs.com/5ishare/'} '; // json to JS object var jsobj=Json.parse (JSONSTR); Console.log (jsobj); // JS Object goto JSON var newjsonstr=json.stringify (jsobj); Console.log (NEWJSONSTR);
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe"' Cuiyanwei ',  ' Male ',   ' http://www.cnblogs.com/5ishare/' } {' name ': ' Cuiyanwei ', ' sex ': ' Male ', ' blog ': '/http '/' Www.cnblogs.com/5ishare/" with exit code 0

Buffer Module Buffers Data

1. Introduction

Buffered data is made up of a series of big-endian or small-end (this to Baidu) format bytes, which takes up less space than text data. The buffer module is global and does not require the use of require () to access it.

Buffered data is stored in an array-like structure, but is stored in the original memory allocation area outside of the normal V8 heap memory and therefore cannot be resized.

2. Use

In the introduction, the buffer data is stored in a similar array of structure, the basic and the operation of the group is similar: Create object, read, write, split, copy, merge, etc.

1). Create

The buffer object is in the original memory allocation area, and determines the size at the time of creation.

    //     1. Byte size 2. Eight-bit byte buffer 3.utf8 string var  buffer256=new  buffer (n); var  bufferoctets=New Buffer ([0x6f,0x63,0x74,0x65,0x74,0x73]); var bufferutf8=New  Buffer ("Some UTF8 Text", ' UTF8 ');

2). Write (Many ways)

 var  buf256=new  Buffer (25); // fill (Value,[offset],[end]) writes value to each byte that the buffer starts at the offset index (default 0) and ends at the end index ( Default buffer length)  buf256.fill ("1", 0 // wirte (String,[offset],[length],[encoding]) Use encoding encoding to start writing from the buffer offset index to the length number of bytes in the string  buf256.write ("abc", "UTF8"  6]=56; //  console.log (buf256.tostring ()); 
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe buffer.js 1111111111111111111111111 with exit code 0

3). Read (reversible, have write and read)

varbufutf8=NewBuffer ("Some UTF8 Text", ' UTF8 '); Console.log (bufutf8.tostring ());//toString ([Encoding],[start],[end]) uses encoding encoding to read from start (0) to end (tail)Console.log (bufutf8.tostring (' UTF8 ', 0,4));//returns the octal value of the buffer at the specified offset byteConsole.log (Bufutf8[8].tostring (16));//node. JS provides a Stringdecoder object, has a write (buffer) to decode, and writes buffer data using the specified encodingvarStringdecoder=require (' String_decoder '). Stringdecoder;varDecoder=NewStringdecoder (' UTF8 '); Console.log (Decoder.write (bufUTF8));
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe buffer.jssome UTF8 textsome $ with exit code 0 

4.Buffer length

The length of the buffer is the length of the computed byte, and the length of the string is the length of the character.

var s= ' UTF8 text \u00b6 '; Console.log (s.length); Console.log (Buffer.bytelength (S,' UTF8 ')); Console.log (Buffer (s). Length)
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe buffer.js111212    with exit code 0

5. The copy buffer is primarily the copy method

var bufsource=New  Buffer ("1234567890", ' UTF8 '); var buftarget=New  Buffer ("abcedfghijklmn", ' UTF8 '); // copy. (Targetbuffer, Targetstart, Sourcestart, Sourceend) // bufsource.copy (buftarget,10,3,9);//abcedfghij4567bufsource.copy (buftarget,0,3,9); // 456789ghijklmnConsole.log (buftarget.tostring ());

6. Slicing the buffer

This is a bit like string interception. Slice ([start],[end]), which returns a buffer object. If editing a slice also changes the original buffer.

var  number=New Buffer (' 123456789 '); Console.log (number.tostring ()); var slice=number.slice (3.9); Console.log (slice.tostring ());slice[0]= ' # '. charCodeAt (0); Slice[slice.length-1]= ' # '. charCodeAt (0); Console.log (slice.tostring ()); Console.log (number.tostring () );
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe" F:\nodejs\node.exe buffer.js 123456789456789#5678#123#5678 with exit code 0

7. Splicing Buffers

This is a bit like string concatenation in C. Concat (List,[totallength]) You can stitch multiple buffer objects together to form a new buffer.

var New Buffer ("123"); var New Buffer ("abc"); Console.log (Buffer.concat ([Buff1, buff2,buff1]). toString ());
"C:\Program Files (x86) \jetbrains\webstorm 11.0.3\bin\runnerw.exe" with exit code 0

node. JS buffer module buffered data for processing I/O data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.