|
| 1 | +package net.jpountz.lz4; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright 2020 Adrien Grand and the lz4-java contributors. |
| 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 | +import net.jpountz.util.ByteBufferUtils; |
| 20 | +import net.jpountz.util.SafeUtils; |
| 21 | + |
| 22 | +import java.nio.ByteBuffer; |
| 23 | +import java.util.concurrent.locks.ReentrantLock; |
| 24 | + |
| 25 | +abstract class AbstractLZ4JNIFastResetCompressor extends LZ4Compressor implements AutoCloseable { |
| 26 | + |
| 27 | + private static final String IN_USE_ERROR = "This compressor is not thread-safe and is already in use"; |
| 28 | + private static final String CLOSED_ERROR = "Compressor has been closed"; |
| 29 | + private static final String UNSUPPORTED_BUFFER_ERROR = "ByteBuffer must be direct or array-backed"; |
| 30 | + |
| 31 | + private final ReentrantLock lock = new ReentrantLock(); |
| 32 | + private long statePtr; |
| 33 | + |
| 34 | + AbstractLZ4JNIFastResetCompressor(long statePtr, String allocationFailureMessage) { |
| 35 | + if (statePtr == 0) { |
| 36 | + throw new LZ4Exception(allocationFailureMessage); |
| 37 | + } |
| 38 | + this.statePtr = statePtr; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Compresses {@code src[srcOff:srcOff+srcLen]} into |
| 43 | + * {@code dest[destOff:destOff+maxDestLen]}. |
| 44 | + * |
| 45 | + * @param src source data |
| 46 | + * @param srcOff the start offset in src |
| 47 | + * @param srcLen the number of bytes to compress |
| 48 | + * @param dest destination buffer |
| 49 | + * @param destOff the start offset in dest |
| 50 | + * @param maxDestLen the maximum number of bytes to write in dest |
| 51 | + * @return the compressed size |
| 52 | + * @throws LZ4Exception if maxDestLen is too small |
| 53 | + * @throws IllegalStateException if the compressor has been closed or is already in use |
| 54 | + */ |
| 55 | + @Override |
| 56 | + public final int compress(byte[] src, int srcOff, int srcLen, byte[] dest, int destOff, int maxDestLen) { |
| 57 | + if (!lock.tryLock()) { |
| 58 | + throw new IllegalStateException(IN_USE_ERROR); |
| 59 | + } |
| 60 | + |
| 61 | + try { |
| 62 | + long ptr = checkOpen(); |
| 63 | + SafeUtils.checkRange(src, srcOff, srcLen); |
| 64 | + SafeUtils.checkRange(dest, destOff, maxDestLen); |
| 65 | + |
| 66 | + final int result = compressNative( |
| 67 | + ptr, src, null, srcOff, srcLen, |
| 68 | + dest, null, destOff, maxDestLen); |
| 69 | + return checkResult(result); |
| 70 | + } finally { |
| 71 | + lock.unlock(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Compresses {@code src[srcOff:srcOff+srcLen]} into |
| 77 | + * {@code dest[destOff:destOff+maxDestLen]}. |
| 78 | + * <p> |
| 79 | + * Both buffers must be either direct or array-backed. |
| 80 | + * {@link ByteBuffer} positions remain unchanged. |
| 81 | + * |
| 82 | + * @param src source data |
| 83 | + * @param srcOff the start offset in src |
| 84 | + * @param srcLen the number of bytes to compress |
| 85 | + * @param dest destination buffer |
| 86 | + * @param destOff the start offset in dest |
| 87 | + * @param maxDestLen the maximum number of bytes to write in dest |
| 88 | + * @return the compressed size |
| 89 | + * @throws LZ4Exception if maxDestLen is too small |
| 90 | + * @throws IllegalArgumentException if src or dest is neither array-backed nor direct |
| 91 | + * @throws IllegalStateException if the compressor has been closed or is already in use |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public final int compress(ByteBuffer src, int srcOff, int srcLen, ByteBuffer dest, int destOff, int maxDestLen) { |
| 95 | + if (!lock.tryLock()) { |
| 96 | + throw new IllegalStateException(IN_USE_ERROR); |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + long ptr = checkOpen(); |
| 101 | + checkByteBuffer(src); |
| 102 | + checkByteBuffer(dest); |
| 103 | + ByteBufferUtils.checkNotReadOnly(dest); |
| 104 | + ByteBufferUtils.checkRange(src, srcOff, srcLen); |
| 105 | + ByteBufferUtils.checkRange(dest, destOff, maxDestLen); |
| 106 | + |
| 107 | + byte[] srcArr = src.hasArray() ? src.array() : null; |
| 108 | + byte[] destArr = dest.hasArray() ? dest.array() : null; |
| 109 | + ByteBuffer srcBuf = srcArr == null ? src : null; |
| 110 | + ByteBuffer destBuf = destArr == null ? dest : null; |
| 111 | + int srcBufferOff = srcOff + (srcArr != null ? src.arrayOffset() : 0); |
| 112 | + int destBufferOff = destOff + (destArr != null ? dest.arrayOffset() : 0); |
| 113 | + |
| 114 | + final int result = compressNative( |
| 115 | + ptr, srcArr, srcBuf, srcBufferOff, srcLen, |
| 116 | + destArr, destBuf, destBufferOff, maxDestLen); |
| 117 | + return checkResult(result); |
| 118 | + } finally { |
| 119 | + lock.unlock(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public final boolean isClosed() { |
| 124 | + lock.lock(); |
| 125 | + try { |
| 126 | + return statePtr == 0; |
| 127 | + } finally { |
| 128 | + lock.unlock(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Closes this compressor and releases native resources. |
| 134 | + * After calling this method, all compress methods will throw {@link IllegalStateException}. |
| 135 | + * |
| 136 | + * @throws IllegalStateException if the compressor is in use by another thread |
| 137 | + */ |
| 138 | + @Override |
| 139 | + public final void close() { |
| 140 | + if (!lock.tryLock()) { |
| 141 | + throw new IllegalStateException(IN_USE_ERROR); |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + long ptr = statePtr; |
| 146 | + statePtr = 0; |
| 147 | + if (ptr != 0) { |
| 148 | + freeState(ptr); |
| 149 | + } |
| 150 | + } finally { |
| 151 | + lock.unlock(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private long checkOpen() { |
| 156 | + if (statePtr == 0) { |
| 157 | + throw new IllegalStateException(CLOSED_ERROR); |
| 158 | + } |
| 159 | + return statePtr; |
| 160 | + } |
| 161 | + |
| 162 | + private static int checkResult(int result) { |
| 163 | + if (result <= 0) { |
| 164 | + throw new LZ4Exception("maxDestLen is too small"); |
| 165 | + } |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + private static void checkByteBuffer(ByteBuffer buffer) { |
| 170 | + if (!(buffer.hasArray() || buffer.isDirect())) { |
| 171 | + throw new IllegalArgumentException(UNSUPPORTED_BUFFER_ERROR); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + protected abstract int compressNative(long ptr, byte[] srcArr, ByteBuffer srcBuf, int srcOff, int srcLen, |
| 176 | + byte[] destArr, ByteBuffer destBuf, int destOff, int maxDestLen); |
| 177 | + |
| 178 | + protected abstract void freeState(long ptr); |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | + |
0 commit comments