You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bl.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var DuplexStream = require('readable-stream/duplex')
  2. , util = require('util')
  3. , Buffer = require('safe-buffer').Buffer
  4. function BufferList (callback) {
  5. if (!(this instanceof BufferList))
  6. return new BufferList(callback)
  7. this._bufs = []
  8. this.length = 0
  9. if (typeof callback == 'function') {
  10. this._callback = callback
  11. var piper = function piper (err) {
  12. if (this._callback) {
  13. this._callback(err)
  14. this._callback = null
  15. }
  16. }.bind(this)
  17. this.on('pipe', function onPipe (src) {
  18. src.on('error', piper)
  19. })
  20. this.on('unpipe', function onUnpipe (src) {
  21. src.removeListener('error', piper)
  22. })
  23. } else {
  24. this.append(callback)
  25. }
  26. DuplexStream.call(this)
  27. }
  28. util.inherits(BufferList, DuplexStream)
  29. BufferList.prototype._offset = function _offset (offset) {
  30. var tot = 0, i = 0, _t
  31. if (offset === 0) return [ 0, 0 ]
  32. for (; i < this._bufs.length; i++) {
  33. _t = tot + this._bufs[i].length
  34. if (offset < _t || i == this._bufs.length - 1)
  35. return [ i, offset - tot ]
  36. tot = _t
  37. }
  38. }
  39. BufferList.prototype.append = function append (buf) {
  40. var i = 0
  41. if (Buffer.isBuffer(buf)) {
  42. this._appendBuffer(buf);
  43. } else if (Array.isArray(buf)) {
  44. for (; i < buf.length; i++)
  45. this.append(buf[i])
  46. } else if (buf instanceof BufferList) {
  47. // unwrap argument into individual BufferLists
  48. for (; i < buf._bufs.length; i++)
  49. this.append(buf._bufs[i])
  50. } else if (buf != null) {
  51. // coerce number arguments to strings, since Buffer(number) does
  52. // uninitialized memory allocation
  53. if (typeof buf == 'number')
  54. buf = buf.toString()
  55. this._appendBuffer(Buffer.from(buf));
  56. }
  57. return this
  58. }
  59. BufferList.prototype._appendBuffer = function appendBuffer (buf) {
  60. this._bufs.push(buf)
  61. this.length += buf.length
  62. }
  63. BufferList.prototype._write = function _write (buf, encoding, callback) {
  64. this._appendBuffer(buf)
  65. if (typeof callback == 'function')
  66. callback()
  67. }
  68. BufferList.prototype._read = function _read (size) {
  69. if (!this.length)
  70. return this.push(null)
  71. size = Math.min(size, this.length)
  72. this.push(this.slice(0, size))
  73. this.consume(size)
  74. }
  75. BufferList.prototype.end = function end (chunk) {
  76. DuplexStream.prototype.end.call(this, chunk)
  77. if (this._callback) {
  78. this._callback(null, this.slice())
  79. this._callback = null
  80. }
  81. }
  82. BufferList.prototype.get = function get (index) {
  83. return this.slice(index, index + 1)[0]
  84. }
  85. BufferList.prototype.slice = function slice (start, end) {
  86. if (typeof start == 'number' && start < 0)
  87. start += this.length
  88. if (typeof end == 'number' && end < 0)
  89. end += this.length
  90. return this.copy(null, 0, start, end)
  91. }
  92. BufferList.prototype.copy = function copy (dst, dstStart, srcStart, srcEnd) {
  93. if (typeof srcStart != 'number' || srcStart < 0)
  94. srcStart = 0
  95. if (typeof srcEnd != 'number' || srcEnd > this.length)
  96. srcEnd = this.length
  97. if (srcStart >= this.length)
  98. return dst || Buffer.alloc(0)
  99. if (srcEnd <= 0)
  100. return dst || Buffer.alloc(0)
  101. var copy = !!dst
  102. , off = this._offset(srcStart)
  103. , len = srcEnd - srcStart
  104. , bytes = len
  105. , bufoff = (copy && dstStart) || 0
  106. , start = off[1]
  107. , l
  108. , i
  109. // copy/slice everything
  110. if (srcStart === 0 && srcEnd == this.length) {
  111. if (!copy) { // slice, but full concat if multiple buffers
  112. return this._bufs.length === 1
  113. ? this._bufs[0]
  114. : Buffer.concat(this._bufs, this.length)
  115. }
  116. // copy, need to copy individual buffers
  117. for (i = 0; i < this._bufs.length; i++) {
  118. this._bufs[i].copy(dst, bufoff)
  119. bufoff += this._bufs[i].length
  120. }
  121. return dst
  122. }
  123. // easy, cheap case where it's a subset of one of the buffers
  124. if (bytes <= this._bufs[off[0]].length - start) {
  125. return copy
  126. ? this._bufs[off[0]].copy(dst, dstStart, start, start + bytes)
  127. : this._bufs[off[0]].slice(start, start + bytes)
  128. }
  129. if (!copy) // a slice, we need something to copy in to
  130. dst = Buffer.allocUnsafe(len)
  131. for (i = off[0]; i < this._bufs.length; i++) {
  132. l = this._bufs[i].length - start
  133. if (bytes > l) {
  134. this._bufs[i].copy(dst, bufoff, start)
  135. } else {
  136. this._bufs[i].copy(dst, bufoff, start, start + bytes)
  137. break
  138. }
  139. bufoff += l
  140. bytes -= l
  141. if (start)
  142. start = 0
  143. }
  144. return dst
  145. }
  146. BufferList.prototype.shallowSlice = function shallowSlice (start, end) {
  147. start = start || 0
  148. end = end || this.length
  149. if (start < 0)
  150. start += this.length
  151. if (end < 0)
  152. end += this.length
  153. var startOffset = this._offset(start)
  154. , endOffset = this._offset(end)
  155. , buffers = this._bufs.slice(startOffset[0], endOffset[0] + 1)
  156. if (endOffset[1] == 0)
  157. buffers.pop()
  158. else
  159. buffers[buffers.length-1] = buffers[buffers.length-1].slice(0, endOffset[1])
  160. if (startOffset[1] != 0)
  161. buffers[0] = buffers[0].slice(startOffset[1])
  162. return new BufferList(buffers)
  163. }
  164. BufferList.prototype.toString = function toString (encoding, start, end) {
  165. return this.slice(start, end).toString(encoding)
  166. }
  167. BufferList.prototype.consume = function consume (bytes) {
  168. while (this._bufs.length) {
  169. if (bytes >= this._bufs[0].length) {
  170. bytes -= this._bufs[0].length
  171. this.length -= this._bufs[0].length
  172. this._bufs.shift()
  173. } else {
  174. this._bufs[0] = this._bufs[0].slice(bytes)
  175. this.length -= bytes
  176. break
  177. }
  178. }
  179. return this
  180. }
  181. BufferList.prototype.duplicate = function duplicate () {
  182. var i = 0
  183. , copy = new BufferList()
  184. for (; i < this._bufs.length; i++)
  185. copy.append(this._bufs[i])
  186. return copy
  187. }
  188. BufferList.prototype.destroy = function destroy () {
  189. this._bufs.length = 0
  190. this.length = 0
  191. this.push(null)
  192. }
  193. ;(function () {
  194. var methods = {
  195. 'readDoubleBE' : 8
  196. , 'readDoubleLE' : 8
  197. , 'readFloatBE' : 4
  198. , 'readFloatLE' : 4
  199. , 'readInt32BE' : 4
  200. , 'readInt32LE' : 4
  201. , 'readUInt32BE' : 4
  202. , 'readUInt32LE' : 4
  203. , 'readInt16BE' : 2
  204. , 'readInt16LE' : 2
  205. , 'readUInt16BE' : 2
  206. , 'readUInt16LE' : 2
  207. , 'readInt8' : 1
  208. , 'readUInt8' : 1
  209. }
  210. for (var m in methods) {
  211. (function (m) {
  212. BufferList.prototype[m] = function (offset) {
  213. return this.slice(offset, offset + methods[m])[m](0)
  214. }
  215. }(m))
  216. }
  217. }())
  218. module.exports = BufferList