Scoop API

scoop:scoop.compression:0.1.0

src/messages.scoop

Selected production source · src/messages.scoop

1package scoop.compression
3import scoop.platform.memory.NativeByteBuffer
5/**
6 * An RFC 7692 message encoder with explicit message boundaries and context-takeover selection.
7 * Finish each message before starting the next.
8 */
9public class DeflateMessageEncoder public constructor(
10 options: DeflateOptions = DeflateOptions(), contextTakeover: Boolean = true
11) : Closeable {
12 private val codec: CompressionCodec = newMessageEncoder(options, contextTakeover)
13 /**
14 * The actual cumulative byte counts accepted and produced by this codec.
15 */
16 public val totals: CompressionTotals get() = codec.totals
17 /**
18 * The recorded terminal failure, or None before failure.
19 */
20 public val failure: Throwable? get() = codec.failure
22 /**
23 * Processes at most count readable input bytes into the writable output region, advancing both
24 * cursors.
25 */
26 public fun process(input: NativeByteBuffer, output: NativeByteBuffer, count: Long = input.readableBytes): CompressionStep =
27 codec.step(Some(input), count, output, output.writableBytes, 0)
28 /**
29 * Drains a DEFLATE synchronization point without ending the current message.
30 */
31 public fun flush(output: NativeByteBuffer): CompressionStep = codec.step(None, 0L, output, output.writableBytes, 1)
32 /**
33 * Ends the current message and drains its remaining output; repeat until Finished.
34 */
35 public fun finishMessage(output: NativeByteBuffer): CompressionStep = codec.step(None, 0L, output, output.writableBytes, 2)
36 /**
37 * Starts the next message after the previous one finished, retaining or resetting history
38 * according to context takeover.
39 */
40 public fun nextMessage() { codec.nextMessage() }
41 /**
42 * Releases native codec state. Closing does not finish incomplete stream or message data.
43 */
44 public override fun close() { codec.close() }
47/**
48 * An RFC 7692 message decoder with explicit boundaries, window size, context takeover, and
49 * optional output limit.
50 */
51public class DeflateMessageDecoder public constructor(
52 windowBits: Int = 15, contextTakeover: Boolean = true, maxOutputBytes: Long? = None
53) : Closeable {
54 private val codec: CompressionCodec = newMessageDecoder(windowBits, contextTakeover, maxOutputBytes)
55 /**
56 * The actual cumulative byte counts accepted and produced by this codec.
57 */
58 public val totals: CompressionTotals get() = codec.totals
59 /**
60 * The recorded terminal failure, or None before failure.
61 */
62 public val failure: Throwable? get() = codec.failure
64 /**
65 * Processes at most count readable input bytes into the writable output region, advancing both
66 * cursors.
67 */
68 public fun process(input: NativeByteBuffer, output: NativeByteBuffer, count: Long = input.readableBytes): CompressionStep =
69 codec.step(Some(input), count, output, output.writableBytes, 0)
70 /**
71 * Ends the current message and drains its remaining output; repeat until Finished.
72 */
73 public fun finishMessage(output: NativeByteBuffer): CompressionStep = codec.step(None, 0L, output, output.writableBytes, 2)
74 /**
75 * Starts the next message after the previous one finished, retaining or resetting history
76 * according to context takeover.
77 */
78 public fun nextMessage() { codec.nextMessage() }
79 /**
80 * Releases native codec state. Closing does not finish incomplete stream or message data.
81 */
82 public override fun close() { codec.close() }
85private fun newMessageEncoder(options: DeflateOptions, takeover: Boolean): CompressionCodec {
86 val config = NativeCompressionOptions(2, 1, options.level, options.windowBits,
87 deflateStrategyCode(options.strategy), 0, 0, -1L, -1L)
88 @Unsafe { return ownCompressionHandle(compressionMessageNew(config, if (takeover) 1 else 0), CompressionFormat.RawDeflate, true) }
91private fun newMessageDecoder(window: Int, takeover: Boolean, limit: Long?): CompressionCodec {
92 if (limit != None && limit!! < 0L) throw IllegalArgumentException(Some("negative DEFLATE message output limit"))
93 val config = NativeCompressionOptions(2, 0, 0, window, 0, 0, 0, limit ?: -1L, -1L)
94 @Unsafe { return ownCompressionHandle(compressionMessageNew(config, if (takeover) 1 else 0), CompressionFormat.RawDeflate, false) }