1 package net.jpountz.lz4;
3 import java.nio.ByteBuffer;
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
20 * LZ4 decompressor that requires the size of the original input to be known.
21 * Use {@link LZ4SafeDecompressor} if you only know the size of the
24 * Instances of this class are thread-safe.
26 public abstract class LZ4FastDecompressor implements LZ4Decompressor {
28 /** Decompress <code>src[srcOff:]</code> into <code>dest[destOff:destOff+destLen]</code>
29 * and return the number of bytes read from <code>src</code>.
30 * <code>destLen</code> must be exactly the size of the decompressed data.
32 * @param destLen the <b>exact</b> size of the original input
33 * @return the number of bytes read to restore the original input
35 public abstract int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen);
37 /** Decompress <code>src[srcOff:]</code> into <code>dest[destOff:destOff+destLen]</code>
38 * and return the number of bytes read from <code>src</code>.
39 * <code>destLen</code> must be exactly the size of the decompressed data.
40 * The positions and limits of the {@link ByteBuffer}s remain unchanged.
42 * @param destLen the <b>exact</b> size of the original input
43 * @return the number of bytes read to restore the original input
45 public abstract int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen);
48 * Convenience method, equivalent to calling
49 * {@link #decompress(byte[], int, byte[], int, int) decompress(src, 0, dest, 0, destLen)}.
51 public final int decompress(byte[] src, byte[] dest, int destLen) {
52 return decompress(src, 0, dest, 0, destLen);
56 * Convenience method, equivalent to calling
57 * {@link #decompress(byte[], byte[], int) decompress(src, dest, dest.length)}.
59 public final int decompress(byte[] src, byte[] dest) {
60 return decompress(src, dest, dest.length);
64 * Convenience method which returns <code>src[srcOff:?]</code>
66 * <p><b><span style="color:red">Warning</span></b>: this method has an
67 * important overhead due to the fact that it needs to allocate a buffer to
68 * decompress into.</p>
69 * <p>Here is how this method is implemented:</p>
71 * final byte[] decompressed = new byte[destLen];
72 * decompress(src, srcOff, decompressed, 0, destLen);
73 * return decompressed;
76 public final byte[] decompress(byte[] src, int srcOff, int destLen) {
77 final byte[] decompressed = new byte[destLen];
78 decompress(src, srcOff, decompressed, 0, destLen);
83 * Convenience method, equivalent to calling
84 * {@link #decompress(byte[], int, int) decompress(src, 0, destLen)}.
86 public final byte[] decompress(byte[] src, int destLen) {
87 return decompress(src, 0, destLen);
91 * Decompress <code>src</code> into <code>dest</code>. <code>dest</code>'s
92 * {@link ByteBuffer#remaining()} must be exactly the size of the decompressed
93 * data. This method moves the positions of the buffers.
95 public final void decompress(ByteBuffer src, ByteBuffer dest) {
96 final int read = decompress(src, src.position(), dest, dest.position(), dest.remaining());
97 dest.position(dest.limit());
98 src.position(src.position() + read);
102 public String toString() {
103 return getClass().getSimpleName();