Encodes the given data buffer.
(byte[] data, int offset, int length, Parameters params)
| 209 | |
| 210 | /** Encodes the given data buffer. */ |
| 211 | public static byte[] compress(byte[] data, int offset, int length, Parameters params) |
| 212 | throws IOException { |
| 213 | if (length == 0) { |
| 214 | byte[] empty = new byte[1]; |
| 215 | empty[0] = 6; |
| 216 | return empty; |
| 217 | } |
| 218 | /* data.length > 0 */ |
| 219 | ArrayList<byte[]> output = new ArrayList<>(); |
| 220 | int totalOutputSize = 0; |
| 221 | EncoderJNI.Wrapper encoder = |
| 222 | new EncoderJNI.Wrapper(length, params.quality, params.lgwin, params.mode); |
| 223 | try { |
| 224 | encoder.getInputBuffer().put(data, offset, length); |
| 225 | encoder.push(EncoderJNI.Operation.FINISH, length); |
| 226 | while (true) { |
| 227 | if (!encoder.isSuccess()) { |
| 228 | throw new IOException("encoding failed"); |
| 229 | } else if (encoder.hasMoreOutput()) { |
| 230 | ByteBuffer buffer = encoder.pull(); |
| 231 | byte[] chunk = new byte[buffer.remaining()]; |
| 232 | buffer.get(chunk); |
| 233 | output.add(chunk); |
| 234 | totalOutputSize += chunk.length; |
| 235 | } else if (!encoder.isFinished()) { |
| 236 | encoder.push(EncoderJNI.Operation.FINISH, 0); |
| 237 | } else { |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | } finally { |
| 242 | encoder.destroy(); |
| 243 | } |
| 244 | if (output.size() == 1) { |
| 245 | return output.get(0); |
| 246 | } |
| 247 | byte[] result = new byte[totalOutputSize]; |
| 248 | int resultOffset = 0; |
| 249 | for (byte[] chunk : output) { |
| 250 | System.arraycopy(chunk, 0, result, resultOffset, chunk.length); |
| 251 | resultOffset += chunk.length; |
| 252 | } |
| 253 | return result; |
| 254 | } |
| 255 | |
| 256 | /** Encodes the given data buffer. */ |
| 257 | public static byte[] compress(byte[] data, Parameters params) throws IOException { |