Merge pull request #2 from maxogden/buffer-read
switch out the accumulator reader for a faster buffer readermaster
commit
369325f253
@ -1,33 +1,24 @@
|
||||
module.exports = Decoder
|
||||
module.exports = read
|
||||
|
||||
var MSB = 0x80
|
||||
, REST = 0x7F
|
||||
|
||||
function read(buf, offset) {
|
||||
var res = 0
|
||||
, offset = offset || 0
|
||||
, shift = 0
|
||||
, counter = offset
|
||||
, b
|
||||
|
||||
function Decoder() {
|
||||
this.accum = []
|
||||
}
|
||||
Decoder.prototype.write = write;
|
||||
|
||||
function write(byte) {
|
||||
var msb = byte & MSB
|
||||
, accum = this.accum
|
||||
, len
|
||||
, out
|
||||
|
||||
accum[accum.length] = byte & REST
|
||||
if(msb) {
|
||||
return
|
||||
}
|
||||
|
||||
len = accum.length
|
||||
out = 0
|
||||
do {
|
||||
b = buf[counter++]
|
||||
res += shift < 28
|
||||
? (b & REST) << shift
|
||||
: (b & REST) * Math.pow(2, shift)
|
||||
shift += 7
|
||||
} while (b >= MSB)
|
||||
|
||||
for(var i = 0; i < len; ++i) {
|
||||
out |= accum[i] << (7 * i)
|
||||
}
|
||||
read.bytesRead = counter - offset
|
||||
|
||||
accum.length = 0
|
||||
this.ondata(out)
|
||||
return
|
||||
return res
|
||||
}
|
Loading…
Reference in New Issue