-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuffer-builder.js
175 lines (148 loc) · 6.59 KB
/
buffer-builder.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
module.exports = BufferBuilder;
function BufferBuilder(initialCapacity) {
var buffer = Buffer.isBuffer(initialCapacity) ? initialCapacity : new Buffer(initialCapacity || 512);
this.buffers = [buffer];
this.writeIndex = 0;
this.length = 0;
}
/* Append a (subsequence of a) Buffer */
BufferBuilder.prototype.appendBuffer = function(source) {
if (source.length === 0) return this;
var tail = this.buffers[this.buffers.length-1];
var spaceInCurrent = tail.length - this.writeIndex;
if (source.length <= spaceInCurrent) {
// We can fit the whole thing in the current buffer
source.copy(tail, this.writeIndex);
this.writeIndex += source.length;
} else {
// Copy as much as we can into the current buffer
if (spaceInCurrent) { // Buffer.copy does not handle the degenerate case well
source.copy(tail, this.writeIndex);//, start, start + spaceInCurrent);
}
// Fit the rest into a new buffer. Make sure it is at least as big as
// what we're being asked to add, and also follow our double-previous-buffer pattern.
var newBuf = new Buffer(Math.max(tail.length*2, source.length));
this.buffers.push(newBuf);
this.writeIndex = source.copy(newBuf, 0, spaceInCurrent);
}
this.length += source.length;
return this;
};
function makeAppender(encoder, size) {
return function(x) {
var buf = this.buffers[this.buffers.length-1];
if (this.writeIndex + size <= buf.length) {
encoder.call(buf, x, this.writeIndex, true);
this.writeIndex += size;
this.length += size;
} else {
var scratchBuffer = new Buffer(size);
encoder.call(scratchBuffer, x, 0, true);
this.appendBuffer(scratchBuffer);
}
return this;
};
}
BufferBuilder.prototype.appendUInt8 = makeAppender(Buffer.prototype.writeUInt8, 1);
BufferBuilder.prototype.appendUInt16LE = makeAppender(Buffer.prototype.writeUInt16LE, 2);
BufferBuilder.prototype.appendUInt16BE = makeAppender(Buffer.prototype.writeUInt16BE, 2);
BufferBuilder.prototype.appendUInt32LE = makeAppender(Buffer.prototype.writeUInt32LE, 4);
BufferBuilder.prototype.appendUInt32BE = makeAppender(Buffer.prototype.writeUInt32BE, 4);
BufferBuilder.prototype.appendInt8 = makeAppender(Buffer.prototype.writeInt8, 1);
BufferBuilder.prototype.appendInt16LE = makeAppender(Buffer.prototype.writeInt16LE, 2);
BufferBuilder.prototype.appendInt16BE = makeAppender(Buffer.prototype.writeInt16BE, 2);
BufferBuilder.prototype.appendInt32LE = makeAppender(Buffer.prototype.writeInt32LE, 4);
BufferBuilder.prototype.appendInt32BE = makeAppender(Buffer.prototype.writeInt32BE, 4);
BufferBuilder.prototype.appendFloatLE = makeAppender(Buffer.prototype.writeFloatLE, 4);
BufferBuilder.prototype.appendFloatBE = makeAppender(Buffer.prototype.writeFloatBE, 4);
BufferBuilder.prototype.appendDoubleLE = makeAppender(Buffer.prototype.writeDoubleLE, 8);
BufferBuilder.prototype.appendDoubleBE = makeAppender(Buffer.prototype.writeDoubleBE, 8);
BufferBuilder.prototype.appendString = function(str, encoding) {
return this.appendBuffer(new Buffer(str, encoding));
};
BufferBuilder.prototype.appendStringZero = function(str, encoding) {
return this.appendString(str + '\0', encoding);
}
BufferBuilder.prototype.appendFill = function(value, count) {
if (!count) return;
var tail = this.buffers[this.buffers.length-1];
var spaceInCurrent = tail.length - this.writeIndex;
if (count <= spaceInCurrent) {
// We can fit the whole thing in the current buffer
tail.fill(value, this.writeIndex, this.writeIndex + count);
this.writeIndex += count;
} else {
// Copy as much as we can into the current buffer
if (spaceInCurrent) { // does not handle the degenerate case well
tail.fill(value, this.writeIndex);
}
// Fit the rest into a new buffer. Make sure it is at least as big as
// what we're being asked to add, and also follow our double-previous-buffer pattern.
var newBuf = new Buffer(Math.max(tail.length*2, count));
var couldNotFit = count - spaceInCurrent;
newBuf.fill(value, 0, couldNotFit);
this.buffers.push(newBuf);
this.writeIndex = couldNotFit;
}
this.length += count;
return this;
};
/* Convert to a plain Buffer */
BufferBuilder.prototype.get = function() {
var concatted = new Buffer(this.length);
this.copy(concatted);
return concatted;
};
/* Copy into targetBuffer */
BufferBuilder.prototype.copy = function(targetBuffer, targetStart, sourceStart, sourceEnd) {
targetStart || (targetStart = 0);
sourceStart || (sourceStart = 0);
sourceEnd !== undefined || (sourceEnd = this.length);
// Validation. Besides making us fail nicely, this makes it so we can skip checks below.
if (targetStart < 0 || (targetStart>0 && targetStart >= targetBuffer.length)) {
throw new Error('targetStart is out of bounds');
}
if (sourceEnd < sourceStart) {
throw new Error('sourceEnd < sourceStart');
}
if (sourceStart < 0 || (sourceStart>0 && sourceStart >= this.length)) {
throw new Error('sourceStart is out of bounds');
}
if (sourceEnd > this.length) {
throw new Error('sourceEnd out of bounds');
}
sourceEnd = Math.min(sourceEnd, sourceStart + (targetBuffer.length-targetStart));
var targetWriteIdx = targetStart;
var readBuffer = 0;
// Skip through our buffers until we get to where the copying should start.
var copyLength = sourceEnd - sourceStart;
var skipped = 0;
while (skipped < sourceStart) {
var buffer = this.buffers[readBuffer];
if (buffer.length + skipped < targetStart) {
skipped += buffer.length;
} else {
// Do the first copy. This one is different from the others in that it
// does not start from the beginning of one of our internal buffers.
var copyStart = sourceStart - skipped;
var inThisBuffer = Math.min(copyLength, buffer.length - copyStart);
buffer.copy(targetBuffer, targetWriteIdx, copyStart, copyStart + inThisBuffer);
targetWriteIdx += inThisBuffer;
copyLength -= inThisBuffer;
readBuffer++;
break;
}
readBuffer++;
}
// Copy the rest. Note that we can't run off of our end because we validated the range up above
while (copyLength > 0) {
var buffer = this.buffers[readBuffer];
var toCopy = Math.min(buffer.length, copyLength);
buffer.copy(targetBuffer, targetWriteIdx, 0, toCopy);
copyLength -= toCopy;
targetWriteIdx += toCopy;
readBuffer++;
}
// Return how many bytes were copied
return sourceEnd - sourceStart;
};