Decodes the given data buffer starting at offset till length.
(byte[] data, int offset, int length)
| 141 | |
| 142 | /** Decodes the given data buffer starting at offset till length. */ |
| 143 | public static byte[] decompress(byte[] data, int offset, int length) throws IOException { |
| 144 | ArrayList<byte[]> output = new ArrayList<>(); |
| 145 | int totalOutputSize = 0; |
| 146 | DecoderJNI.Wrapper decoder = new DecoderJNI.Wrapper(length); |
| 147 | try { |
| 148 | decoder.getInputBuffer().put(data, offset, length); |
| 149 | decoder.push(length); |
| 150 | while (decoder.getStatus() != DecoderJNI.Status.DONE) { |
| 151 | switch (decoder.getStatus()) { |
| 152 | case OK: |
| 153 | decoder.push(0); |
| 154 | break; |
| 155 | |
| 156 | case NEEDS_MORE_OUTPUT: |
| 157 | ByteBuffer buffer = decoder.pull(); |
| 158 | byte[] chunk = new byte[buffer.remaining()]; |
| 159 | buffer.get(chunk); |
| 160 | output.add(chunk); |
| 161 | totalOutputSize += chunk.length; |
| 162 | break; |
| 163 | |
| 164 | case NEEDS_MORE_INPUT: |
| 165 | // Give decoder a chance to process the remaining of the buffered byte. |
| 166 | decoder.push(0); |
| 167 | // If decoder still needs input, this means that stream is truncated. |
| 168 | if (decoder.getStatus() == DecoderJNI.Status.NEEDS_MORE_INPUT) { |
| 169 | throw new IOException("corrupted input"); |
| 170 | } |
| 171 | break; |
| 172 | |
| 173 | default: |
| 174 | throw new IOException("corrupted input"); |
| 175 | } |
| 176 | } |
| 177 | } finally { |
| 178 | decoder.destroy(); |
| 179 | } |
| 180 | if (output.size() == 1) { |
| 181 | return output.get(0); |
| 182 | } |
| 183 | byte[] result = new byte[totalOutputSize]; |
| 184 | int resultOffset = 0; |
| 185 | for (byte[] chunk : output) { |
| 186 | System.arraycopy(chunk, 0, result, resultOffset, chunk.length); |
| 187 | resultOffset += chunk.length; |
| 188 | } |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | /** Decodes the given data buffer. */ |
| 193 | public static byte[] decompress(byte[] data) throws IOException { |