Version 1
[yaffs-website] / node_modules / bl / README.md
1 # bl *(BufferList)*
2
3 [![Build Status](https://travis-ci.org/rvagg/bl.svg?branch=master)](https://travis-ci.org/rvagg/bl)
4
5 **A Node.js Buffer list collector, reader and streamer thingy.**
6
7 [![NPM](https://nodei.co/npm/bl.png?downloads=true&downloadRank=true)](https://nodei.co/npm/bl/)
8 [![NPM](https://nodei.co/npm-dl/bl.png?months=6&height=3)](https://nodei.co/npm/bl/)
9
10 **bl** is a storage object for collections of Node Buffers, exposing them with the main Buffer readable API. Also works as a duplex stream so you can collect buffers from a stream that emits them and emit buffers to a stream that consumes them!
11
12 The original buffers are kept intact and copies are only done as necessary. Any reads that require the use of a single original buffer will return a slice of that buffer only (which references the same memory as the original buffer). Reads that span buffers perform concatenation as required and return the results transparently.
13
14 ```js
15 const BufferList = require('bl')
16
17 var bl = new BufferList()
18 bl.append(new Buffer('abcd'))
19 bl.append(new Buffer('efg'))
20 bl.append('hi')                     // bl will also accept & convert Strings
21 bl.append(new Buffer('j'))
22 bl.append(new Buffer([ 0x3, 0x4 ]))
23
24 console.log(bl.length) // 12
25
26 console.log(bl.slice(0, 10).toString('ascii')) // 'abcdefghij'
27 console.log(bl.slice(3, 10).toString('ascii')) // 'defghij'
28 console.log(bl.slice(3, 6).toString('ascii'))  // 'def'
29 console.log(bl.slice(3, 8).toString('ascii'))  // 'defgh'
30 console.log(bl.slice(5, 10).toString('ascii')) // 'fghij'
31
32 // or just use toString!
33 console.log(bl.toString())               // 'abcdefghij\u0003\u0004'
34 console.log(bl.toString('ascii', 3, 8))  // 'defgh'
35 console.log(bl.toString('ascii', 5, 10)) // 'fghij'
36
37 // other standard Buffer readables
38 console.log(bl.readUInt16BE(10)) // 0x0304
39 console.log(bl.readUInt16LE(10)) // 0x0403
40 ```
41
42 Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**:
43
44 ```js
45 const bl = require('bl')
46     , fs = require('fs')
47
48 fs.createReadStream('README.md')
49   .pipe(bl(function (err, data) { // note 'new' isn't strictly required
50     // `data` is a complete Buffer object containing the full data
51     console.log(data.toString())
52   }))
53 ```
54
55 Note that when you use the *callback* method like this, the resulting `data` parameter is a concatenation of all `Buffer` objects in the list. If you want to avoid the overhead of this concatenation (in cases of extreme performance consciousness), then avoid the *callback* method and just listen to `'end'` instead, like a standard Stream.
56
57 Or to fetch a URL using [hyperquest](https://github.com/substack/hyperquest) (should work with [request](http://github.com/mikeal/request) and even plain Node http too!):
58 ```js
59 const hyperquest = require('hyperquest')
60     , bl         = require('bl')
61     , url        = 'https://raw.github.com/rvagg/bl/master/README.md'
62
63 hyperquest(url).pipe(bl(function (err, data) {
64   console.log(data.toString())
65 }))
66 ```
67
68 Or, use it as a readable stream to recompose a list of Buffers to an output source:
69
70 ```js
71 const BufferList = require('bl')
72     , fs         = require('fs')
73
74 var bl = new BufferList()
75 bl.append(new Buffer('abcd'))
76 bl.append(new Buffer('efg'))
77 bl.append(new Buffer('hi'))
78 bl.append(new Buffer('j'))
79
80 bl.pipe(fs.createWriteStream('gibberish.txt'))
81 ```
82
83 ## API
84
85   * <a href="#ctor"><code><b>new BufferList([ callback ])</b></code></a>
86   * <a href="#length"><code>bl.<b>length</b></code></a>
87   * <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
88   * <a href="#get"><code>bl.<b>get(index)</b></code></a>
89   * <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
90   * <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
91   * <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a>
92   * <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a>
93   * <a href="#toString"><code>bl.<b>toString([encoding, [ start, [ end ]]])</b></code></a>
94   * <a href="#readXX"><code>bl.<b>readDoubleBE()</b></code>, <code>bl.<b>readDoubleLE()</b></code>, <code>bl.<b>readFloatBE()</b></code>, <code>bl.<b>readFloatLE()</b></code>, <code>bl.<b>readInt32BE()</b></code>, <code>bl.<b>readInt32LE()</b></code>, <code>bl.<b>readUInt32BE()</b></code>, <code>bl.<b>readUInt32LE()</b></code>, <code>bl.<b>readInt16BE()</b></code>, <code>bl.<b>readInt16LE()</b></code>, <code>bl.<b>readUInt16BE()</b></code>, <code>bl.<b>readUInt16LE()</b></code>, <code>bl.<b>readInt8()</b></code>, <code>bl.<b>readUInt8()</b></code></a>
95   * <a href="#streams">Streams</a>
96
97 --------------------------------------------------------
98 <a name="ctor"></a>
99 ### new BufferList([ callback | buffer | buffer array ])
100 The constructor takes an optional callback, if supplied, the callback will be called with an error argument followed by a reference to the **bl** instance, when `bl.end()` is called (i.e. from a piped stream). This is a convenient method of collecting the entire contents of a stream, particularly when the stream is *chunky*, such as a network stream.
101
102 Normally, no arguments are required for the constructor, but you can initialise the list by passing in a single `Buffer` object or an array of `Buffer` object.
103
104 `new` is not strictly required, if you don't instantiate a new object, it will be done automatically for you so you can create a new instance simply with:
105
106 ```js
107 var bl = require('bl')
108 var myinstance = bl()
109
110 // equivilant to:
111
112 var BufferList = require('bl')
113 var myinstance = new BufferList()
114 ```
115
116 --------------------------------------------------------
117 <a name="length"></a>
118 ### bl.length
119 Get the length of the list in bytes. This is the sum of the lengths of all of the buffers contained in the list, minus any initial offset for a semi-consumed buffer at the beginning. Should accurately represent the total number of bytes that can be read from the list.
120
121 --------------------------------------------------------
122 <a name="append"></a>
123 ### bl.append(buffer)
124 `append(buffer)` adds an additional buffer or BufferList to the internal list.
125
126 --------------------------------------------------------
127 <a name="get"></a>
128 ### bl.get(index)
129 `get()` will return the byte at the specified index.
130
131 --------------------------------------------------------
132 <a name="slice"></a>
133 ### bl.slice([ start, [ end ] ])
134 `slice()` returns a new `Buffer` object containing the bytes within the range specified. Both `start` and `end` are optional and will default to the beginning and end of the list respectively.
135
136 If the requested range spans a single internal buffer then a slice of that buffer will be returned which shares the original memory range of that Buffer. If the range spans multiple buffers then copy operations will likely occur to give you a uniform Buffer.
137
138 --------------------------------------------------------
139 <a name="copy"></a>
140 ### bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])
141 `copy()` copies the content of the list in the `dest` buffer, starting from `destStart` and containing the bytes within the range specified with `srcStart` to `srcEnd`. `destStart`, `start` and `end` are optional and will default to the beginning of the `dest` buffer, and the beginning and end of the list respectively.
142
143 --------------------------------------------------------
144 <a name="duplicate"></a>
145 ### bl.duplicate()
146 `duplicate()` performs a **shallow-copy** of the list. The internal Buffers remains the same, so if you change the underlying Buffers, the change will be reflected in both the original and the duplicate. This method is needed if you want to call `consume()` or `pipe()` and still keep the original list.Example:
147
148 ```js
149 var bl = new BufferList()
150
151 bl.append('hello')
152 bl.append(' world')
153 bl.append('\n')
154
155 bl.duplicate().pipe(process.stdout, { end: false })
156
157 console.log(bl.toString())
158 ```
159
160 --------------------------------------------------------
161 <a name="consume"></a>
162 ### bl.consume(bytes)
163 `consume()` will shift bytes *off the start of the list*. The number of bytes consumed don't need to line up with the sizes of the internal Buffers&mdash;initial offsets will be calculated accordingly in order to give you a consistent view of the data.
164
165 --------------------------------------------------------
166 <a name="toString"></a>
167 ### bl.toString([encoding, [ start, [ end ]]])
168 `toString()` will return a string representation of the buffer. The optional `start` and `end` arguments are passed on to `slice()`, while the `encoding` is passed on to `toString()` of the resulting Buffer. See the [Buffer#toString()](http://nodejs.org/docs/latest/api/buffer.html#buffer_buf_tostring_encoding_start_end) documentation for more information.
169
170 --------------------------------------------------------
171 <a name="readXX"></a>
172 ### bl.readDoubleBE(), bl.readDoubleLE(), bl.readFloatBE(), bl.readFloatLE(), bl.readInt32BE(), bl.readInt32LE(), bl.readUInt32BE(), bl.readUInt32LE(), bl.readInt16BE(), bl.readInt16LE(), bl.readUInt16BE(), bl.readUInt16LE(), bl.readInt8(), bl.readUInt8()
173
174 All of the standard byte-reading methods of the `Buffer` interface are implemented and will operate across internal Buffer boundaries transparently.
175
176 See the <b><code>[Buffer](http://nodejs.org/docs/latest/api/buffer.html)</code></b> documentation for how these work.
177
178 --------------------------------------------------------
179 <a name="streams"></a>
180 ### Streams
181 **bl** is a Node **[Duplex Stream](http://nodejs.org/docs/latest/api/stream.html#stream_class_stream_duplex)**, so it can be read from and written to like a standard Node stream. You can also `pipe()` to and from a **bl** instance.
182
183 --------------------------------------------------------
184
185 ## Contributors
186
187 **bl** is brought to you by the following hackers:
188
189  * [Rod Vagg](https://github.com/rvagg)
190  * [Matteo Collina](https://github.com/mcollina)
191  * [Jarett Cruger](https://github.com/jcrugzz)
192
193 =======
194
195 <a name="license"></a>
196 ## License &amp; copyright
197
198 Copyright (c) 2013-2014 bl contributors (listed above).
199
200 bl is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.