1 package net.jpountz.lz4;
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import java.nio.ByteBuffer;
18 import java.util.Arrays;
23 * Instances of this class are thread-safe.
25 public abstract class LZ4Compressor {
27 /** Return the maximum compressed length for an input of size <code>length</code>. */
28 @SuppressWarnings("static-method")
29 public final int maxCompressedLength(int length) {
30 return LZ4Utils.maxCompressedLength(length);
34 * Compress <code>src[srcOff:srcOff+srcLen]</code> into
35 * <code>dest[destOff:destOff+destLen]</code> and return the compressed
38 * This method will throw a {@link LZ4Exception} if this compressor is unable
39 * to compress the input into less than <code>maxDestLen</code> bytes. To
40 * prevent this exception to be thrown, you should make sure that
41 * <code>maxDestLen >= maxCompressedLength(srcLen)</code>.
43 * @throws LZ4Exception if maxDestLen is too small
44 * @return the compressed size
46 public abstract int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
49 * Compress <code>src[srcOff:srcOff+srcLen]</code> into
50 * <code>dest[destOff:destOff+destLen]</code> and return the compressed
53 * This method will throw a {@link LZ4Exception} if this compressor is unable
54 * to compress the input into less than <code>maxDestLen</code> bytes. To
55 * prevent this exception to be thrown, you should make sure that
56 * <code>maxDestLen >= maxCompressedLength(srcLen)</code>.
58 * {@link ByteBuffer} positions remain unchanged.
60 * @throws LZ4Exception if maxDestLen is too small
61 * @return the compressed size
63 public abstract int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen);
66 * Convenience method, equivalent to calling
67 * {@link #compress(byte[], int, int, byte[], int, int) compress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)}.
69 public final int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff) {
70 return compress(src, srcOff, srcLen, dest, destOff, dest.length - destOff);
74 * Convenience method, equivalent to calling
75 * {@link #compress(byte[], int, int, byte[], int) compress(src, 0, src.length, dest, 0)}.
77 public final int compress(byte[] src, byte[] dest) {
78 return compress(src, 0, src.length, dest, 0);
82 * Convenience method which returns <code>src[srcOff:srcOff+srcLen]</code>
84 * <p><b><span style="color:red">Warning</span></b>: this method has an
85 * important overhead due to the fact that it needs to allocate a buffer to
86 * compress into, and then needs to resize this buffer to the actual
87 * compressed length.</p>
88 * <p>Here is how this method is implemented:</p>
90 * final int maxCompressedLength = maxCompressedLength(srcLen);
91 * final byte[] compressed = new byte[maxCompressedLength];
92 * final int compressedLength = compress(src, srcOff, srcLen, compressed, 0);
93 * return Arrays.copyOf(compressed, compressedLength);
96 public final byte[] compress(byte[] src, int srcOff, int srcLen) {
97 final int maxCompressedLength = maxCompressedLength(srcLen);
98 final byte[] compressed = new byte[maxCompressedLength];
99 final int compressedLength = compress(src, srcOff, srcLen, compressed, 0);
100 return Arrays.copyOf(compressed, compressedLength);
104 * Convenience method, equivalent to calling
105 * {@link #compress(byte[], int, int) compress(src, 0, src.length)}.
107 public final byte[] compress(byte[] src) {
108 return compress(src, 0, src.length);
112 * Compress <code>src</code> into <code>dest</code>. Calling this method
113 * will update the positions of both {@link ByteBuffer}s.
115 public final void compress(ByteBuffer src, ByteBuffer dest) {
116 final int cpLen = compress(src, src.position(), src.remaining(), dest, dest.position(), dest.remaining());
117 src.position(src.limit());
118 dest.position(dest.position() + cpLen);
122 public String toString() {
123 return getClass().getSimpleName();