Repository navigation
Expand file tree
/
Copy pathUncompressInputStream.java
More file actions
539 lines (446 loc) · 14.1 KB
/
Copy pathUncompressInputStream.java
File metadata and controls
539 lines (446 loc) · 14.1 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
* BioJava development code
*
* This code may be freely distributed and modified under the
* terms of the GNU Lesser General Public Licence. This should
* be distributed with the code. If you do not have a copy,
* see:
*
* http://www.gnu.org/copyleft/lesser.html
*
* Copyright for this code is held jointly by the individual
* authors. These should be listed in @author doc comments.
*
* For more information on the BioJava project and its aims,
* or to join the biojava-l mailing list, visit the home page
* at:
*
* http://www.biojava.org/
*
*/
/*
* @(#)UncompressInputStream.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschalar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@xxxxxxxxxxxxx
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*/
package org.biojava.nbio.core.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
/**
* This class decompresses an input stream containing data compressed with
* the unix "compress" utility (LZC, a LZW variant). This code is based
* heavily on the <var>unlzw.c</var> code in <var>gzip-1.2.4</var> (written
* by Peter Jannesen) and the original compress code.
*
*
* This version has been modified from the original 0.3-3 version by the
* Unidata Program Center (support@xxxxxxxxxxxxxxxx) to make the constructor
* public and to fix a couple of bugs.
* Also:
* - markSupported() returns false
* - add uncompress() static method
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschalar
* @author Unidata Program Center
* @author Richard Holland - making LZW_MAGIC package-visible.
*
* @version 0.3-5 2008/01/19
* @author Fred Hansen (zweibieren@yahoo.com)
* Fixed available() and the EOF condition for mainloop.
* Also added some comments.
*
* @version 1.0 2018/01/08
* @author Fred Hansen (zweibieren@yahoo.com)
* added uncompress(InputStream,OutputStream)
* and called it from main(String[])
* and uncompress(String, FileOutputStream)
* normalize indentation
* rewrite skip method
* amend logging code in uncompress(String, FileOutputStream)
*/
public class UncompressInputStream extends FilterInputStream {
private final static Logger logger
= LoggerFactory.getLogger(UncompressInputStream.class);
/**
* @param is the input stream to decompress
* @throws IOException if the header is malformed
*/
public UncompressInputStream(InputStream is) throws IOException {
super(is);
parse_header();
}
@Override
public synchronized int read() throws IOException {
byte[] one = new byte[1];
int b = read(one, 0, 1);
if (b == 1)
return (one[0] & 0xff);
else
return -1;
}
// string table stuff
private static final int TBL_CLEAR = 0x100;
private static final int TBL_FIRST = TBL_CLEAR + 1;
private int[] tab_prefix;
private byte[] tab_suffix;
final private int[] zeros = new int[256];
private byte[] stack;
// various state
private boolean block_mode;
private int n_bits;
private int maxbits;
private int maxmaxcode;
private int maxcode;
private int bitmask;
private int oldcode;
private byte finchar;
private int stackp;
private int free_ent;
/* input buffer
The input stream must be considered in chunks
Each chunk is of length eight times the current code length.
Thus the chunk contains eight codes; NOT on byte boundaries.
*/
final private byte[] data = new byte[10000];
private int
bit_pos = 0, // current bitwise location in bitstream
end = 0, // index of next byte to fill in data
got = 0; // number of bytes gotten by most recent read()
private boolean eof = false;
private static final int EXTRA = 64;
@Override
public synchronized int read(byte[] buf, int off, int len)
throws IOException {
if (eof) return -1;
int start = off;
/* Using local copies of various variables speeds things up by as
* much as 30% !
*/
int[] l_tab_prefix = tab_prefix;
byte[] l_tab_suffix = tab_suffix;
byte[] l_stack = stack;
int l_n_bits = n_bits;
int l_maxcode = maxcode;
int l_maxmaxcode = maxmaxcode;
int l_bitmask = bitmask;
int l_oldcode = oldcode;
byte l_finchar = finchar;
int l_stackp = stackp;
int l_free_ent = free_ent;
byte[] l_data = data;
int l_bit_pos = bit_pos;
// empty stack if stuff still left
int s_size = l_stack.length - l_stackp;
if (s_size > 0) {
int num = (s_size >= len) ? len : s_size;
System.arraycopy(l_stack, l_stackp, buf, off, num);
off += num;
len -= num;
l_stackp += num;
}
if (len == 0) {
stackp = l_stackp;
return off - start;
}
// loop, filling local buffer until enough data has been decompressed
main_loop: do {
if (end < EXTRA) fill();
int bit_end = (got > 0)
? (end - end % l_n_bits) << 3 // set to a "chunk" boundary
: (end << 3) - (l_n_bits - 1); // no more data, set to last code
while (l_bit_pos < bit_end) { // handle 1-byte reads correctly
if (len == 0) {
n_bits = l_n_bits;
maxcode = l_maxcode;
maxmaxcode = l_maxmaxcode;
bitmask = l_bitmask;
oldcode = l_oldcode;
finchar = l_finchar;
stackp = l_stackp;
free_ent = l_free_ent;
bit_pos = l_bit_pos;
return off - start;
}
// check for code-width expansion
if (l_free_ent > l_maxcode) {
int n_bytes = l_n_bits << 3;
l_bit_pos = (l_bit_pos - 1) +
n_bytes - (l_bit_pos - 1 + n_bytes) % n_bytes;
l_n_bits++;
l_maxcode = (l_n_bits == maxbits) ? l_maxmaxcode :
(1 << l_n_bits) - 1;
logger.debug("Code-width expanded to ", l_n_bits);
l_bitmask = (1 << l_n_bits) - 1;
l_bit_pos = resetbuf(l_bit_pos);
continue main_loop;
}
// read next code
int pos = l_bit_pos >> 3;
int code = (((l_data[pos] & 0xFF) | ((l_data[pos + 1] & 0xFF) << 8) |
((l_data[pos + 2] & 0xFF) << 16))
>> (l_bit_pos & 0x7)) & l_bitmask;
l_bit_pos += l_n_bits;
// handle first iteration
if (l_oldcode == -1) {
if (code >= 256)
throw new IOException("corrupt input: " + code +
" > 255");
l_finchar = (byte) (l_oldcode = code);
buf[off++] = l_finchar;
len--;
continue;
}
// handle CLEAR code
if (code == TBL_CLEAR && block_mode) {
System.arraycopy(zeros, 0, l_tab_prefix, 0, zeros.length);
l_free_ent = TBL_FIRST - 1;
int n_bytes = l_n_bits << 3;
l_bit_pos = (l_bit_pos - 1) +
n_bytes - (l_bit_pos - 1 + n_bytes) % n_bytes;
l_n_bits = INIT_BITS;
l_maxcode = (1 << l_n_bits) - 1;
l_bitmask = l_maxcode;
logger.debug("Code tables reset");
l_bit_pos = resetbuf(l_bit_pos);
continue main_loop;
}
// setup
int incode = code;
l_stackp = l_stack.length;
// Handle KwK case
if (code >= l_free_ent) {
if (code > l_free_ent)
throw new IOException("corrupt input: code=" + code +
", free_ent=" + l_free_ent);
l_stack[--l_stackp] = l_finchar;
code = l_oldcode;
}
// Generate output characters in reverse order
while (code >= 256) {
l_stack[--l_stackp] = l_tab_suffix[code];
code = l_tab_prefix[code];
}
l_finchar = l_tab_suffix[code];
buf[off++] = l_finchar;
len--;
// And put them out in forward order
s_size = l_stack.length - l_stackp;
int num = (s_size >= len) ? len : s_size;
System.arraycopy(l_stack, l_stackp, buf, off, num);
off += num;
len -= num;
l_stackp += num;
// generate new entry in table
if (l_free_ent < l_maxmaxcode) {
l_tab_prefix[l_free_ent] = l_oldcode;
l_tab_suffix[l_free_ent] = l_finchar;
l_free_ent++;
}
// Remember previous code
l_oldcode = incode;
// if output buffer full, then return
if (len == 0) {
n_bits = l_n_bits;
maxcode = l_maxcode;
bitmask = l_bitmask;
oldcode = l_oldcode;
finchar = l_finchar;
stackp = l_stackp;
free_ent = l_free_ent;
bit_pos = l_bit_pos;
return off - start;
}
}
l_bit_pos = resetbuf(l_bit_pos);
} while
// old code: (got>0) fails if code width expands near EOF
(got > 0 // usually true
|| l_bit_pos < (end << 3) - (l_n_bits - 1)); // last few bytes
n_bits = l_n_bits;
maxcode = l_maxcode;
bitmask = l_bitmask;
oldcode = l_oldcode;
finchar = l_finchar;
stackp = l_stackp;
free_ent = l_free_ent;
bit_pos = l_bit_pos;
eof = true;
return off - start;
}
/**
* Moves the unread data in the buffer to the beginning and resets
* the pointers.
*/
private int resetbuf(int bit_pos) {
int pos = bit_pos >> 3;
System.arraycopy(data, pos, data, 0, end - pos);
end -= pos;
return 0;
}
private void fill() throws IOException {
got = in.read(data, end, data.length - 1 - end);
if (got > 0) end += got;
}
@Override
public synchronized long skip(long num) throws IOException {
return Math.max(0, read(new byte[(int) num]));
}
@Override
public synchronized int available() throws IOException {
if (eof) return 0;
// the old code was: return in.available();
// it fails because this.read() can return bytes
// even after in.available() is zero
// -- zweibieren
int avail = in.available();
return (avail == 0) ? 1 : avail;
}
static final int LZW_MAGIC = 0x1f9d;
private static final int MAX_BITS = 16;
private static final int INIT_BITS = 9;
private static final int HDR_MAXBITS = 0x1f;
private static final int HDR_EXTENDED = 0x20;
private static final int HDR_FREE = 0x40;
private static final int HDR_BLOCK_MODE = 0x80;
private void parse_header() throws IOException {
// read in and check magic number
int t = in.read();
if (t < 0) throw new EOFException("Failed to read magic number");
int magic = (t & 0xff) << 8;
t = in.read();
if (t < 0) throw new EOFException("Failed to read magic number");
magic += t & 0xff;
if (magic != LZW_MAGIC)
throw new IOException("Input not in compress format (read " +
"magic number 0x" +
Integer.toHexString(magic) + ")");
// read in header byte
int header = in.read();
if (header < 0) throw new EOFException("Failed to read header");
block_mode = (header & HDR_BLOCK_MODE) > 0;
maxbits = header & HDR_MAXBITS;
if (maxbits > MAX_BITS)
throw new IOException("Stream compressed with " + maxbits +
" bits, but can only handle " + MAX_BITS +
" bits");
if ((header & HDR_EXTENDED) > 0)
throw new IOException("Header extension bit set");
if ((header & HDR_FREE) > 0)
throw new IOException("Header bit 6 set");
logger.debug("block mode: {}", block_mode);
logger.debug("max bits: {}", maxbits);
// initialize stuff
maxmaxcode = 1 << maxbits;
n_bits = INIT_BITS;
maxcode = (1 << n_bits) - 1;
bitmask = maxcode;
oldcode = -1;
finchar = 0;
free_ent = block_mode ? TBL_FIRST : 256;
tab_prefix = new int[1 << maxbits];
tab_suffix = new byte[1 << maxbits];
stack = new byte[1 << maxbits];
stackp = stack.length;
for (int idx = 255; idx >= 0; idx--)
tab_suffix[idx] = (byte) idx;
}
/**
* This stream does not support mark/reset on the stream.
*
* @return false
*/
@Override
public boolean markSupported() {
return false;
}
/**
* Read a named file and uncompress it.
* @param fileInName Name of compressed file.
* @param out A destination for the result. It is closed after data is sent.
* @return number of bytes sent to the output stream,
* @throws IOException for any error
*/
public static long uncompress(String fileInName, FileOutputStream out)
throws IOException {
long start = System.currentTimeMillis();
long total;
try (InputStream fin = new FileInputStream(fileInName)) {
total = uncompress(fin, out);
}
out.close();
if (debugTiming) {
long end = System.currentTimeMillis();
logger.info("Decompressed {} bytes", total);
UncompressInputStream.logger.info("Time: {} seconds", (end - start) / 1000);
}
return total;
}
/**
* Read an input stream and uncompress it to an output stream.
* @param in the incoming InputStream. It is NOT closed.
* @param out the destination OutputStream. It is NOT closed.
* @return number of bytes sent to the output stream
* @throws IOException for any error
*/
public static long uncompress(InputStream in, OutputStream out)
throws IOException {
UncompressInputStream ucis = new UncompressInputStream(in);
long total = 0;
byte[] buffer = new byte[100000];
while (true) {
int bytesRead = ucis.read(buffer);
if (bytesRead == -1) break;
out.write(buffer, 0, bytesRead);
total += bytesRead;
}
return total;
}
private static final boolean debugTiming = false;
/**
* Reads a file, uncompresses it, and sends the result to stdout.
* Also writes trivial statistics to stderr.
* @param args An array with one String element, the name of the file to read.
* @throws IOException for any failure
*/
public static void main(String[] args) throws Exception {
if (args.length != 1) {
logger.info("Usage: UncompressInputStream <file>");
System.exit(1);
}
long beg = System.currentTimeMillis();
long tot;
try (InputStream in = new FileInputStream(args[0])) {
tot = uncompress(in, System.out);
}
long end = System.currentTimeMillis();
logger.info("Decompressed {} bytes", tot);
logger.info("Time: {} seconds", (end - beg) / 1000);
}
}