]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.lz4/src/net/jpountz/lz4/LZ4Compressor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.lz4 / src / net / jpountz / lz4 / LZ4Compressor.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 compressor.
22  * <p>
23  * Instances of this class are thread-safe.
24  */
25 public abstract class LZ4Compressor {
26
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);
31   }
32
33   /**
34    * Compress <code>src[srcOff:srcOff+srcLen]</code> into
35    * <code>dest[destOff:destOff+destLen]</code> and return the compressed
36    * length.
37    *
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>.
42    *
43    * @throws LZ4Exception if maxDestLen is too small
44    * @return the compressed size
45    */
46   public abstract int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen);
47
48   /**
49    * Compress <code>src[srcOff:srcOff+srcLen]</code> into
50    * <code>dest[destOff:destOff+destLen]</code> and return the compressed
51    * length.
52    *
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>.
57    *
58    * {@link ByteBuffer} positions remain unchanged.
59    *
60    * @throws LZ4Exception if maxDestLen is too small
61    * @return the compressed size
62    */
63   public abstract int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen);
64
65   /**
66    * Convenience method, equivalent to calling
67    * {@link #compress(byte[], int, int, byte[], int, int) compress(src, srcOff, srcLen, dest, destOff, dest.length - destOff)}.
68    */
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);
71   }
72
73   /**
74    * Convenience method, equivalent to calling
75    * {@link #compress(byte[], int, int, byte[], int) compress(src, 0, src.length, dest, 0)}.
76    */
77   public final int compress(byte[] src, byte[] dest) {
78     return compress(src, 0, src.length, dest, 0);
79   }
80
81   /**
82    * Convenience method which returns <code>src[srcOff:srcOff+srcLen]</code>
83    * compressed.
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>
89    * <pre>
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);
94    * </pre>
95    */
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);
101   }
102
103   /**
104    * Convenience method, equivalent to calling
105    * {@link #compress(byte[], int, int) compress(src, 0, src.length)}.
106    */
107   public final byte[] compress(byte[] src) {
108     return compress(src, 0, src.length);
109   }
110
111   /**
112    * Compress <code>src</code> into <code>dest</code>. Calling this method
113    * will update the positions of both {@link ByteBuffer}s.
114    */
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);
119   }
120
121   @Override
122   public String toString() {
123     return getClass().getSimpleName();
124   }
125
126 }