/** * Get the maximum byte size needed for compressing data of the given byte * size. * * @param byteSize byte size of the data to compress * @return maximum byte size of the compressed data */ public static int maxCompressedLength(int byteSize) { return impl.maxCompressedLength(byteSize); }
/** * 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); }
/** * Zero-copy decompress using memory addresses. * * @param inputAddr input memory address * @param inputSize input byte size * @param destAddr destination address of the uncompressed data * @return the uncompressed data size * @throws IOException */ public static long rawUncompress(long inputAddr, long inputSize, long destAddr) throws IOException { return impl.rawUncompress(inputAddr, inputSize, destAddr); }
/** * Get the uncompressed byte size of the given compressed input. This * operation takes O(1) time. * * @param input * @return uncompressed byte size of the the given input data * @throws IOException when failed to uncompress the given input. The error code is * {@link SnappyErrorCode#PARSING_ERROR} */ public static int uncompressedLength(byte[] input) throws IOException { return impl.uncompressedLength(input, 0, input.length); }
/** * 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(long inputAddr, long offset, long length) throws IOException { return impl.isValidCompressedBuffer(inputAddr, offset, length); }
/** * Make a call to {@link System#loadLibrary(String)} to load the native library which assumes * that the library is available on the path based on this {@link Bundle}'s {@link Manifest}. */ public void start(BundleContext context) throws Exception { String library = System.mapLibraryName(LIBRARY_NAME); if (library.toLowerCase().endsWith(".dylib")) { // some MacOS JDK7+ vendors map to dylib instead of jnilib library = library.replace(".dylib", ".jnilib"); } System.loadLibrary(library); SnappyLoader.setSnappyApi(new SnappyNative()); }
/** * Copy bytes from source to destination * * @param src pointer to the source array * @param offset byte offset in the source array * @param byteLength the number of bytes to copy * @param dest pointer to the destination array * @param dest_offset byte offset in the destination array * @throws IOException */ public static void arrayCopy(Object src, int offset, int byteLength, Object dest, int dest_offset) throws IOException { impl.arrayCopy(src, offset, byteLength, dest, dest_offset); }
/** * Get the uncompressed byte size of the given compressed input. This operation takes O(1) time. * * @param inputAddr compressed data address * @param len byte length of the input * @return uncompressed byte length of the given input * @throws IOException when failed to uncompress the given input. The error code is * {@link SnappyErrorCode#PARSING_ERROR} */ public static long uncompressedLength(long inputAddr, long len) throws IOException { return impl.uncompressedLength(inputAddr, len); }
/** * 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, int offset, int length) throws IOException { if (input == null) { throw new NullPointerException("input is null"); } return impl.isValidCompressedBuffer(input, offset, length); }
static synchronized SnappyNative loadSnappyApi() { if (snappyApi != null) { return snappyApi; } loadNativeLibrary(); setSnappyApi(new SnappyNative()); return snappyApi; }
/** * Uncompress the content in the input buffer. The uncompressed data is * written to the output buffer. * <p/> * Note that if you pass the wrong data or the range [inputOffset, * inputOffset + inputLength) that cannot be uncompressed, your JVM might * crash due to the access violation exception issued in the native code * written in C++. To avoid this type of crash, use * {@link #isValidCompressedBuffer(byte[], int, int)} first. * * @param input input byte array * @param inputOffset byte offset in the input byte array * @param inputLength byte length of the input data * @param output output buffer, MUST be a primitive type array * @param outputOffset byte offset in the output buffer * @return the byte size of the uncompressed data * @throws IOException when failed to uncompress the input data */ public static int rawUncompress(byte[] input, int inputOffset, int inputLength, Object output, int outputOffset) throws IOException { if (input == null || output == null) { throw new NullPointerException("input or output is null"); } return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset); }
/** * 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; }
@Override public long maxCompressedLength(long inputSize) { return snappyNative.maxCompressedLength((int) inputSize); }
/** * Get the uncompressed byte size of the given compressed input. This * operation takes O(1) time. * * @param input * @param offset * @param length * @return uncompressed byte size of the the given input data * @throws IOException when failed to uncompress the given input. The error code is * {@link SnappyErrorCode#PARSING_ERROR} */ public static int uncompressedLength(byte[] input, int offset, int length) throws IOException { if (input == null) { throw new NullPointerException("input is null"); } return impl.uncompressedLength(input, offset, length); }
/** * Returns true iff the contents of compressed buffer [pos() ... limit()) * 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(ByteBuffer compressed) throws IOException { return impl.isValidCompressedBuffer(compressed, compressed.position(), compressed.remaining()); }
/** * 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; }
/** * 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; }
/** * Get the uncompressed byte size of the given compressed input. This * operation takes O(1) time. * * @param compressed input data [pos() ... limit()) * @return uncompressed byte length of the given input * @throws IOException when failed to uncompress the given input. The error code is * {@link SnappyErrorCode#PARSING_ERROR} * @throws SnappyError when the input is not a direct buffer */ public static int uncompressedLength(ByteBuffer compressed) throws IOException { if (!compressed.isDirect()) { throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer"); } return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining()); }
/** * 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; }
/** * 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; }