/** * Returns true iff the contents of compressed buffer [offset, * offset+length) can be uncompressed successfully. Does not return the * uncompressed data. Takes time proportional to the input length, but is * usually at least a factor of four faster than actual decompression. */ public static boolean isValidCompressedBuffer(byte[] input) throws IOException { return isValidCompressedBuffer(input, 0, input.length); }
@Override public DataPoint getDataPoint(long timestamp, KDataInput buffer) throws IOException { int buffSz = buffer.readUnsignedShort(); byte[] byteBuffer = new byte[buffSz]; buffer.readFully(byteBuffer, 0, buffSz); String result; if (Snappy.isValidCompressedBuffer(byteBuffer, 0, buffSz)) { byte[] uncompressedArray = new byte[Snappy.uncompressedLength(byteBuffer, 0, buffSz)]; int incompressedLength = Snappy.uncompress(byteBuffer, 0, buffSz, uncompressedArray, 0); result = new String(uncompressedArray, 0, incompressedLength, UTF8); } else { result = new String(byteBuffer, UTF8); } SnappyStringDataPoint ret = new SnappyStringDataPoint(timestamp, result); return ret; }
public static byte[] decompressWithSnappy(byte[] bytes) { try { if (!Snappy.isValidCompressedBuffer(bytes)) { throw new IllegalArgumentException("Cannot decompress these bytes using Snappy"); } return Snappy.uncompress(bytes); } catch (IOException e) { throw Throwables.throwUncheckedException(e); } } }
if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed");
private ByteBuf decompressDirect(ByteBuf input) throws IOException { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in)); try { ByteBuffer out = outputNioBuffer(output); int size = Snappy.uncompress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + size); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
public static byte[] decompressWithSnappy(byte[] bytes) { try { if (!Snappy.isValidCompressedBuffer(bytes)) { throw new IllegalArgumentException("Cannot decompress these bytes using Snappy"); } return Snappy.uncompress(bytes); } catch (IOException e) { throw Throwables.throwUncheckedException(e); } } }
if (!Snappy.isValidCompressedBuffer(compressedBuffer)) throw new IOException("Invalid compressed data"); r = Snappy.uncompress(compressedBuffer, decompressedBuffer);
private Object decode(ByteBuffer bytes) throws SQLException { Object obj = null; try { byte[] bytesArr = new byte[bytes.remaining()]; bytes.get(bytesArr); byte[] rawSer = bytesArr; if (bytesArr.length >= minCompressionSize) { rawSer = Snappy.uncompress(bytesArr); } else if (Snappy.isValidCompressedBuffer(bytesArr)) { rawSer = Snappy.uncompress(bytesArr); } obj = KryoJavaPoolSerialization.getInstance().chill .fromBytes(rawSer); } catch (Exception ex) { throw new SQLException("Serialization error.", ex); } return obj; }
private ByteBuf decompressHeap(ByteBuf input) throws IOException { // Not a direct buffer so use byte arrays... int inOffset = input.arrayOffset() + input.readerIndex(); byte[] in = input.array(); int len = input.readableBytes(); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in, inOffset, len)); try { // Calculate the correct offset. int offset = output.arrayOffset() + output.writerIndex(); byte[] out = output.array(); int written = Snappy.uncompress(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 decompressHeap(ByteBuf input) throws IOException { // Not a direct buffer so use byte arrays... int inOffset = input.arrayOffset() + input.readerIndex(); byte[] in = input.array(); int len = input.readableBytes(); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in, inOffset, len)); try { // Calculate the correct offset. int offset = output.arrayOffset() + output.writerIndex(); byte[] out = output.array(); int written = Snappy.uncompress(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 decompressHeap(ByteBuf input) throws IOException { // Not a direct buffer so use byte arrays... int inOffset = input.arrayOffset() + input.readerIndex(); byte[] in = input.array(); int len = input.readableBytes(); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in, inOffset, len)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in, inOffset, len)); try { // Calculate the correct offset. int offset = output.arrayOffset() + output.writerIndex(); byte[] out = output.array(); int written = Snappy.uncompress(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 decompressDirect(ByteBuf input) throws IOException { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in)); try { ByteBuffer out = outputNioBuffer(output); int size = Snappy.uncompress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + size); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
private ByteBuf decompressDirect(ByteBuf input) throws IOException { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in)); try { ByteBuffer out = outputNioBuffer(output); int size = Snappy.uncompress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + size); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
private ByteBuf decompressDirect(ByteBuf input) throws IOException { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); if (!Snappy.isValidCompressedBuffer(in)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); // 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(Snappy.uncompressedLength(in)); try { ByteBuffer out = outputNioBuffer(output); int size = Snappy.uncompress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + size); } catch (IOException e) { // release output buffer so we not leak and rethrow exception. output.release(); throw e; } return output; }
public Frame decompress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) throw new ProtocolException("Provided frame does not appear to be Snappy compressed"); ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.uncompressedLength(input)); try { int size = Snappy.uncompress(input, 0, input.length, output.array(), output.arrayOffset()); output.writerIndex(size); } catch (final Throwable e) { output.release(); throw e; } finally { //release the old frame frame.release(); } return frame.with(output); } }
public Frame decompress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) throw new ProtocolException("Provided frame does not appear to be Snappy compressed"); ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.uncompressedLength(input)); try { int size = Snappy.uncompress(input, 0, input.length, output.array(), output.arrayOffset()); output.writerIndex(size); } catch (final Throwable e) { output.release(); throw e; } finally { //release the old frame frame.release(); } return frame.with(output); } }
public Frame decompress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) throw new ProtocolException("Provided frame does not appear to be Snappy compressed"); ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.uncompressedLength(input)); try { int size = Snappy.uncompress(input, 0, input.length, output.array(), output.arrayOffset()); output.writerIndex(size); } catch (final Throwable e) { output.release(); throw e; } finally { //release the old frame frame.release(); } return frame.with(output); } }
public Frame decompress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) throw new DriverInternalError("Provided frame does not appear to be Snappy compressed"); byte[] output = new byte[Snappy.uncompressedLength(input)]; int size = Snappy.uncompress(input, 0, input.length, output, 0); return frame.with(ChannelBuffers.wrappedBuffer(output, 0, size)); } }
@Override protected ByteBuf decompressDirect(ByteBuf input) { ByteBuffer in = inputNioBuffer(input); // Increase reader index. input.readerIndex(input.writerIndex()); ByteBuf output = null; try { if (!Snappy.isValidCompressedBuffer(in)) { throw new IllegalArgumentException( "Provided frame does not appear to be Snappy compressed"); } // 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. output = input.alloc().directBuffer(Snappy.uncompressedLength(in)); ByteBuffer out = outputNioBuffer(output); int size = Snappy.uncompress(in, out); // Set the writer index so the amount of written bytes is reflected output.writerIndex(output.writerIndex() + size); return output; } catch (IOException e) { // release output buffer so we not leak and rethrow exception. if (output != null) { output.release(); } throw new RuntimeException(e); } }
public Frame decompress(Frame frame) throws IOException { byte[] input = CBUtil.readRawBytes(frame.body); if (!Snappy.isValidCompressedBuffer(input, 0, input.length)) throw new ProtocolException("Provided frame does not appear to be Snappy compressed"); ByteBuf output = CBUtil.allocator.heapBuffer(Snappy.uncompressedLength(input)); try { int size = Snappy.uncompress(input, 0, input.length, output.array(), output.arrayOffset()); output.writerIndex(size); } catch (final Throwable e) { output.release(); throw e; } finally { //release the old frame frame.release(); } return frame.with(output); } }