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
|
var MSB = 0x80
|
||||||
, REST = 0x7F
|
, REST = 0x7F
|
||||||
|
|
||||||
|
function read(buf, offset) {
|
||||||
|
var res = 0
|
||||||
|
, offset = offset || 0
|
||||||
|
, shift = 0
|
||||||
|
, counter = offset
|
||||||
|
, b
|
||||||
|
|
||||||
function Decoder() {
|
do {
|
||||||
this.accum = []
|
b = buf[counter++]
|
||||||
}
|
res += shift < 28
|
||||||
Decoder.prototype.write = write;
|
? (b & REST) << shift
|
||||||
|
: (b & REST) * Math.pow(2, shift)
|
||||||
function write(byte) {
|
shift += 7
|
||||||
var msb = byte & MSB
|
} while (b >= MSB)
|
||||||
, accum = this.accum
|
|
||||||
, len
|
|
||||||
, out
|
|
||||||
|
|
||||||
accum[accum.length] = byte & REST
|
|
||||||
if(msb) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
len = accum.length
|
|
||||||
out = 0
|
|
||||||
|
|
||||||
for(var i = 0; i < len; ++i) {
|
read.bytesRead = counter - offset
|
||||||
out |= accum[i] << (7 * i)
|
|
||||||
}
|
|
||||||
|
|
||||||
accum.length = 0
|
return res
|
||||||
this.ondata(out)
|
|
||||||
return
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue