/** * 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()); }
/** * 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; }
/** * Load a native library of snappy-java */ private synchronized static void loadNativeLibrary() { if (!isLoaded) { try { nativeLibFile = findNativeLibrary(); if (nativeLibFile != null) { // Load extracted or specified snappyjava native library. System.load(nativeLibFile.getAbsolutePath()); } else { // Load preinstalled snappyjava (in the path -Djava.library.path) System.loadLibrary("snappyjava"); } } catch (Exception e) { e.printStackTrace(); throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, e.getMessage()); } isLoaded = true; } }
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer"); throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer"); throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer"); throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "destination is not a direct buffer");
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, String.format("Failed to write a native library file at %s", extractedLibFile));
throw new SnappyError(SnappyErrorCode.FAILED_TO_LOAD_NATIVE_LIBRARY, errorMessage);