/
Reader.js
234 lines (184 loc) · 5.37 KB
/
Reader.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// vim: ts=2:sw=2
var util = require('util');
var Stream = require('stream').Stream;
module.exports = Reader;
util.inherits(Reader, Stream);
function Reader(options) {
Stream.call(this);
if (Buffer.isBuffer(options)) {
options = {buffer: options};
}
options = options || {};
this.writable = true;
this._buffer = options.buffer || new Buffer(0);
this._compact = options.compact || false;
this._paused = false;
this._setOffset(options.offset || 0);
}
Reader.prototype.write = function(newBuffer) {
var bytesAhead = this.bytesAhead();
// existing buffer has enough space in the beginning?
var reuseBuffer = (this._offset >= newBuffer.length);
if (!this.writable) {
var err = new Error('stream not writable');
err.code = 'EPIPE';
this.emit('error', err);
return false;
}
if (reuseBuffer) {
// move unread bytes forward to make room for the new
this._buffer.copy(this._buffer, this._offset - newBuffer.length, this._offset);
this._moveOffset(- newBuffer.length);
// add the new bytes at the end
newBuffer.copy(this._buffer, this._buffer.length - newBuffer.length);
if (this._compact) {
this.compact();
}
} else {
var oldBuffer = this._buffer;
// grow a new buffer that can hold both
this._buffer = new Buffer(bytesAhead + newBuffer.length);
// copy the old and new buffer into it
oldBuffer.copy(this._buffer, 0, this._offset);
newBuffer.copy(this._buffer, bytesAhead);
this._setOffset(0);
}
return !this._paused;
};
Reader.prototype.pause = function() {
this._paused = true;
};
Reader.prototype.resume = function() {
this._paused = false;
this.emit('drain');
};
Reader.prototype.bytesAhead = function() {
return this._buffer.length - this._offset;
};
Reader.prototype.bytesBuffered = function() {
return this._buffer.length;
};
Reader.prototype.uint8 = function() {
return this._buffer.readUInt8(this._moveOffset(1));
};
Reader.prototype.int8 = function() {
return this._buffer.readInt8(this._moveOffset(1));
};
Reader.prototype.uint16BE = function() {
return this._buffer.readUInt16BE(this._moveOffset(2));
};
Reader.prototype.int16BE = function() {
return this._buffer.readInt16BE(this._moveOffset(2));
};
Reader.prototype.uint16LE = function() {
return this._buffer.readUInt16LE(this._moveOffset(2));
};
Reader.prototype.int16LE = function() {
return this._buffer.readInt16LE(this._moveOffset(2));
};
Reader.prototype.uint32BE = function() {
return this._buffer.readUInt32BE(this._moveOffset(4));
};
Reader.prototype.int32BE = function() {
return this._buffer.readInt32BE(this._moveOffset(4));
};
Reader.prototype.uint32LE = function() {
return this._buffer.readUInt32LE(this._moveOffset(4));
};
Reader.prototype.int32LE = function() {
return this._buffer.readInt32LE(this._moveOffset(4));
};
Reader.prototype.float32BE = function() {
return this._buffer.readFloatBE(this._moveOffset(4));
};
Reader.prototype.float32LE = function() {
return this._buffer.readFloatLE(this._moveOffset(4));
};
Reader.prototype.double64BE = function() {
return this._buffer.readDoubleBE(this._moveOffset(8));
};
Reader.prototype.double64LE = function() {
return this._buffer.readDoubleLE(this._moveOffset(8));
};
Reader.prototype.ascii = function(bytes) {
return this._string('ascii', bytes);
};
Reader.prototype.utf8 = function(bytes) {
return this._string('utf8', bytes);
};
Reader.prototype._string = function(encoding, bytes) {
var nullTerminated = (bytes === undefined);
if (nullTerminated) {
bytes = this._nullDistance();
}
var offset = this._offset;
this._moveOffset(bytes);
var value = this._buffer.toString(encoding, offset, this._offset);
if (nullTerminated) {
this._moveOffset(1);
}
return value;
};
Reader.prototype._nullDistance = function() {
for (var i = this._offset; i < this._buffer.length; i++) {
var byte = this._buffer[i];
if (byte === 0) {
return i - this._offset;
}
}
};
Reader.prototype.buffer = function(bytes) {
this._moveOffset(bytes);
var ret = new Buffer(bytes);
this._buffer.copy(ret, 0, this._offset - bytes, this._offset);
return ret;
};
Reader.prototype.skip = function(bytes) {
if (bytes < 0) {
this.emit('error', new Error('tried to skip outsite of the buffer'));
}
this._moveOffset(bytes);
};
Reader.prototype.compact = function() {
if (this.offset < 1) {
return;
}
this._buffer = this._buffer.slice(this._offset);
this._setOffset(0);
};
Reader.prototype.end = function(newBuffer) {
if (undefined !== newBuffer) {
this.write(newBuffer);
}
this.writable = false;
if (0 === this.bytesAhead()) {
this.destroy();
}
return true;
};
Reader.prototype.destroy = function() {
this._buffer = null;
this._offset = 0;
this.writable = false;
this.emit('close');
};
Reader.prototype._setOffset = function(offset) {
var that = this;
if ((offset < 0) || (offset > this.bytesBuffered())) {
this.emit('error', new Error('tried to read outsite of buffer boundary'));
}
this._offset = offset;
// handle end()
if (! this.writable && (this.bytesAhead() === 0)) {
// delay since a read may be in progress
process.nextTick(function () {
that.destroy();
});
}
return offset;
};
Reader.prototype._moveOffset = function(relativeOffset) {
var oldOffset = this._offset;
this._setOffset(oldOffset + relativeOffset);
return oldOffset;
};