scoop:scoop.compression:0.1.0
src/dictionary.scoop
Selected production source · src/dictionary.scoop
1
package scoop.compression3
import scoop.platform.memory.NativeByteBuffer5
/**6
* Owned native dictionary bytes for zstd. Construction copies the supplied bytes; close the7
* dictionary after it is no longer needed for codec construction.8
*/9
public class CompressionDictionary private constructor(private val buffer: NativeByteBuffer) : Closeable {10
/**11
* The dictionary byte count.12
*/13
public val size: Long get() = buffer.readableBytes15
/**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() }36
}