44 */
55package aws.smithy.kotlin.runtime.compression
66
7- import kotlinx.atomicfu.atomic
87import kotlinx.cinterop.*
98import platform.zlib.*
109
@@ -19,10 +18,7 @@ internal class GzipCompressor {
1918 private val buffer = ByteArray (BUFFER_SIZE )
2019 private val stream = nativeHeap.alloc< z_stream> ()
2120 private val outputBuffer = ArrayList <Byte >()
22- private val _isClosed = atomic(false )
23-
24- internal val isClosed
25- get() = _isClosed .value
21+ internal var isClosed = false
2622
2723 internal val availableForRead: Int
2824 get() = outputBuffer.size
@@ -40,9 +36,7 @@ internal class GzipCompressor {
4036 sizeOf< z_stream> ().toInt(),
4137 )
4238
43- if (initResult != Z_OK ) {
44- throw RuntimeException (" Failed to initialize zlib deflate with error code $initResult : ${zError(initResult)!! .toKString()} " )
45- }
39+ check(initResult == Z_OK ) { " Failed to initialize zlib deflate with error code $initResult : ${zError(initResult)!! .toKString()} " }
4640 }
4741
4842 /* *
@@ -60,9 +54,7 @@ internal class GzipCompressor {
6054 stream.avail_out = BUFFER_SIZE .toUInt()
6155
6256 val deflateResult = deflate(stream.ptr, Z_NO_FLUSH )
63- if (deflateResult != Z_OK ) {
64- throw RuntimeException (" Deflate failed: $deflateResult " )
65- }
57+ check(deflateResult == Z_OK ) { " Deflate failed with error code $deflateResult " }
6658
6759 val bytesWritten = BUFFER_SIZE - stream.avail_out.toInt()
6860 outputBuffer.addAll(buffer.take(bytesWritten))
@@ -77,11 +69,8 @@ internal class GzipCompressor {
7769 * Consume [count] gzip-compressed bytes.
7870 */
7971 fun consume (count : Int ): ByteArray {
80- if (count < 0 ) {
81- throw IllegalArgumentException (" Requested bytes must be at least 0, got $count " )
82- }
83- if (count > availableForRead) {
84- throw IllegalArgumentException (" Requested more bytes than available, $count > ${outputBuffer.size} " )
72+ require(count in 0 .. availableForRead) {
73+ " Count must be between 0 and $availableForRead , got $count "
8574 }
8675
8776 val result = outputBuffer.take(count).toByteArray()
@@ -90,10 +79,9 @@ internal class GzipCompressor {
9079 }
9180
9281 /* *
93- * Close the compressor, clean up all resources, and return the terminal sequence of bytes
94- * that represent the end of the gzip compression.
82+ * Flush the compressor and return the terminal sequence of bytes that represent the end of the gzip compression.
9583 */
96- fun close (): ByteArray {
84+ fun flush (): ByteArray {
9785 if (isClosed) {
9886 return byteArrayOf()
9987 }
@@ -108,7 +96,7 @@ internal class GzipCompressor {
10896
10997 val deflateResult = deflate(stream.ptr, Z_FINISH )
11098 if (deflateResult != Z_STREAM_END && deflateResult != Z_OK ) {
111- throw RuntimeException (" Deflate failed during finish: $deflateResult " )
99+ throw RuntimeException (" Deflate failed during finish with error code $deflateResult " )
112100 }
113101
114102 val bytesWritten = BUFFER_SIZE - stream.avail_out.toInt()
@@ -120,7 +108,7 @@ internal class GzipCompressor {
120108
121109 deflateEnd(stream.ptr)
122110 nativeHeap.free(stream.ptr)
123- _isClosed .value = true
111+ isClosed = true
124112
125113 return outputBuffer.toByteArray()
126114 }
0 commit comments