| 1 |
define([ |
| 2 |
"dojo/_base/lang" |
| 3 |
], function(lang) { |
| 4 |
var bits = lang.getObject("dojox.encoding.bits", true); |
| 5 |
|
| 6 |
bits.OutputStream = function(){ |
| 7 |
this.reset(); |
| 8 |
}; |
| 9 |
|
| 10 |
lang.extend(bits.OutputStream, { |
| 11 |
reset: function(){ |
| 12 |
this.buffer = []; |
| 13 |
this.accumulator = 0; |
| 14 |
this.available = 8; |
| 15 |
}, |
| 16 |
putBits: function(value, width){ |
| 17 |
while(width){ |
| 18 |
var w = Math.min(width, this.available); |
| 19 |
var v = (w <= width ? value >>> (width - w) : value) << (this.available - w); |
| 20 |
this.accumulator |= v & (255 >>> (8 - this.available)); |
| 21 |
this.available -= w; |
| 22 |
if(!this.available){ |
| 23 |
this.buffer.push(this.accumulator); |
| 24 |
this.accumulator = 0; |
| 25 |
this.available = 8; |
| 26 |
} |
| 27 |
width -= w; |
| 28 |
} |
| 29 |
}, |
| 30 |
getWidth: function(){ |
| 31 |
return this.buffer.length * 8 + (8 - this.available); |
| 32 |
}, |
| 33 |
getBuffer: function(){ |
| 34 |
var b = this.buffer; |
| 35 |
if(this.available < 8){ b.push(this.accumulator & (255 << this.available)); } |
| 36 |
this.reset(); |
| 37 |
return b; |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
bits.InputStream = function(buffer, width){ |
| 42 |
this.buffer = buffer; |
| 43 |
this.width = width; |
| 44 |
this.bbyte = this.bit = 0; |
| 45 |
}; |
| 46 |
|
| 47 |
lang.extend(bits.InputStream, { |
| 48 |
getBits: function(width){ |
| 49 |
var r = 0; |
| 50 |
while(width){ |
| 51 |
var w = Math.min(width, 8 - this.bit); |
| 52 |
var v = this.buffer[this.bbyte] >>> (8 - this.bit - w); |
| 53 |
r <<= w; |
| 54 |
r |= v & ~(~0 << w); |
| 55 |
this.bit += w; |
| 56 |
if(this.bit == 8){ |
| 57 |
++this.bbyte; |
| 58 |
this.bit = 0; |
| 59 |
} |
| 60 |
width -= w; |
| 61 |
} |
| 62 |
return r; |
| 63 |
}, |
| 64 |
getWidth: function(){ |
| 65 |
return this.width - this.bbyte * 8 - this.bit; |
| 66 |
} |
| 67 |
}); |
| 68 |
|
| 69 |
|
| 70 |
return bits; |
| 71 |
}); |