2019-01-12 admin


什么是bl,Buffer List: collect buffers and access with a standard readable Buffer interface, streamable too!


bl (BufferList)

Build Status

A Node.js Buffer list collector, reader and streamer thingy.


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.

const BufferList = require('bl')

var bl = new BufferList()
bl.append('hi')                     // bl will also accept & convert Strings
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'

// 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:

const bl = require('bl')
    , fs = require('fs')

  .pipe(bl(function (err, data) { // note 'new' isn't strictly required
    // `data` is a complete Buffer object containing the full data

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 (should work with request and even plain Node http too!):

const hyperquest = require('hyperquest')
    , bl         = require('bl')
    , url        = ''

hyperquest(url).pipe(bl(function (err, data) {

Or, use it as a readable stream to recompose a list of Buffers to an output source:

const BufferList = require('bl')
    , fs         = require('fs')

var bl = new BufferList()



  • **new BufferList([ callback ])**
  • bl.**length**
  • bl.**append(buffer)**
  • bl.**get(index)**
  • bl.**slice([ start[, end ] ])**
  • bl.**shallowSlice([ start[, end ] ])**
  • bl.**copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])**
  • bl.**duplicate()**
  • bl.**consume(bytes)**
  • bl.**toString([encoding, [ start, [ end ]]])**
  • 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()**
  • Streams

<a name=“user-content-ctor”></a>

new BufferList([ callback | Buffer | Buffer array | BufferList | BufferList array | String ])

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.

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.

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:

var bl = require('bl')
var myinstance = bl()

// equivalent to:

var BufferList = require('bl')
var myinstance = new BufferList()

<a name=“user-content-length”></a>


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=“user-content-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=“user-content-get”></a>


get() will return the byte at the specified index.

<a name=“user-content-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=“user-content-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 respectively.

No copies will be performed. All buffers in the result share memory with the original list.

<a name=“user-content-copy”></a>

bl.copy(dest, [ destStart, [ srcStart [, srcEnd ] ] ])

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.

<a name=“user-content-duplicate”></a>


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:

var bl = new BufferList()

bl.append(' world')

bl.duplicate().pipe(process.stdout, { end: false })


<a name=“user-content-consume”></a>


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—initial offsets will be calculated accordingly in order to give you a consistent view of the data.

<a name=“user-content-tostring”></a>

bl.toString([encoding, [ start, [ end ]]])

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() documentation for more information.

<a name=“user-content-readxx”></a>

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()

All of the standard byte-reading methods of the Buffer interface are implemented and will operate across internal Buffer boundaries transparently.

See the [Buffer]( documentation for how these work.

<a name=“user-content-streams”></a>


bl is a Node Duplex Stream, so it can be read from and written to like a standard Node stream. You can also pipe() to and from a bl instance.


bl is brought to you by the following hackers:


<a name=“user-content-license”></a>

License & copyright

Copyright © 2013-2018 bl contributors (listed above).

bl is licensed under the MIT license. All rights not explicitly granted in the MIT license are reserved. See the included file for more details.


转载请注明:文章转载自 JavaScript中文网 []



本文实例讲述了javascript实现Table排序的方法。分享给大家供大家参考。具体实现方法如下: &lt;!DOCTYPE html PUBLIC &quot;-&#x2F;&#x2F;W3C&#x2F;&#x2F;DTD XHTML ...
本文实例讲述了javascript实现Table间隔色以及选择高亮(和动态切换数据)的方法。分享给大家供大家参考。具体实现方法如下: &lt;!DOCTYPE html PUBLIC &quot;-&#x2F;&#x2F;W3C&#x2F;...
在项目做完之余,测试最近做的一个论坛,其中有个导出功能,发现点击网页导出按钮之后,将页面的table内容另存为excel文件后,却发现无法保存表格样式,经过问题分析之后找到根本原因,下面小编就把我的实现思路给大家分享如下: 问题描述: 问题...
handsontable是什么 什么是handsontable,Spreadsheet-like data grid editor that provides copy/paste functionality compatible with...
简介 在使用vue1.x之前的版本的时候,页面中的拖拽功能,我在项目中是直接用的jquery ui中的sortable.js,只是在拖拽完成后,在update的回调函数中又重新排序了存放数据的数组。但是当把vue升级到2.0以上后发现拖拽功...
本文实例讲述了javascript清空table表格的方法。分享给大家供大家参考。具体如下: 1. 通常方法 循环table的rows,然后一个一个删除。 这个方法是通常的方法,可行,但是效率不好。 2. 另一个方法 var artTabl...
使用contextMenu插件实现Bootstrap table弹出右键菜单
如今Bootstrap这个前端框架已被许多人接受并应用在不同的项目中,其中“开发高效,设备兼容”的特点表现得非常明显。再加上它默认的美观的UI组件,简直就是很多前端的大爱啊!!! 今天在这里跟大家分享Bootstrap table弹出右键菜...
easyui Draggable组件实现拖动效果
easyui做为一个封装了JQusey的UI插件,其实还是蛮好用的,至少省了像我这种渣渣很多时间。 Draggable的加载方式有两种: 1,通过class加载,如下: &lt;div id=&quot;box&quot; class=&q...
Vue.Draggable是什么 什么是Vue.Draggable,Vue component allowing drag-and-drop sorting in sync with View-Model. Based on Sortabl...
最近遇到这个需要单页访问跨域后台的问题。 遇到了网上很多人说的,proxyTable无论如何修改,都没效果的现象。 经过几轮测试,总结出一下几种解决方案: 1.(非常重要)确保proxyTable配置的地址能访问,因为如果不能访问,在浏览器...