Skip to content

Commit bc3221d

Browse files
committed
Refactor to use SdkBuffer
1 parent a4f7ba6 commit bc3221d

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ package aws.smithy.kotlin.runtime.compression
66

77
import aws.sdk.kotlin.crt.Closeable
88
import aws.smithy.kotlin.runtime.io.SdkBuffer
9-
import aws.smithy.kotlin.runtime.io.SdkByteChannel
10-
import aws.smithy.kotlin.runtime.io.readFully
11-
import aws.smithy.kotlin.runtime.io.readToByteArray
12-
import aws.smithy.kotlin.runtime.io.use
13-
import aws.smithy.kotlin.runtime.io.write
149
import kotlinx.cinterop.*
1510
import platform.zlib.*
1611

@@ -27,11 +22,11 @@ internal class GzipCompressor : Closeable {
2722
}
2823

2924
private val stream = nativeHeap.alloc<z_stream>()
30-
private val outputBuffer = SdkByteChannel()
25+
private val outputBuffer = SdkBuffer()
3126
internal var isClosed = false
3227

3328
internal val availableForRead: Int
34-
get() = outputBuffer.availableForRead
29+
get() = outputBuffer.size.toInt()
3530

3631
init {
3732
// Initialize deflate with gzip encoding
@@ -52,7 +47,7 @@ internal class GzipCompressor : Closeable {
5247
/**
5348
* Update the compressor with [input] bytes
5449
*/
55-
suspend fun update(input: ByteArray) = memScoped {
50+
fun update(input: ByteArray) = memScoped {
5651
check(!isClosed) { "Compressor is closed" }
5752

5853
val inputPin = input.pin()
@@ -82,22 +77,19 @@ internal class GzipCompressor : Closeable {
8277
/**
8378
* Consume [count] gzip-compressed bytes.
8479
*/
85-
suspend fun consume(count: Int): ByteArray {
80+
fun consume(count: Int): ByteArray {
8681
check(!isClosed) { "Compressor is closed" }
8782
require(count in 0..availableForRead) {
8883
"Count must be between 0 and $availableForRead, got $count"
8984
}
9085

91-
return SdkBuffer().use {
92-
outputBuffer.readFully(it, count.toLong())
93-
it.readToByteArray()
94-
}
86+
return outputBuffer.readByteArray(count.toLong())
9587
}
9688

9789
/**
9890
* Flush the compressor and return the terminal sequence of bytes that represent the end of the gzip compression.
9991
*/
100-
suspend fun flush(): ByteArray {
92+
fun flush(): ByteArray {
10193
check(!isClosed) { "Compressor is closed" }
10294

10395
memScoped {
@@ -120,10 +112,7 @@ internal class GzipCompressor : Closeable {
120112
outputPin.unpin()
121113
} while (deflateResult != Z_STREAM_END)
122114

123-
return SdkBuffer().use {
124-
outputBuffer.readFully(it, outputLength)
125-
it.readByteArray()
126-
}
115+
return outputBuffer.readByteArray(outputLength)
127116
}
128117
}
129118

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import aws.smithy.kotlin.runtime.io.GzipByteReadChannel
1010
import aws.smithy.kotlin.runtime.io.GzipSdkSource
1111
import aws.smithy.kotlin.runtime.io.SdkByteReadChannel
1212
import aws.smithy.kotlin.runtime.io.SdkSource
13-
import aws.smithy.kotlin.runtime.io.readToByteArray
13+
import aws.smithy.kotlin.runtime.io.buffer
1414
import aws.smithy.kotlin.runtime.io.source
15-
import kotlinx.coroutines.runBlocking
15+
import aws.smithy.kotlin.runtime.io.use
1616

1717
/**
1818
* The gzip compression algorithm.
@@ -42,8 +42,8 @@ public actual class Gzip : CompressionAlgorithm {
4242
if (bytes.isEmpty()) {
4343
stream
4444
} else {
45-
runBlocking {
46-
GzipSdkSource(bytes.source()).readToByteArray().asByteStream()
45+
GzipSdkSource(bytes.source()).use {
46+
it.buffer().readByteArray().asByteStream()
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)