1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | |
---|
19 | package org.apache.hadoop.io.compress; |
---|
20 | |
---|
21 | import java.io.EOFException; |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | |
---|
25 | import org.apache.hadoop.io.compress.Decompressor; |
---|
26 | |
---|
27 | public class DecompressorStream extends CompressionInputStream { |
---|
28 | protected Decompressor decompressor = null; |
---|
29 | protected byte[] buffer; |
---|
30 | protected boolean eof = false; |
---|
31 | protected boolean closed = false; |
---|
32 | |
---|
33 | public DecompressorStream(InputStream in, Decompressor decompressor, int bufferSize) { |
---|
34 | super(in); |
---|
35 | |
---|
36 | if (in == null || decompressor == null) { |
---|
37 | throw new NullPointerException(); |
---|
38 | } else if (bufferSize <= 0) { |
---|
39 | throw new IllegalArgumentException("Illegal bufferSize"); |
---|
40 | } |
---|
41 | |
---|
42 | this.decompressor = decompressor; |
---|
43 | buffer = new byte[bufferSize]; |
---|
44 | } |
---|
45 | |
---|
46 | public DecompressorStream(InputStream in, Decompressor decompressor) { |
---|
47 | this(in, decompressor, 512); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Allow derived classes to directly set the underlying stream. |
---|
52 | * |
---|
53 | * @param in Underlying input stream. |
---|
54 | */ |
---|
55 | protected DecompressorStream(InputStream in) { |
---|
56 | super(in); |
---|
57 | } |
---|
58 | |
---|
59 | private byte[] oneByte = new byte[1]; |
---|
60 | public int read() throws IOException { |
---|
61 | checkStream(); |
---|
62 | return (read(oneByte, 0, oneByte.length) == -1) ? -1 : (oneByte[0] & 0xff); |
---|
63 | } |
---|
64 | |
---|
65 | public int read(byte[] b, int off, int len) throws IOException { |
---|
66 | checkStream(); |
---|
67 | |
---|
68 | if ((off | len | (off + len) | (b.length - (off + len))) < 0) { |
---|
69 | throw new IndexOutOfBoundsException(); |
---|
70 | } else if (len == 0) { |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | return decompress(b, off, len); |
---|
75 | } |
---|
76 | |
---|
77 | protected int decompress(byte[] b, int off, int len) throws IOException { |
---|
78 | int n = 0; |
---|
79 | |
---|
80 | while ((n = decompressor.decompress(b, off, len)) == 0) { |
---|
81 | if (decompressor.finished() || decompressor.needsDictionary()) { |
---|
82 | eof = true; |
---|
83 | return -1; |
---|
84 | } |
---|
85 | if (decompressor.needsInput()) { |
---|
86 | getCompressedData(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | return n; |
---|
91 | } |
---|
92 | |
---|
93 | protected void getCompressedData() throws IOException { |
---|
94 | checkStream(); |
---|
95 | |
---|
96 | int n = in.read(buffer, 0, buffer.length); |
---|
97 | if (n == -1) { |
---|
98 | throw new EOFException("Unexpected end of input stream"); |
---|
99 | } |
---|
100 | |
---|
101 | decompressor.setInput(buffer, 0, n); |
---|
102 | } |
---|
103 | |
---|
104 | protected void checkStream() throws IOException { |
---|
105 | if (closed) { |
---|
106 | throw new IOException("Stream closed"); |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | public void resetState() throws IOException { |
---|
111 | decompressor.reset(); |
---|
112 | } |
---|
113 | |
---|
114 | private byte[] skipBytes = new byte[512]; |
---|
115 | public long skip(long n) throws IOException { |
---|
116 | // Sanity checks |
---|
117 | if (n < 0) { |
---|
118 | throw new IllegalArgumentException("negative skip length"); |
---|
119 | } |
---|
120 | checkStream(); |
---|
121 | |
---|
122 | // Read 'n' bytes |
---|
123 | int skipped = 0; |
---|
124 | while (skipped < n) { |
---|
125 | int len = Math.min(((int)n - skipped), skipBytes.length); |
---|
126 | len = read(skipBytes, 0, len); |
---|
127 | if (len == -1) { |
---|
128 | eof = true; |
---|
129 | break; |
---|
130 | } |
---|
131 | skipped += len; |
---|
132 | } |
---|
133 | return skipped; |
---|
134 | } |
---|
135 | |
---|
136 | public int available() throws IOException { |
---|
137 | checkStream(); |
---|
138 | return (eof) ? 0 : 1; |
---|
139 | } |
---|
140 | |
---|
141 | public void close() throws IOException { |
---|
142 | if (!closed) { |
---|
143 | in.close(); |
---|
144 | closed = true; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | public boolean markSupported() { |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | public synchronized void mark(int readlimit) { |
---|
153 | } |
---|
154 | |
---|
155 | public synchronized void reset() throws IOException { |
---|
156 | throw new IOException("mark/reset not supported"); |
---|
157 | } |
---|
158 | |
---|
159 | } |
---|