| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void decompress(ByteBuffer in, ByteBuffer out) throws IOException { |
| 156 | |
| 157 | if(in.isDirect() && out.isDirect()) { |
| 158 | directDecompress(in, out); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | Inflater inflater = new Inflater(true); |
| 163 | try { |
| 164 | inflater.setInput(in.array(), in.arrayOffset() + in.position(), |
| 165 | in.remaining()); |
| 166 | while (!(inflater.finished() || inflater.needsDictionary() || |
| 167 | inflater.needsInput())) { |
| 168 | try { |
| 169 | int count = inflater.inflate(out.array(), |
| 170 | out.arrayOffset() + out.position(), |
| 171 | out.remaining()); |
| 172 | out.position(count + out.position()); |
| 173 | |
| 174 | if (!inflater.finished() && !inflater.needsDictionary() && !inflater.needsInput() && |
| 175 | count == 0) { |
| 176 | if (out.remaining() == 0) { |
| 177 | throw new IOException("Decompress output buffer too small. in = " + in + |
| 178 | ", out = " + out); |
| 179 | } else { |
| 180 | throw new IOException("Decompress error. in = " + in + |
| 181 | ", out = " + out); |
| 182 | } |
| 183 | } |
| 184 | } catch (DataFormatException dfe) { |
| 185 | throw new IOException("Bad compression data", dfe); |
| 186 | } |
| 187 | } |
| 188 | out.flip(); |
| 189 | } finally { |
| 190 | inflater.end(); |
| 191 | } |
| 192 | in.position(in.limit()); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean isAvailable() { |