1:
27:
28:
33:
34:
42:
43: package ;
44: import ;
45:
46: class BitBuffer
47:
... {
48:
private static final int InitSize = 256;
49:
private static final int NBits = 32;
50:
private static final int BitsInByte = 8;
51:
private static final int BytesInInt = 4;
52:
53:
private int _avail = NBits;
54:
private int _word = 0;
55:
private int _free = 0;
56:
private int _size = InitSize;
57:
private int[] _array = new int[InitSize];
58:
59:
public void close()
60:
... {
61:
if (_avail < NBits)
62:
store(_word << _avail);
63:
else
64:
_avail = 0;
65:
}
66:
67:
public void write(DataOutput out) throws IOException
68:
... {
69:
for (int i = 0; i < _free - 1; i++)
70:
out.writeInt(_array[i]);
71:
int word = _array[_free - 1];
72:
int bytes = BytesInInt - _avail/BitsInByte;
73:
int shift = NBits;
74:
while (bytes-- > 0)
75:
out.writeByte((word >>> (shift -= BitsInByte)) & 0xFF);
76:
}
77:
78:
public void clear()
79:
... {
80:
_word = 0;
81:
_avail = NBits;
82:
_free = 0;
83:
}
84:
85:
... public int byteCount() {
86:
return _free*BytesInInt - _avail/BitsInByte;
87:
}
88:
89:
... public int bitCount() {
90:
return NBits*_free - _avail;
91:
}
92:
93:
public void setFrom(BitBuffer rhs)
94:
... {
95:
_word = rhs._word;
96:
_avail = rhs._avail;
97:
if ((_free = rhs._free) > _size)
98:
_array = new int[_size = rhs._free];
99:
System.arraycopy(rhs._array, 0, _array, 0, _free);
100:
}
101:
102:
private void growArray(int newSize)
103:
... {
104:
int[] newArray = new int[_size = newSize];
105:
System.arraycopy(_array, 0, newArray, 0, _free);
106:
_array = newArray;
107:
}
108:
109:
private void store(int value)
110:
... {
111:
if (_free == _size)
112:
growArray(_size * 2);
113:
_array[_free++] = value;
114:
}
115:
116:
public void append(int bit)
117:
... {
118:
_word = (_word << 1) | bit;
119:
if (--_avail == 0)
120:
... {
121:
store(_word);
122:
_word = 0;
123:
_avail = NBits;
124:
}
125:
}
126:
127:
public void append(int source, int kBits)
128:
... {
129:
if (kBits < _avail)
130:
... {
131:
_word = (_word << kBits) | source;
132:
_avail -= kBits;
133:
}
134:
else if (kBits > _avail)
135:
... {
136:
int leftover = kBits - _avail;
137:
store((_word << _avail) | (source >>> leftover));
138:
_word = source;
139:
_avail = NBits - leftover;
140:
}
141:
else
142:
... {
143:
store((_word << kBits) | source);
144:
_word = 0;
145:
_avail = NBits;
146:
}
147:
}
148:
149:
public void concatenate(BitBuffer bb)
150:
... {
151:
if (NBits*(_size - _free) + _avail < bb.bitCount())
152:
growArray(_free + bb._free + 1);
153:
154:
if (_avail == 0)
155:
... {
156:
System.arraycopy(bb._array, 0, _array, _free, bb._free);
157:
_avail = bb._avail;
158:
_free += bb._free;
159:
}
160:
else
161:
... {
162:
int tp = _free - 1;
163:
int sp = 0;
164:
... do {
165:
_array[tp++] |= bb._array[sp] >>> (NBits - _avail);
166:
_array[tp] = bb._array[sp++] << _avail;
167:
}
168:
while (sp < bb._free);
169:
_free += bb._free;
170:
if ((_avail += bb._avail) >= NBits)
171:
... {
172:
_avail -= NBits;
173:
_free--;
174:
}
175:
}
176:
}
177:
}