]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4SafeDecompressor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4SafeDecompressor.java
1 package net.jpountz.lz4;
2
3 /*
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 import java.nio.ByteBuffer;
18 import java.util.Arrays;
19
20 /**
21  * LZ4 decompressor that requires the size of the compressed data to be known.
22  * <p>
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
25  * be known.
26  */
27 public abstract class LZ4SafeDecompressor implements LZ4UnknownSizeDecompressor {
28
29   /**
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>.
33    *
34    * @param srcLen the exact size of the compressed stream
35    * @return the original input size
36    * @throws LZ4Exception if maxDestLen is too small
37    */
38   public abstract int decompress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
39
40   /**
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>.
44    *
45    * @param srcLen the exact size of the compressed stream
46    * @return the original input size
47    * @throws LZ4Exception if maxDestLen is too small
48    */
49   public abstract int decompress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen);
50
51   /**
52    * Convenience method, equivalent to calling
53    * {@link #decompress(byte[], int, int, byte[], int, int) decompress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)}.
54    */
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);
57   }
58
59   /**
60    * Convenience method, equivalent to calling
61    * {@link #decompress(byte[], int, int, byte[], int) decompress(src, 0, src.length, dest, 0)}
62    */
63   public final int decompress(byte[] src, byte[] dest) {
64     return decompress(src, 0, src.length, dest, 0);
65   }
66
67   /**
68    * Convenience method which returns <code>src[srcOff:srcOff+srcLen]</code>
69    * decompressed.
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>
75    * <pre>
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);
80    * }
81    * return decompressed;
82    * </pre>
83    */
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);
89     }
90     return decompressed;
91   }
92
93   /**
94    * Convenience method, equivalent to calling
95    * {@link #decompress(byte[], int, int, int) decompress(src, 0, src.length, maxDestLen)}.
96    */
97   public final byte[] decompress(byte[] src, int maxDestLen) {
98     return decompress(src, 0, src.length, maxDestLen);
99   }
100
101   /**
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.
105    */
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);
110   }
111
112   @Override
113   public String toString() {
114     return getClass().getSimpleName();
115   }
116
117 }