Skip to content

Commit c8408a7

Browse files
committed
Implement Closeable
1 parent 477201d commit c8408a7

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
*/
55
package aws.smithy.kotlin.runtime.compression
66

7+
import aws.sdk.kotlin.crt.Closeable
78
import kotlinx.cinterop.*
89
import platform.zlib.*
910

1011
/**
1112
* Streaming-style gzip compressor, implemented using zlib bindings
1213
*/
13-
internal class GzipCompressor {
14+
internal class GzipCompressor : Closeable {
1415
companion object {
1516
internal const val BUFFER_SIZE = 16384
1617
}
@@ -106,11 +107,15 @@ internal class GzipCompressor {
106107
outputPin.unpin()
107108
}
108109

109-
deflateEnd(stream.ptr)
110-
nativeHeap.free(stream.ptr)
111-
isClosed = true
112-
113110
return outputBuffer.toByteArray()
114111
}
115112
}
113+
114+
override fun close() {
115+
if (isClosed) { return }
116+
117+
deflateEnd(stream.ptr)
118+
nativeHeap.free(stream.ptr)
119+
isClosed = true
120+
}
116121
}

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

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

7+
import aws.sdk.kotlin.crt.use
78
import aws.smithy.kotlin.runtime.content.ByteStream
89
import aws.smithy.kotlin.runtime.io.GzipByteReadChannel
910
import aws.smithy.kotlin.runtime.io.GzipSdkSource
@@ -38,9 +39,11 @@ public actual class Gzip : CompressionAlgorithm {
3839
if (sourceBytes.isEmpty()) {
3940
stream
4041
} else {
41-
val compressed = GzipCompressor().apply {
42-
update(sourceBytes)
43-
}.flush()
42+
val compressed = GzipCompressor().use {
43+
it.apply {
44+
update(sourceBytes)
45+
}.flush()
46+
}
4447

4548
object : ByteStream.Buffer() {
4649
override fun bytes(): ByteArray = compressed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ public actual class GzipByteReadChannel actual constructor(public val channel: S
4747
if (compressor.availableForRead == 0 && channel.isClosedForRead) {
4848
val terminationBytes = compressor.flush()
4949
sink.write(terminationBytes)
50-
return terminationBytes.size.toLong()
50+
return terminationBytes.size.toLong().also {
51+
compressor.close()
52+
}
5153
}
5254

5355
// Read compressed bytes from the compressor
@@ -58,7 +60,7 @@ public actual class GzipByteReadChannel actual constructor(public val channel: S
5860
}
5961

6062
actual override fun cancel(cause: Throwable?): Boolean {
61-
compressor.flush()
63+
compressor.close()
6264
return channel.cancel(cause)
6365
}
6466
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ public actual class GzipSdkSource actual constructor(public val source: SdkSourc
3535
if (compressor.availableForRead == 0) {
3636
val terminationBytes = compressor.flush()
3737
sink.write(terminationBytes)
38-
return terminationBytes.size.toLong()
38+
return terminationBytes.size.toLong().also {
39+
compressor.close()
40+
}
3941
}
4042

4143
// Read compressed bytes from the compressor
@@ -46,7 +48,7 @@ public actual class GzipSdkSource actual constructor(public val source: SdkSourc
4648
}
4749

4850
actual override fun close() {
49-
compressor.flush()
51+
compressor.close()
5052
source.close()
5153
}
5254
}

0 commit comments

Comments
 (0)