summaryrefslogtreecommitdiff
path: root/js/dojo-1.6/dojox/encoding/bits.xd.js
blob: 4b58f091ef37354f5f55a7fc4041ee75345b1f27 (plain)
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
/*
	Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


dojo._xdResourceLoaded(function(dojo, dijit, dojox){
return {depends: [["provide", "dojox.encoding.bits"]],
defineResource: function(dojo, dijit, dojox){if(!dojo._hasResource["dojox.encoding.bits"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.encoding.bits"] = true;
dojo.provide("dojox.encoding.bits");


dojo.getObject("encoding.bits", true, dojox);

dojox.encoding.bits.OutputStream = function(){
	this.reset();
};

dojo.extend(dojox.encoding.bits.OutputStream, {
	reset: function(){
		this.buffer = [];
		this.accumulator = 0;
		this.available = 8;
	},
	putBits: function(value, width){
		while(width){
			var w = Math.min(width, this.available);
			var v = (w <= width ? value >>> (width - w) : value) << (this.available - w);
			this.accumulator |= v & (255 >>> (8 - this.available));
			this.available -= w;
			if(!this.available){
				this.buffer.push(this.accumulator);
				this.accumulator = 0;
				this.available = 8;
			}
			width -= w;
		}
	},
	getWidth: function(){
		return this.buffer.length * 8 + (8 - this.available);
	},
	getBuffer: function(){
		var b = this.buffer;
		if(this.available < 8){ b.push(this.accumulator & (255 << this.available)); }
		this.reset();
		return b;
	}
});

dojox.encoding.bits.InputStream = function(buffer, width){
	this.buffer = buffer;
	this.width = width;
	this.bbyte = this.bit = 0;
};

dojo.extend(dojox.encoding.bits.InputStream, {
	getBits: function(width){
		var r = 0;
		while(width){
			var w = Math.min(width, 8 - this.bit);
			var v = this.buffer[this.bbyte] >>> (8 - this.bit - w);
			r <<= w;
			r |= v & ~(~0 << w);
			this.bit += w;
			if(this.bit == 8){
				++this.bbyte;
				this.bit = 0;
			}
			width -= w;
		}
		return r;
	},
	getWidth: function(){
		return this.width - this.bbyte * 8 - this.bit;
	}
});

}

}};});