Skip to content

Commit 477201d

Browse files
committed
Use check, simplify atomic with boolean
1 parent cc45358 commit 477201d

File tree

4 files changed

+14
-26
lines changed

4 files changed

+14
-26
lines changed

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/compression/GzipCompressor.kt

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55
package aws.smithy.kotlin.runtime.compression
66

7-
import kotlinx.atomicfu.atomic
87
import kotlinx.cinterop.*
98
import 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
}

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/compression/GzipNative.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public actual class Gzip : CompressionAlgorithm {
4040
} else {
4141
val compressed = GzipCompressor().apply {
4242
update(sourceBytes)
43-
}.close()
43+
}.flush()
4444

4545
object : ByteStream.Buffer() {
4646
override fun bytes(): ByteArray = compressed

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/io/GzipByteReadChannelNative.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public actual class GzipByteReadChannel actual constructor(public val channel: S
4545

4646
// If still no data is available and the channel is closed, we've hit EOF. Close the compressor and write the remaining bytes
4747
if (compressor.availableForRead == 0 && channel.isClosedForRead) {
48-
val terminationBytes = compressor.close()
48+
val terminationBytes = compressor.flush()
4949
sink.write(terminationBytes)
5050
return terminationBytes.size.toLong()
5151
}
@@ -58,7 +58,7 @@ public actual class GzipByteReadChannel actual constructor(public val channel: S
5858
}
5959

6060
actual override fun cancel(cause: Throwable?): Boolean {
61-
compressor.close()
61+
compressor.flush()
6262
return channel.cancel(cause)
6363
}
6464
}

runtime/runtime-core/native/src/aws/smithy/kotlin/runtime/io/GzipSdkSourceNative.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public actual class GzipSdkSource actual constructor(public val source: SdkSourc
3333

3434
// If still no data is available, we've hit EOF. Close the compressor and write the remaining bytes
3535
if (compressor.availableForRead == 0) {
36-
val terminationBytes = compressor.close()
36+
val terminationBytes = compressor.flush()
3737
sink.write(terminationBytes)
3838
return terminationBytes.size.toLong()
3939
}
@@ -46,7 +46,7 @@ public actual class GzipSdkSource actual constructor(public val source: SdkSourc
4646
}
4747

4848
actual override fun close() {
49-
compressor.close()
49+
compressor.flush()
5050
source.close()
5151
}
5252
}

0 commit comments

Comments
 (0)