-
Notifications
You must be signed in to change notification settings - Fork 1
/
bufferhelper.coffee
46 lines (38 loc) · 915 Bytes
/
bufferhelper.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class BufferHelper
constructor: () ->
@buffers = []
@size = 0
@_status = 'changed'
concat: (buffer) ->
@_concat(arguments[i]) for i in [0...arguments.length]
return @
_concat: (buffer) ->
@buffers.push buffer
@size += buffer.length
@_status = 'changed'
return @
empty: () ->
@buffers = []
@size = 0
@_status = 'changed'
_toBuffer: () ->
data = null
buffers = @buffers
switch @buffers.length
when 0 then data = new Buffer(0)
when 1 then data = buffers[0]
else
data = new Buffer(@size)
pos = 0
for i in [0...buffers.length]
buffer = buffers[i]
buffer.copy(data, pos)
pos += buffer.length
@_status = 'computed'
@buffer = data
return data
toBuffer: () ->
return if @_status == 'computed' then @buffer else @_toBuffer()
toString: () ->
return Buffer.prototype.toString.apply(@toBuffer(), arguments)
module.exports = BufferHelper