Debian Codegraph @

src/main/java/org/freehep/util/io/DecompressableInputStream.java

raw
1 // Copyright 2001, FreeHEP.
2 package org.freehep.util.io;
3
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.zip.InflaterInputStream;
7
8 /**
9 * Special stream that can be used to read uncompressed first and compressed
10 * from a certain byte.
11 *
12 * IMPORTANT: inherits from InputStream rather than FilterInputStream so that
13 * the correct read(byte[], int, int) method is used.
14 *
15 * @author Mark Donszelmann
16 * @version $Id: DecompressableInputStream.java 8584 2006-08-10 23:06:37Z duns $
17 */
18 public class DecompressableInputStream extends InputStream {
19
20 private boolean decompress;
21
22 private InflaterInputStream iis;
23
24 private InputStream in;
25
26 /**
27 * Creates a Decompressable input stream from given stream.
28 *
29 * @param input stream to read from.
30 */
31 public DecompressableInputStream(InputStream input) {
32 super();
33 in = input;
34 decompress = false;
35 }
36
37 public int read() throws IOException {
38 return (decompress) ? iis.read() : in.read();
39 }
40
41 public long skip(long n) throws IOException {
42 return (decompress) ? iis.skip(n) : in.skip(n);
43 }
44
45 /**
46 * Start reading in compressed mode from the next byte.
47 *
48 * @throws IOException if read fails.
49 */
50 public void startDecompressing() throws IOException {
51 decompress = true;
52 iis = new InflaterInputStream(in);
53 }
54 }