File tree Expand file tree Collapse file tree 4 files changed +24
-12
lines changed
runtime/runtime-core/native/src/aws/smithy/kotlin/runtime Expand file tree Collapse file tree 4 files changed +24
-12
lines changed Original file line number Diff line number Diff line change 44 */
55package aws.smithy.kotlin.runtime.compression
66
7+ import aws.sdk.kotlin.crt.Closeable
78import kotlinx.cinterop.*
89import 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}
Original file line number Diff line number Diff line change 44 */
55package aws.smithy.kotlin.runtime.compression
66
7+ import aws.sdk.kotlin.crt.use
78import aws.smithy.kotlin.runtime.content.ByteStream
89import aws.smithy.kotlin.runtime.io.GzipByteReadChannel
910import 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
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments