private boolean hasSufficientOutputBufferFor(int inputSize) { int maxCompressedSize = Snappy.maxCompressedLength(inputSize); return maxCompressedSize < outputBuffer.length - outputCursor - 4; }
/** * Compress the input data and produce a byte array of the uncompressed data * * @param data input array. The input MUST be an array type * @param byteSize the input byte size * @return compressed data */ public static byte[] rawCompress(Object data, int byteSize) throws IOException { byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)]; int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0); byte[] result = new byte[compressedByteSize]; System.arraycopy(buf, 0, result, 0, compressedByteSize); return result; }
@Override ByteBuffer compress(ByteBuffer in) throws IOException { ByteBuffer out = ByteBuffer.allocate(Snappy.maxCompressedLength(in.remaining())); int size = Snappy.compress(in.array(), in.position(), in.remaining(), out.array(), 0); out.limit(size); return out; }
@Override public ByteBuffer compress(ByteBuffer in) throws IOException { ByteBuffer out = ByteBuffer.allocate(Snappy.maxCompressedLength(in.remaining())+4); int size = Snappy.compress(in.array(), in.position(), in.remaining(), out.array(), 0); crc32.reset(); crc32.update(in.array(), in.position(), in.remaining()); out.putInt(size, (int)crc32.getValue()); out.limit(size+4); return out; }
/** * @param size */ private void allocateBuffersBasedOnSize(int size) { if (input != null) { releaseDirectByteBuffer(input); } if (uncompressedDirect != null) { releaseDirectByteBuffer(uncompressedDirect); } input = ByteBuffer.allocateDirect(size); final int maxCompressedLength = Snappy.maxCompressedLength(size); uncompressedDirect = ByteBuffer.allocateDirect(maxCompressedLength); buffer = new byte[maxCompressedLength]; }
@Override public ByteBuffer compress(ByteBuffer in) throws IOException { int offset = computeOffset(in); ByteBuffer out = ByteBuffer.allocate(Snappy.maxCompressedLength(in.remaining())+4); int size = Snappy.compress(in.array(), offset, in.remaining(), out.array(), 0); crc32.reset(); crc32.update(in.array(), offset, in.remaining()); out.putInt(size, (int)crc32.getValue()); out.limit(size+4); return out; }
directInputBuffer = ByteBuffer.allocateDirect(blockSize); outputBuffer = ByteBuffer.allocateDirect(Snappy .maxCompressedLength(blockSize));
public SnappyOutputStream(OutputStream out, int blockSize, BufferAllocatorFactory bufferAllocatorFactory) { this.out = out; this.blockSize = Math.max(MIN_BLOCK_SIZE, blockSize); int inputSize = blockSize; int outputSize = SnappyCodec.HEADER_SIZE + 4 + Snappy.maxCompressedLength(blockSize); this.inputBufferAllocator = bufferAllocatorFactory.getBufferAllocator(inputSize); this.outputBufferAllocator = bufferAllocatorFactory.getBufferAllocator(outputSize); inputBuffer = inputBufferAllocator.allocate(inputSize); outputBuffer = outputBufferAllocator.allocate(outputSize); }
@Override public void compress(final List<ByteBuf> source, final BsonOutput target) { int uncompressedSize = getUncompressedSize(source); byte[] singleByteArraySource = new byte[uncompressedSize]; copy(source, singleByteArraySource); try { byte[] out = new byte[Snappy.maxCompressedLength(uncompressedSize)]; int compressedSize = Snappy.compress(singleByteArraySource, 0, singleByteArraySource.length, out, 0); target.writeBytes(out, 0, compressedSize); } catch (IOException e) { throw new MongoInternalException("Unexpected IOException", e); } }
private ByteBuf compressHeap(ByteBuf input) throws IOException { int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes()); int inOffset = input.arrayOffset() + input.readerIndex(); byte[] in = input.array(); int len = input.readableBytes(); // Increase reader index. input.readerIndex(input.writerIndex()); // Allocate a heap buffer from the ByteBufAllocator as we may use a PooledByteBufAllocator and // so // can eliminate the overhead of allocate a new byte[]. ByteBuf output = input.alloc().heapBuffer(maxCompressedLength); try { // Calculate the correct offset. int offset = output.arrayOffset() + output.writerIndex(); byte[] out = output.array(); int written = Snappy.compress(in, inOffset, len, out, offset); // Increase the writerIndex with the written bytes. output.writerIndex(output.writerIndex() + written); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
private ByteBuf compressDirect(ByteBuf input) throws IOException { int maxCompressedLength = Snappy.maxCompressedLength(input.readableBytes()); // If the input is direct we will allocate a direct output buffer as well as this will allow us // to use // Snappy.compress(ByteBuffer, ByteBuffer) and so eliminate memory copies. ByteBuf output = input.alloc().directBuffer(maxCompressedLength); try { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); ByteBuffer out = outputNioBuffer(output); int written = Snappy.compress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + written); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
int maxCompressedSize = Snappy.maxCompressedLength(baOut.size()); byte[] compressed = new byte[maxCompressedSize]; // size for worst case int compressedSize = Snappy.compress(baOut.getBuffer(), 0, baOut.size(), compressed, 0);
SnappyRandomReader(BlockRandomInput data, int maxBlockSize) { this.data = data; this.maxBlockSize = maxBlockSize; blockSize = 0; bufPos = 0; uncompressedBuf = new byte[maxBlockSize]; compressedBuf = new byte[Snappy.maxCompressedLength(maxBlockSize)]; }
@Override public int getMaxBytesForCompression(int uncompressedDataSize) { return Snappy.maxCompressedLength(uncompressedDataSize); }
public SnappyReader(InputStream data, int maxBlockSize, long start) { this.data = data; blockSize = 0; bufPos = 0; curBlockStart = start; nextBlockStart = start; uncompressedBuf = new byte[maxBlockSize]; compressedBuf = new byte[Snappy.maxCompressedLength(maxBlockSize)]; }
@Override public int maxCompressedLength(int length) { return org.xerial.snappy.Snappy.maxCompressedLength(length); } }
SnappyOutputStream(int maxBlockSize, OutputStream output, FileDescriptor fileDescriptor) throws IOException { this.fileDescriptor = fileDescriptor; if (maxBlockSize < 10) { throw new IOException("Too small block size - won't be able to fit keylen + valuelen in a single block"); } this.maxBlockSize = maxBlockSize; this.output = output; uncompressedBuffer = new byte[maxBlockSize]; compressedBuffer = new byte[Snappy.maxCompressedLength(maxBlockSize)]; }
@Override public ByteBuffer compress(ByteBuffer in) throws IOException { ByteBuffer out = ByteBuffer.allocate(Snappy.maxCompressedLength(in.remaining())+4); int size = Snappy.compress(in.array(), in.position(), in.remaining(), out.array(), 0); crc32.reset(); crc32.update(in.array(), in.position(), in.remaining()); out.putInt(size, (int)crc32.getValue()); out.limit(size+4); return out; }
@Override public ByteBuf compressOutput(Object proto, RpcMethodInfo rpcMethodInfo) throws IOException { byte[] bytes = rpcMethodInfo.outputEncode(proto); int maxCompressedSize = Snappy.maxCompressedLength(bytes.length); byte[] compressedBytes = new byte[maxCompressedSize]; int compressedLen = Snappy.compress(bytes, 0, bytes.length, compressedBytes, 0); return Unpooled.wrappedBuffer(compressedBytes, 0, compressedLen); }
public Frame compress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); byte[] output = new byte[Snappy.maxCompressedLength(input.length)]; int written = Snappy.compress(input, 0, input.length, output, 0); return frame.with(ChannelBuffers.wrappedBuffer(output, 0, written)); }