/** * Alternate chunk compression method that will append encoded chunk in * pre-allocated buffer. Note that caller must ensure that the buffer is * large enough to hold not just encoded result but also intermediate * result; latter may be up to 4% larger than input; caller may use * {@link LZFEncoder#estimateMaxWorkspaceSize(int)} to calculate * necessary buffer size. * * @return Offset in output buffer after appending the encoded chunk */ public int appendEncodedChunk(final byte[] input, final int inputPtr, final int inputLen, final byte[] outputBuffer, final int outputPos) { if (inputLen >= MIN_BLOCK_TO_COMPRESS) { /* If we have non-trivial block, and can compress it by at least * 2 bytes (since header is 2 bytes longer), use as-is */ final int compStart = outputPos + LZFChunk.HEADER_LEN_COMPRESSED; final int end = tryCompress(input, inputPtr, inputPtr+inputLen, outputBuffer, compStart); final int uncompEnd = (outputPos + LZFChunk.HEADER_LEN_NOT_COMPRESSED) + inputLen; if (end < uncompEnd) { // yes, compressed by at least one byte final int compLen = end - compStart; LZFChunk.appendCompressedHeader(inputLen, compLen, outputBuffer, outputPos); return end; } } // Otherwise append as non-compressed chunk instead (length + 5): return LZFChunk.appendNonCompressed(input, inputPtr, inputLen, outputBuffer, outputPos); }
/** * Alternate chunk compression method that will append encoded chunk in * pre-allocated buffer. Note that caller must ensure that the buffer is * large enough to hold not just encoded result but also intermediate * result; latter may be up to 4% larger than input; caller may use * {@link LZFEncoder#estimateMaxWorkspaceSize(int)} to calculate * necessary buffer size. * * @return Offset in output buffer after appending the encoded chunk */ public int appendEncodedChunk(final byte[] input, final int inputPtr, final int inputLen, final byte[] outputBuffer, final int outputPos) { if (inputLen >= MIN_BLOCK_TO_COMPRESS) { /* If we have non-trivial block, and can compress it by at least * 2 bytes (since header is 2 bytes longer), use as-is */ final int compStart = outputPos + LZFChunk.HEADER_LEN_COMPRESSED; final int end = tryCompress(input, inputPtr, inputPtr+inputLen, outputBuffer, compStart); final int uncompEnd = (outputPos + LZFChunk.HEADER_LEN_NOT_COMPRESSED) + inputLen; if (end < uncompEnd) { // yes, compressed by at least one byte final int compLen = end - compStart; LZFChunk.appendCompressedHeader(inputLen, compLen, outputBuffer, outputPos); return end; } } // Otherwise append as non-compressed chunk instead (length + 5): return LZFChunk.appendNonCompressed(input, inputPtr, inputLen, outputBuffer, outputPos); }
/** * Alternate chunk compression method that will append encoded chunk in * pre-allocated buffer. Note that caller must ensure that the buffer is * large enough to hold not just encoded result but also intermediate * result; latter may be up to 4% larger than input; caller may use * {@link LZFEncoder#estimateMaxWorkspaceSize(int)} to calculate * necessary buffer size. * * @return Offset in output buffer after appending the encoded chunk */ public int appendEncodedChunk(final byte[] input, final int inputPtr, final int inputLen, final byte[] outputBuffer, final int outputPos) { if (inputLen >= MIN_BLOCK_TO_COMPRESS) { /* If we have non-trivial block, and can compress it by at least * 2 bytes (since header is 2 bytes longer), use as-is */ final int compStart = outputPos + LZFChunk.HEADER_LEN_COMPRESSED; final int end = tryCompress(input, inputPtr, inputPtr+inputLen, outputBuffer, compStart); final int uncompEnd = (outputPos + LZFChunk.HEADER_LEN_NOT_COMPRESSED) + inputLen; if (end < uncompEnd) { // yes, compressed by at least one byte final int compLen = end - compStart; LZFChunk.appendCompressedHeader(inputLen, compLen, outputBuffer, outputPos); return end; } } // Otherwise append as non-compressed chunk instead (length + 5): return LZFChunk.appendNonCompressed(input, inputPtr, inputLen, outputBuffer, outputPos); }