Scoop API

scoop:scoop.compression:0.1.0

src/dictionary.scoop

Selected production source · src/dictionary.scoop

1package scoop.compression
3import scoop.platform.memory.NativeByteBuffer
5/**
6 * Owned native dictionary bytes for zstd. Construction copies the supplied bytes; close the
7 * dictionary after it is no longer needed for codec construction.
8 */
9public class CompressionDictionary private constructor(private val buffer: NativeByteBuffer) : Closeable {
10 /**
11 * The dictionary byte count.
12 */
13 public val size: Long get() = buffer.readableBytes
15 /**
16 * Creates owned dictionary storage from application bytes.
17 */
18 public companion object {
19 /**
20 * Copies dictionary bytes into independently owned native storage.
21 */
22 public fun fromByteArray(data: List<Byte>): CompressionDictionary {
23 if (data.size == 0L) throw IllegalArgumentException(Some("compression dictionary cannot be empty"))
24 val buffer = NativeByteBuffer.allocate(data.size)
25 try { buffer.writeBytes(data); return CompressionDictionary(buffer) }
26 catch (error: Throwable) { buffer.close(); throw error }
27 }
28 }
30 @Unsafe internal fun <T> withBytes(block: (Ptr<Byte>?, Long) -> T): T = buffer.withReadableBytes(block)
32 /**
33 * Releases owned dictionary storage.
34 */
35 public override fun close() { buffer.close() }