/** Alternate version that accepts pre-allocated output buffer. */ public static int appendEncoded( ChunkEncoder enc, byte[] input, int inputPtr, int inputLength, byte[] outputBuffer, int outputPtr) { int left = inputLength; int chunkLen = Math.min(LZFChunk.MAX_CHUNK_LEN, left); outputPtr = enc.appendEncodedChunk(input, inputPtr, chunkLen, outputBuffer, outputPtr); left -= chunkLen; // shortcut: if it all fit in, no need to coalesce: if (left < 1) { return outputPtr; } // otherwise need to keep on encoding... inputPtr += chunkLen; do { chunkLen = Math.min(left, LZFChunk.MAX_CHUNK_LEN); outputPtr = enc.appendEncodedChunk(input, inputPtr, chunkLen, outputBuffer, outputPtr); inputPtr += chunkLen; left -= chunkLen; } while (left > 0); return outputPtr; }
/** * Alternate version that accepts pre-allocated output buffer. * * <p>Method that will use "safe" {@link ChunkEncoder}, as produced by {@link * ChunkEncoderFactory#safeInstance}, for encoding. Safe here means that it does not use any * non-compliant features beyond core JDK. */ public static int safeAppendEncoded( byte[] input, int inputPtr, int inputLength, byte[] outputBuffer, int outputPtr) { ChunkEncoder enc = ChunkEncoderFactory.safeNonAllocatingInstance(inputLength); int len = appendEncoded(enc, input, inputPtr, inputLength, outputBuffer, outputPtr); enc.close(); return len; }
/** * Method that encodes given input using provided {@link ChunkEncoder}, and aggregating it into a * single byte array and returning that. * * <p>NOTE: method does NOT call {@link ChunkEncoder#close}; caller is responsible for doing that * after it is done using the encoder. */ public static byte[] encode(ChunkEncoder enc, byte[] data, int offset, int length) { int left = length; int chunkLen = Math.min(LZFChunk.MAX_CHUNK_LEN, left); LZFChunk first = enc.encodeChunk(data, offset, chunkLen); left -= chunkLen; // shortcut: if it all fit in, no need to coalesce: if (left < 1) { return first.getData(); } // otherwise need to get other chunks: int resultBytes = first.length(); offset += chunkLen; LZFChunk last = first; do { chunkLen = Math.min(left, LZFChunk.MAX_CHUNK_LEN); LZFChunk chunk = enc.encodeChunk(data, offset, chunkLen); offset += chunkLen; left -= chunkLen; resultBytes += chunk.length(); last.setNext(chunk); last = chunk; } while (left > 0); // and then coalesce returns into single contiguous byte array byte[] result = new byte[resultBytes]; int ptr = 0; for (; first != null; first = first.next()) { ptr = first.copyTo(result, ptr); } return result; }
/** * Method that will use "safe" {@link ChunkEncoder}, as produced by {@link * ChunkEncoderFactory#safeInstance}, for encoding. Safe here means that it does not use any * non-compliant features beyond core JDK. */ public static byte[] safeEncode( byte[] data, int offset, int length, BufferRecycler bufferRecycler) { ChunkEncoder enc = ChunkEncoderFactory.safeInstance(length, bufferRecycler); byte[] result = encode(enc, data, offset, length); enc.close(); return result; }
/** * Alternate version that accepts pre-allocated output buffer. * * <p>Note that {@link ChunkEncoder} instance used is one produced by {@link * ChunkEncoderFactory#optimalNonAllocatingInstance}, which typically is "unsafe" instance if one * can be used on current JVM. */ public static int appendEncoded( byte[] input, int inputPtr, int inputLength, byte[] outputBuffer, int outputPtr, BufferRecycler bufferRecycler) { ChunkEncoder enc = ChunkEncoderFactory.optimalNonAllocatingInstance(inputLength, bufferRecycler); int len = appendEncoded(enc, input, inputPtr, inputLength, outputBuffer, outputPtr); enc.close(); return len; }
/** * Method for compressing given input data using LZF encoding and block structure (compatible with * lzf command line utility). Result consists of a sequence of chunks. * * <p>Note that {@link ChunkEncoder} instance used is one produced by {@link * ChunkEncoderFactory#optimalInstance}, which typically is "unsafe" instance if one can be used * on current JVM. */ public static byte[] encode(byte[] data, int offset, int length) { ChunkEncoder enc = ChunkEncoderFactory.optimalInstance(length); byte[] result = encode(enc, data, offset, length); enc.close(); // important for buffer reuse! return result; }