2
* @(#)Decompressor.java 1.7 06/10/30
4
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7
* This code is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 2 only, as
9
* published by the Free Software Foundation. Sun designates this
10
* particular file as subject to the "Classpath" exception as provided
11
* by Sun in the LICENSE file that accompanied this code.
13
* This code is distributed in the hope that it will be useful, but WITHOUT
14
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16
* version 2 for more details (a copy is included in the LICENSE file that
17
* accompanied this code).
19
* You should have received a copy of the GNU General Public License version
20
* 2 along with this work; if not, write to the Free Software Foundation,
21
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24
* CA 95054 USA or visit www.sun.com if you need additional information or
30
* @author Jacek R. Ambroziak
31
* @group Sun Microsystems Laboratories
34
package com.sun.java.help.search;
36
abstract class Decompressor
38
private static final int BitsInByte = 8;
39
private static final int NBits = 32;
41
private int _readByte;
42
private int _toRead = 0;
43
private int _path = 0;
45
abstract protected int getNextByte() throws Exception;
47
protected void initReading() {
51
private int countZeroes() throws Exception
53
for (int count = 0;; _readByte = getNextByte(), _toRead = BitsInByte)
55
if ((_readByte & (1 << _toRead)) != 0)
61
// reads 1 bit; returns non-0 for bit "1"
62
private int read() throws Exception
65
return _readByte & (1 << _toRead);
68
_toRead = BitsInByte - 1;
69
return (_readByte = getNextByte()) & 0x80;
73
public int read(int kBits) throws Exception
75
int shift = BitsInByte - _toRead;
77
return ((_readByte<<shift) & 0xFF) >>> (shift + (_toRead-=kBits));
80
int result = _toRead > 0
81
? ((_readByte << shift) & 0xFF) >>> shift
83
for (kBits -= _toRead; kBits >= BitsInByte; kBits -= BitsInByte)
84
result = (result << BitsInByte) | getNextByte();
86
return (result << kBits)
87
| ((_readByte = getNextByte()) >>> (_toRead = BitsInByte - kBits));
96
public void beginIteration() {
100
public boolean readNext(int k, CompressorIterator it) throws Exception
104
it.value(_path | read(k));
108
for (int count = 1;; _readByte = getNextByte(), _toRead = BitsInByte)
109
while (_toRead-- > 0)
110
if ((_readByte & (1 << _toRead)) != 0)
113
_path = ((_path >>> (k + count) << count) | read(count)) << k;
116
it.value(_path | read(k));
126
public void decode(int k, IntegerArray array) throws Exception
130
array.add(path | read(k));
133
int count = countZeroes() + 1;
135
path = ((path >>> (k + count) << count) | read(count)) << k;
136
if (path != saved) // convention for end
137
array.add(path | read(k));
143
public void ascDecode(int k, IntegerArray array) throws Exception
145
for (int path = 0, start = 0;;)
147
array.add(start += path | read(k));
150
int count = countZeroes() + 1;
152
path = ((path >>> (k + count) << count) | read(count)) << k;
153
if (path != saved) // convention for end
154
array.add(start += path | read(k));
160
public int ascendingDecode(int k, int start, int[] array) throws Exception
162
int path = 0, index = 0;
166
array[index++] = (start += path | read(k));
168
for (int cnt = 0;; _readByte = getNextByte(), _toRead = BitsInByte)
169
while (_toRead-- > 0)
170
if ((_readByte & (1 << _toRead)) != 0)
173
int Path = ((path >>> (k + cnt) << cnt) | read(cnt)) << k;
176
array[index++] = (start += (path = Path) | read(k));