]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4FastDecompressor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4FastDecompressor.java
1 package net.jpountz.lz4;
2
3 import java.nio.ByteBuffer;
4
5 /*
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
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
22  * compressed stream.
23  * <p>
24  * Instances of this class are thread-safe.
25  */
26 public abstract class LZ4FastDecompressor implements LZ4Decompressor {
27
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.
31    *
32    * @param destLen the <b>exact</b> size of the original input
33    * @return the number of bytes read to restore the original input
34    */
35   public abstract int decompress(byte[] src, int srcOff, byte[] dest, int destOff, int destLen);
36
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.
41    *
42    * @param destLen the <b>exact</b> size of the original input
43    * @return the number of bytes read to restore the original input
44    */
45   public abstract int decompress(ByteBuffer src, int srcOff, ByteBuffer dest, int destOff, int destLen);
46
47   /**
48    * Convenience method, equivalent to calling
49    * {@link #decompress(byte[], int, byte[], int, int) decompress(src, 0, dest, 0, destLen)}.
50    */
51   public final int decompress(byte[] src, byte[] dest, int destLen) {
52     return decompress(src, 0, dest, 0, destLen);
53   }
54
55   /**
56    * Convenience method, equivalent to calling
57    * {@link #decompress(byte[], byte[], int) decompress(src, dest, dest.length)}.
58    */
59   public final int decompress(byte[] src, byte[] dest) {
60     return decompress(src, dest, dest.length);
61   }
62
63   /**
64    * Convenience method which returns <code>src[srcOff:?]</code>
65    * decompressed.
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>
70    * <pre>
71    * final byte[] decompressed = new byte[destLen];
72    * decompress(src, srcOff, decompressed, 0, destLen);
73    * return decompressed;
74    * </pre>
75    */
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);
79     return decompressed;
80   }
81
82   /**
83    * Convenience method, equivalent to calling
84    * {@link #decompress(byte[], int, int) decompress(src, 0, destLen)}.
85    */
86   public final byte[] decompress(byte[] src, int destLen) {
87     return decompress(src, 0, destLen);
88   }
89
90   /**
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.
94    */
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);
99   }
100
101   @Override
102   public String toString() {
103     return getClass().getSimpleName();
104   }
105
106 }