# bl *(BufferList)*
[![Build Status](https://api.travis-ci.com/rvagg/bl.svg?branch=master)](https://travis-ci.com/rvagg/bl/)
**A Node.js Buffer list collector, reader and streamer thingy.**
[![NPM](https://nodei.co/npm/bl.svg)](https://nodei.co/npm/bl/)
**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!
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.
```js
const { BufferList } = require('bl')
const bl = new BufferList()
bl.append(Buffer.from('abcd'))
bl.append(Buffer.from('efg'))
bl.append('hi') // bl will also accept & convert Strings
bl.append(Buffer.from('j'))
bl.append(Buffer.from([ 0x3, 0x4 ]))
console.log(bl.length) // 12
console.log(bl.slice(0, 10).toString('ascii')) // 'abcdefghij'
console.log(bl.slice(3, 10).toString('ascii')) // 'defghij'
console.log(bl.slice(3, 6).toString('ascii')) // 'def'
console.log(bl.slice(3, 8).toString('ascii')) // 'defgh'
console.log(bl.slice(5, 10).toString('ascii')) // 'fghij'
console.log(bl.indexOf('def')) // 3
console.log(bl.indexOf('asdf')) // -1
// or just use toString!
console.log(bl.toString()) // 'abcdefghij\u0003\u0004'
console.log(bl.toString('ascii', 3, 8)) // 'defgh'
console.log(bl.toString('ascii', 5, 10)) // 'fghij'
// other standard Buffer readables
console.log(bl.readUInt16BE(10)) // 0x0304
console.log(bl.readUInt16LE(10)) // 0x0403
```
Give it a callback in the constructor and use it just like **[concat-stream](https://github.com/maxogden/node-concat-stream)**:
```js
const { BufferListStream } = require('bl')
const fs = require('fs')
fs.createReadStream('README.md')
.pipe(BufferListStream((err, data) => { // note 'new' isn't strictly required
// `data` is a complete Buffer object containing the full data
console.log(data.toString())
}))
```
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.
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!):
```js
const hyperquest = require('hyperquest')
const { BufferListStream } = require('bl')
const url = 'https://raw.github.com/rvagg/bl/master/README.md'
hyperquest(url).pipe(BufferListStream((err, data) => {
console.log(data.toString())
}))
```
Or, use it as a readable stream to recompose a list of Buffers to an output source:
```js
const { BufferListStream } = require('bl')
const fs = require('fs')
var bl = new BufferListStream()
bl.append(Buffer.from('abcd'))
bl.append(Buffer.from('efg'))
bl.append(Buffer.from('hi'))
bl.append(Buffer.from('j'))
bl.pipe(fs.createWriteStream('gibberish.txt'))
```
## API
* <a href="#ctor"><code><b>new BufferList([ buf ])</b></code></a>
* <a href="#isBufferList"><code><b>BufferList.isBufferList(obj)</b></code></a>
* <a href="#length"><code>bl.<b>length</b></code></a>
* <a href="#append"><code>bl.<b>append(buffer)</b></code></a>
* <a href="#get"><code>bl.<b>get(index)</b></code></a>
* <a href="#indexOf"><code>bl.<b>indexOf(value[, byteOffset][, encoding])</b></code></a>
* <a href="#slice"><code>bl.<b>slice([ start[, end ] ])</b></code></a>
* <a href="#shallowSlice"><code>bl.<b>shallowSlice([ start[, end ] ])</b></code></a>
* <a href="#copy"><code>bl.<b>copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])</b></code></a>
* <a href="#duplicate"><code>bl.<b>duplicate()</b></code></a>
* <a href="#consume"><code>bl.<b>consume(bytes)</b></code></a>
* <a href="#toString"><code>bl.<b>toString([encoding, [ start, [ end ]]])</b></code></a>
* <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>
* <a href="#ctorStream"><code><b>new BufferListStream([ callback ])</b></code></a>
--------------------------------------------------------
<a name="ctor"></a>
### new BufferList([ Buffer | Buffer array | BufferList | BufferList array | String ])
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` objects.
`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:
```js
const { BufferList } = require('bl')
const bl = BufferList()
// equivalent to:
const { BufferList } = require('bl')
const bl = new BufferList()
```
--------------------------------------------------------
<a name="isBufferList"></a>
### BufferList.isBufferList(obj)
Determines if the passed object is a `BufferList`. It will return `true` if the passed object is an instance of `BufferList` **or** `BufferListStream` and `false` otherwise.
N.B. this won't return `true` for `BufferList` or `BufferListStream` instances created by versions of this library before this static method was added.
--------------------------------------------------------
<a name="length"></a>
### bl.length
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.
--------------------------------------------------------
<a name="append"></a>
### bl.append(Buffer | Buffer array | BufferList | BufferList array | String)
`append(buffer)` adds an additional buffer or BufferList to the internal list. `this` is returned so it can be chained.
--------------------------------------------------------
<a name="get"></a>
### bl.get(index)
`get()` will return the byte at the specified index.
--------------------------------------------------------
<a name="indexOf"></a>
### bl.indexOf(value[, byteOffset][, encoding])
`get()` will return the byte at the specified index.
`indexOf()` method returns the first index at which a given element can be found in the BufferList, or -1 if it is not present.
--------------------------------------------------------
<a name="slice"></a>
### bl.slice([ start, [ end ] ])
`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.
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.
--------------------------------------------------------
<a name="shallowSlice"></a>
### bl.shallowSlice([ start, [ end ] ])
`shallowSlice()` returns a new `BufferList` 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 respe
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
Nosql类型数据库。 (528个子文件)
app.asar 46.1MB
AUTHORS 279B
cypher-shell.bat 2KB
neo4j-import.bat 933B
neo4j-admin.bat 932B
neo4j.bat 926B
v8_context_snapshot.bin 162KB
snapshot_blob.bin 47KB
kerberos_gss.cc 24KB
kerberos_sspi.cc 17KB
kerberos_unix.cc 9KB
kerberos.cc 7KB
kerberos_win32.cc 6KB
base64.cc 4KB
binding.cc 3KB
neo4j.cer 1KB
redis.windows-service.conf 47KB
redis.windows.conf 47KB
neo4j.conf 15KB
qt.conf 19B
icudtl.dat 9.93MB
opengl32sw.dll 14.52MB
libGLESv2.dll 7.57MB
Qt5Gui.dll 4.77MB
vk_swiftshader.dll 4.47MB
Qt5Core.dll 4.41MB
d3dcompiler_47.dll 4.32MB
Qt5Widgets.dll 4.22MB
d3dcompiler_47.dll 3.57MB
Qt5Quick.dll 2.45MB
Qt5Qml.dll 2.39MB
ffmpeg.dll 1.58MB
libGLESV2.dll 1.51MB
libeay32.dll 1.16MB
qwindows.dll 985KB
Qt5Network.dll 832KB
qtquickextrasflatplugin.dll 793KB
vulkan-1.dll 708KB
qtquickcontrolsplugin.dll 680KB
libEGL.dll 429KB
qwebp.dll 319KB
qtiff.dll 309KB
ssleay32.dll 291KB
Qt5Svg.dll 250KB
qjpeg.dll 240KB
dialogplugin.dll 168KB
qmldbg_debugger.dll 135KB
qtquickextrasplugin.dll 121KB
widgetsplugin.dll 96KB
qquicklayoutsplugin.dll 71KB
qmldbg_inspector.dll 65KB
Qt5QuickWidgets.dll 55KB
qmldbg_profiler.dll 51KB
qmldbg_server.dll 47KB
qmlfolderlistmodelplugin.dll 45KB
qnativewifibearer.dll 41KB
qtgraphicaleffectsprivate.dll 41KB
qdds.dll 40KB
qgenericbearer.dll 39KB
dialogsprivateplugin.dll 37KB
qicns.dll 33KB
qsvgicon.dll 31KB
qico.dll 27KB
qmldbg_native.dll 27KB
qgif.dll 26KB
qmlsettingsplugin.dll 24KB
qsvg.dll 21KB
qtga.dll 21KB
qwbmp.dll 20KB
qmldbg_local.dll 20KB
qmldbg_tcp.dll 19KB
windowplugin.dll 16KB
qtquick2plugin.dll 16KB
modelsplugin.dll 15KB
libEGL.dll 13KB
EventLog.dll 1024B
Redis on Windows.docx 16KB
Windows Service Documentation.docx 14KB
Redis on Windows Release Notes.docx 12KB
MongoDBCompass.exe 130.07MB
vcredist_x86.exe 13.79MB
rdm.exe 3.19MB
redis-server.exe 1.59MB
squirrel.exe 1.45MB
redis-cli.exe 488KB
redis-benchmark.exe 400KB
uninstall.exe 397KB
redis-check-aof.exe 251KB
prunsrv-amd64.exe 107KB
prunsrv-i386.exe 85KB
crashreporter.exe 55KB
interruptor.exp 1KB
kerberos.exp 968B
kerberos.vcxproj.filters 4KB
interruptor.vcxproj.filters 3KB
binding.gyp 2KB
binding.gyp 95B
config.gypi 4KB
config.gypi 4KB
kerberos_gss.h 4KB
共 528 条
- 1
- 2
- 3
- 4
- 5
- 6
大米☋
- 粉丝: 50
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0