1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.compress.compressors.deflate64;
18
19 import java.io.Closeable;
20 import java.io.IOException;
21 import java.io.InputStream;
22
23 import org.apache.commons.compress.compressors.CompressorInputStream;
24 import org.apache.commons.compress.utils.InputStreamStatistics;
25
26
27
28
29
30
31
32 public class Deflate64CompressorInputStream extends CompressorInputStream implements InputStreamStatistics {
33 private InputStream originalStream;
34 private HuffmanDecoder decoder;
35 private long compressedBytesRead;
36 private final byte[] oneByte = new byte[1];
37
38 Deflate64CompressorInputStream(final HuffmanDecoder decoder) {
39 this.decoder = decoder;
40 }
41
42
43
44
45
46
47 public Deflate64CompressorInputStream(final InputStream in) {
48 this(new HuffmanDecoder(in));
49 originalStream = in;
50 }
51
52 @Override
53 public int available() throws IOException {
54 return decoder != null ? decoder.available() : 0;
55 }
56
57 @Override
58 public void close() throws IOException {
59 try {
60 closeDecoder();
61 } finally {
62 if (originalStream != null) {
63 originalStream.close();
64 originalStream = null;
65 }
66 }
67 }
68
69 private void closeDecoder() {
70 final Closeable c = decoder;
71 org.apache.commons.io.IOUtils.closeQuietly(c);
72 decoder = null;
73 }
74
75
76
77
78 @Override
79 public long getCompressedCount() {
80 return compressedBytesRead;
81 }
82
83
84
85
86 @Override
87 public int read() throws IOException {
88 while (true) {
89 final int r = read(oneByte);
90 switch (r) {
91 case 1:
92 return oneByte[0] & 0xFF;
93 case -1:
94 return -1;
95 case 0:
96 continue;
97 default:
98 throw new IllegalStateException("Invalid return value from read: " + r);
99 }
100 }
101 }
102
103
104
105
106 @Override
107 public int read(final byte[] b, final int off, final int len) throws IOException {
108 if (len == 0) {
109 return 0;
110 }
111 int read = -1;
112 if (decoder != null) {
113 try {
114 read = decoder.decode(b, off, len);
115 } catch (final RuntimeException ex) {
116 throw new IOException("Invalid Deflate64 input", ex);
117 }
118 compressedBytesRead = decoder.getBytesRead();
119 count(read);
120 if (read == -1) {
121 closeDecoder();
122 }
123 }
124 return read;
125 }
126 }