/** * Zero-copy compress using memory addresses. * * @param inputAddr input memory address * @param inputSize input byte size * @param destAddr destination address of the compressed data * @return the compressed data size * @throws IOException */ public static long rawCompress(long inputAddr, long inputSize, long destAddr) throws IOException { return impl.rawCompress(inputAddr, inputSize, destAddr); }
/** * Compress the input buffer [offset,... ,offset+length) contents, then * write the compressed data to the output buffer[offset, ...) * * @param input input array. This MUST be a primitive array type * @param inputOffset byte offset at the output array * @param inputLength byte length of the input data * @param output output array. This MUST be a primitive array type * @param outputOffset byte offset at the output array * @return byte size of the compressed data * @throws IOException */ public static int rawCompress(Object input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws IOException { if (input == null || output == null) { throw new NullPointerException("input or output is null"); } int compressedSize = impl .rawCompress(input, inputOffset, inputLength, output, outputOffset); return compressedSize; }
/** * 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; }
/** * Compress the content in the given input buffer. After the compression, * you can retrieve the compressed data from the output buffer [pos() ... * limit()) (compressed data size = limit() - pos() = remaining()) * * @param uncompressed buffer[pos() ... limit()) containing the input data * @param compressed output of the compressed data. Uses range [pos()..]. * @return byte size of the compressed data. * @throws SnappyError when the input is not a direct buffer */ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed) throws IOException { if (!uncompressed.isDirect()) { throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer"); } if (!compressed.isDirect()) { throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer"); } // input: uncompressed[pos(), limit()) // output: compressed int uPos = uncompressed.position(); int uLen = uncompressed.remaining(); int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed, compressed.position()); // pos limit // [ ......BBBBBBB.........] compressed.limit(compressed.position() + compressedSize); return compressedSize; }
@Override public long rawCompress(long inputAddress, int inputSize, long outputAddress) throws IOException { return snappyNative.rawCompress(inputAddress, inputSize, outputAddress); }