Compresses an input ByteBuffer into an output ByteBuffer using Zstandard compression. If the maximum bound of the number of output bytes exceeds the output ByteBuffer size, the remaining bytes are written to the overflow ByteBuffer. @param in the bytes to compress @param out the compress
(ByteBuffer in, ByteBuffer out,
ByteBuffer overflow,
Options options)
| 180 | * @return true if input data is compressed. Otherwise, false. |
| 181 | */ |
| 182 | @Override |
| 183 | public boolean compress(ByteBuffer in, ByteBuffer out, |
| 184 | ByteBuffer overflow, |
| 185 | Options options) throws IOException { |
| 186 | int inBytes = in.remaining(); |
| 187 | // Skip with minimum ZStandard format size: |
| 188 | // https://datatracker.ietf.org/doc/html/rfc8878#name-zstandard-frames |
| 189 | // Magic Number (4 bytes) + Frame Header (2 bytes) + Data Block Header (3 bytes) |
| 190 | if (inBytes < 10) return false; |
| 191 | |
| 192 | ZstdOptions zso = (ZstdOptions) options; |
| 193 | |
| 194 | zstdCompressCtx = new ZstdCompressCtx(); |
| 195 | zstdCompressCtx.setLevel(zso.level); |
| 196 | zstdCompressCtx.setLong(zso.windowLog); |
| 197 | zstdCompressCtx.setChecksum(false); |
| 198 | zstdCompressCtx.setStrategy(zso.strategy); |
| 199 | |
| 200 | try { |
| 201 | byte[] compressed = getBuffer((int) Zstd.compressBound(inBytes)); |
| 202 | |
| 203 | int outBytes = zstdCompressCtx.compressByteArray(compressed, 0, compressed.length, |
| 204 | in.array(), in.arrayOffset() + in.position(), inBytes); |
| 205 | if (outBytes < inBytes) { |
| 206 | int remaining = out.remaining(); |
| 207 | if (remaining >= outBytes) { |
| 208 | System.arraycopy(compressed, 0, out.array(), out.arrayOffset() + |
| 209 | out.position(), outBytes); |
| 210 | out.position(out.position() + outBytes); |
| 211 | } else { |
| 212 | System.arraycopy(compressed, 0, out.array(), out.arrayOffset() + |
| 213 | out.position(), remaining); |
| 214 | out.position(out.limit()); |
| 215 | System.arraycopy(compressed, remaining, overflow.array(), |
| 216 | overflow.arrayOffset(), outBytes - remaining); |
| 217 | overflow.position(outBytes - remaining); |
| 218 | } |
| 219 | return true; |
| 220 | } else { |
| 221 | return false; |
| 222 | } |
| 223 | } finally { |
| 224 | zstdCompressCtx.close(); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void decompress(ByteBuffer in, ByteBuffer out) throws IOException { |