/** * High-level API for uncompressing the input byte array. * * @param input * @return the uncompressed byte array * @throws IOException */ public static byte[] uncompress(byte[] input) throws IOException { byte[] result = new byte[Snappy.uncompressedLength(input)]; Snappy.uncompress(input, 0, input.length, result, 0); return result; }
/** * Uncompress the input as a double array * * @param input * @return the uncompressed data * @throws IOException */ public static double[] uncompressDoubleArray(byte[] input) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, 0, input.length); double[] result = new double[uncompressedLength / 8]; impl.rawUncompress(input, 0, input.length, result, 0); return result; }
/** * Uncompress the input[offset, .., offset+length) as a char array * * @param input * @param offset * @param length * @return the uncompressed data * @throws IOException */ public static char[] uncompressCharArray(byte[] input, int offset, int length) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, offset, length); char[] result = new char[uncompressedLength / 2]; impl.rawUncompress(input, offset, length, result, 0); return result; }
/** * Uncompress the input[offset, offset+length) as a short array * * @param input * @param offset * @param length * @return the uncompressed data * @throws IOException */ public static short[] uncompressShortArray(byte[] input, int offset, int length) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, offset, length); short[] result = new short[uncompressedLength / 2]; impl.rawUncompress(input, offset, length, result, 0); return result; }
/** * Uncompress the input[offset, offset+length) as a float array * * @param input * @param offset * @param length * @return the uncompressed data * @throws IOException */ public static float[] uncompressFloatArray(byte[] input, int offset, int length) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, offset, length); float[] result = new float[uncompressedLength / 4]; impl.rawUncompress(input, offset, length, result, 0); return result; }
/** * Uncompress the input[offset, offset+length) as an int array * * @param input * @param offset * @param length * @return the uncompressed data * @throws IOException */ public static int[] uncompressIntArray(byte[] input, int offset, int length) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, offset, length); int[] result = new int[uncompressedLength / 4]; impl.rawUncompress(input, offset, length, result, 0); return result; }
/** * Uncompress the input[offset, offset+length) as a long array * * @param input * @param offset * @param length * @return the uncompressed data * @throws IOException */ public static long[] uncompressLongArray(byte[] input, int offset, int length) throws IOException { int uncompressedLength = Snappy.uncompressedLength(input, offset, length); long[] result = new long[uncompressedLength / 8]; impl.rawUncompress(input, offset, length, result, 0); return result; }
/** * Uncompress the input[offset, offset+length) as a String of the given * encoding * * @param input * @param offset * @param length * @param encoding * @return the uncompressed data * @throws IOException */ public static String uncompressString(byte[] input, int offset, int length, Charset encoding) throws IOException, UnsupportedEncodingException { byte[] uncompressed = new byte[uncompressedLength(input, offset, length)]; uncompress(input, offset, length, uncompressed, 0); return new String(uncompressed, encoding); }
/** * Uncompress the input[offset, offset+length) as a String of the given * encoding * * @param input * @param offset * @param length * @param encoding * @return the uncompressed data * @throws IOException */ public static String uncompressString(byte[] input, int offset, int length, String encoding) throws IOException, UnsupportedEncodingException { byte[] uncompressed = new byte[uncompressedLength(input, offset, length)]; uncompress(input, offset, length, uncompressed, 0); return new String(uncompressed, encoding); }
@Override ByteBuffer decompress(ByteBuffer in) throws IOException { ByteBuffer out = ByteBuffer.allocate (Snappy.uncompressedLength(in.array(),in.position(),in.remaining())); int size = Snappy.uncompress(in.array(),in.position(),in.remaining(), out.array(), 0); out.limit(size); return out; }
protected void readFully(byte[] fragment, int fragmentLength) throws IOException { if (fragmentLength == 0) { finishedReading = true; return; } // read the entire input data to the buffer compressed = new byte[Math.max(8 * 1024, fragmentLength)]; // 8K System.arraycopy(fragment, 0, compressed, 0, fragmentLength); int cursor = fragmentLength; for (int readBytes = 0; (readBytes = in.read(compressed, cursor, compressed.length - cursor)) != -1; ) { cursor += readBytes; if (cursor >= compressed.length) { byte[] newBuf = new byte[(compressed.length * 2)]; System.arraycopy(compressed, 0, newBuf, 0, compressed.length); compressed = newBuf; } } finishedReading = true; // Uncompress int uncompressedLength = Snappy.uncompressedLength(compressed, 0, cursor); uncompressed = new byte[uncompressedLength]; Snappy.uncompress(compressed, 0, cursor, uncompressed, 0); this.uncompressedCursor = 0; this.uncompressedLimit = uncompressedLength; }
@Override public ByteBuffer decompress(ByteBuffer in) throws IOException { ByteBuffer out = ByteBuffer.allocate (Snappy.uncompressedLength(in.array(),in.position(),in.remaining()-4)); int size = Snappy.uncompress(in.array(),in.position(),in.remaining()-4, out.array(), 0); out.limit(size); crc32.reset(); crc32.update(out.array(), 0, size); if (in.getInt(in.limit()-4) != (int)crc32.getValue()) throw new IOException("Checksum failure"); return out; }
@Override public ByteBuffer decompress(ByteBuffer in) throws IOException { int offset = computeOffset(in); ByteBuffer out = ByteBuffer.allocate (Snappy.uncompressedLength(in.array(), offset, in.remaining()-4)); int size = Snappy.uncompress(in.array(), offset, in.remaining()-4, out.array(), 0); out.limit(size); crc32.reset(); crc32.update(out.array(), 0, size); if (in.getInt(in.limit()-4) != (int)crc32.getValue()) throw new IOException("Checksum failure"); return out; }
@Override protected void decode(ChannelHandlerContext ctx, FrameCodec.Frame msg, List<Object> out) throws Exception { byte[] in = new byte[msg.size]; msg.payload.read(in); long uncompressedLength = Snappy.uncompressedLength(in) & 0xFFFFFFFFL; if (uncompressedLength > MAX_SIZE) { logger.info("{}: uncompressed frame size exceeds the limit ({} bytes), drop the peer", channel, uncompressedLength); channel.disconnect(ReasonCode.BAD_PROTOCOL); return; } byte[] uncompressed = new byte[(int) uncompressedLength]; try { Snappy.rawUncompress(in, 0, in.length, uncompressed, 0); } catch (IOException e) { String detailMessage = e.getMessage(); // 5 - error code for framed snappy if (detailMessage.startsWith("FAILED_TO_UNCOMPRESS") && detailMessage.contains("5")) { logger.info("{}: Snappy frames are not allowed in DEVp2p protocol, drop the peer", channel); channel.disconnect(ReasonCode.BAD_PROTOCOL); return; } else { throw e; } } out.add(new FrameCodec.Frame((int) msg.type, uncompressed)); }
final int uncompressedLength = Snappy.uncompressedLength(input);
@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; }
throw new IOException("failed to read chunk"); int uncompressedLength = Snappy.uncompressedLength(compressed, 0, chunkSize); if (uncompressed == null || uncompressedLength > uncompressed.length) { uncompressed = new byte[uncompressedLength];
ByteBuf output = input.alloc().heapBuffer(Snappy.uncompressedLength(in, inOffset, len)); try {
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; }
@Override public Closeable newCache(ImmutableBytesWritable cachePtr, MemoryChunk chunk) throws SQLException { try { int size = Snappy.uncompressedLength(cachePtr.get()); byte[] uncompressed = new byte[size]; Snappy.uncompress(cachePtr.get(), 0, cachePtr.getLength(), uncompressed, 0); return new HashCacheImpl(uncompressed, chunk); } catch (IOException e) { throw ServerUtil.parseServerException(e); } }