@Override ByteBuffer compress(ByteBuffer data) throws IOException { if (compressionBuffer == null) { compressionBuffer = new NonCopyingByteArrayOutputStream( data.remaining()); } if (deflater == null) { deflater = new Deflater(compressionLevel, nowrap); } // Pass output through deflate DeflaterOutputStream deflaterStream = new DeflaterOutputStream(compressionBuffer, deflater); deflaterStream.write(data.array(), data.position() + data.arrayOffset(), data.limit() + data.arrayOffset()); deflaterStream.finish(); ByteBuffer result = compressionBuffer.getByteArrayAsByteBuffer(); deflater.reset(); compressionBuffer.reset(); return result; }
@Override ByteBuffer decompress(ByteBuffer data) throws IOException { if (compressionBuffer == null) { compressionBuffer = new NonCopyingByteArrayOutputStream( data.remaining()); } if (inflater == null) { inflater = new Inflater(nowrap); } InputStream uncompressed = new InflaterInputStream( new ByteArrayInputStream(data.array(), data.position() + data.arrayOffset(), data.remaining()), inflater); int read; byte[] buff = new byte[2048]; try { while (true) { read = uncompressed.read(buff); if (read < 0) break; compressionBuffer.write(buff, 0, read); } } catch (EOFException e) { // sometimes InflaterInputStream.read // throws this instead of returning -1 } ByteBuffer result = compressionBuffer.getByteArrayAsByteBuffer(); inflater.reset(); compressionBuffer.reset(); return result; }
private void writeBlock() throws IOException { if (blockCount > 0) { vout.writeLong(blockCount); ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); ByteBuffer block = codec.compress(uncompressed); vout.writeLong(block.remaining()); vout.writeFixed(block.array(), block.position() + block.arrayOffset(), block.remaining()); buffer.reset(); blockCount = 0; vout.writeFixed(sync); } }
private void writeBlock() throws IOException { if (blockCount > 0) { bufOut.flush(); ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); DataBlock block = new DataBlock(uncompressed, blockCount); block.setFlushOnWrite(flushOnEveryBlock); block.compressUsing(codec); block.writeBlockTo(vout, sync); buffer.reset(); blockCount = 0; } }
private void writeBlock() throws IOException { if (blockCount > 0) { bufOut.flush(); ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); DataBlock block = new DataBlock(uncompressed, blockCount); block.compressUsing(codec); block.writeBlockTo(vout, sync); buffer.reset(); blockCount = 0; } }
private void writeBlock() throws IOException { if (blockCount > 0) { ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); DataBlock block = new DataBlock(uncompressed, blockCount); block.compressUsing(codec); block.writeBlockTo(vout, sync); buffer.reset(); blockCount = 0; } }
private void writeBlock() throws IOException { if (blockCount > 0) { try { bufOut.flush(); ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); DataBlock block = new DataBlock(uncompressed, blockCount); block.setFlushOnWrite(flushOnEveryBlock); block.compressUsing(codec); block.writeBlockTo(vout, sync); } finally { buffer.reset(); blockCount = 0; } } }
private void writeBlock() throws IOException { if (blockCount > 0) { bufOut.flush(); ByteBuffer uncompressed = buffer.getByteArrayAsByteBuffer(); DataBlock block = new DataBlock(uncompressed, blockCount); block.setFlushOnWrite(flushOnEveryBlock); block.compressUsing(codec); block.writeBlockTo(vout, sync); buffer.reset(); blockCount = 0; } }