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;
21 * LZ4 decompressor that requires the size of the compressed data to be known.
23 * Implementations of this class are usually a little slower than those of
24 * {@link LZ4FastDecompressor} but do not require the size of the original data to
27 public abstract class LZ4SafeDecompressor implements LZ4UnknownSizeDecompressor {
30 * Decompress <code>src[srcOff:srcLen]</code> into
31 * <code>dest[destOff:destOff+maxDestLen]</code> and returns the number of
32 * decompressed bytes written into <code>dest</code>.
34 * @param srcLen the exact size of the compressed stream
35 * @return the original input size
36 * @throws LZ4Exception if maxDestLen is too small
38 public abstract int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
41 * Uncompress <code>src[srcOff:srcLen]</code> into
42 * <code>dest[destOff:destOff+maxDestLen]</code> and returns the number of
43 * decompressed bytes written into <code>dest</code>.
45 * @param srcLen the exact size of the compressed stream
46 * @return the original input size
47 * @throws LZ4Exception if maxDestLen is too small
49 public abstract int decompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen);
52 * Convenience method, equivalent to calling
53 * {@link #decompress(byte[], int, int, byte[], int, int) decompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)}.
55 public final int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff) {
56 return decompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff);
60 * Convenience method, equivalent to calling
61 * {@link #decompress(byte[], int, int, byte[], int) decompress(src, 0, src.length, dest, 0)}
63 public final int decompress(byte[] src, byte[] dest) {
64 return decompress(src, 0, src.length, dest, 0);
68 * Convenience method which returns <code>src[srcOff:srcOff+srcLen]</code>
70 * <p><b><span style="color:red">Warning</span></b>: this method has an
71 * important overhead due to the fact that it needs to allocate a buffer to
72 * decompress into, and then needs to resize this buffer to the actual
73 * decompressed length.</p>
74 * <p>Here is how this method is implemented:</p>
76 * byte[] decompressed = new byte[maxDestLen];
77 * final int decompressedLength = decompress(src, srcOff, srcLen, decompressed, 0, maxDestLen);
78 * if (decompressedLength != decompressed.length) {
79 * decompressed = Arrays.copyOf(decompressed, decompressedLength);
81 * return decompressed;
84 public final byte[] decompress(byte[] src, int srcOff, int srcLen, int maxDestLen) {
85 byte[] decompressed = new byte[maxDestLen];
86 final int decompressedLength = decompress(src, srcOff, srcLen, decompressed, 0, maxDestLen);
87 if (decompressedLength != decompressed.length) {
88 decompressed = Arrays.copyOf(decompressed, decompressedLength);
94 * Convenience method, equivalent to calling
95 * {@link #decompress(byte[], int, int, int) decompress(src, 0, src.length, maxDestLen)}.
97 public final byte[] decompress(byte[] src, int maxDestLen) {
98 return decompress(src, 0, src.length, maxDestLen);
102 * Decompress <code>src</code> into <code>dest</code>. <code>src</code>'s
103 * {@link ByteBuffer#remaining()} must be exactly the size of the compressed
104 * data. This method moves the positions of the buffers.
106 public final void decompress(ByteBuffer src, ByteBuffer dest) {
107 final int decompressed = decompress(src, src.position(), src.remaining(), dest, dest.position(), dest.remaining());
108 src.position(src.limit());
109 dest.position(dest.position() + decompressed);
113 public String toString() {
114 return getClass().getSimpleName();