Skip to content

Commit 0cf628a

Browse files
committed
Split JvmAndNative SdkBuffer back into Jvm and Native
1 parent 692ce00 commit 0cf628a

File tree

13 files changed

+427
-28
lines changed

13 files changed

+427
-28
lines changed

runtime/crt-util/jvm/src/aws/smithy/kotlin/runtime/crt/ReadChannelBodyStreamJvm.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ package aws.smithy.kotlin.runtime.crt
77

88
import aws.sdk.kotlin.crt.io.MutableBuffer
99
import aws.smithy.kotlin.runtime.io.SdkBuffer
10-
import aws.smithy.kotlin.runtime.io.read
1110

1211
internal actual fun transferRequestBody(outgoing: SdkBuffer, dest: MutableBuffer) = outgoing.read(dest.buffer)

runtime/runtime-core/api/runtime-core.api

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -871,10 +871,6 @@ public final class aws/smithy/kotlin/runtime/io/HashingSource : aws/smithy/kotli
871871
}
872872

873873
public final class aws/smithy/kotlin/runtime/io/JavaIOKt {
874-
public static final fun inputStream (Laws/smithy/kotlin/runtime/io/SdkBuffer;)Ljava/io/InputStream;
875-
public static final fun isOpen (Laws/smithy/kotlin/runtime/io/SdkBuffer;)Z
876-
public static final fun outputStream (Laws/smithy/kotlin/runtime/io/SdkBuffer;)Ljava/io/OutputStream;
877-
public static final fun read (Laws/smithy/kotlin/runtime/io/SdkBuffer;Ljava/nio/ByteBuffer;)I
878874
public static final fun sink (Ljava/io/File;)Laws/smithy/kotlin/runtime/io/SdkSink;
879875
public static final fun sink (Ljava/io/OutputStream;)Laws/smithy/kotlin/runtime/io/SdkSink;
880876
public static final fun sink (Ljava/nio/file/Path;)Laws/smithy/kotlin/runtime/io/SdkSink;
@@ -885,7 +881,6 @@ public final class aws/smithy/kotlin/runtime/io/JavaIOKt {
885881
public static final fun source (Ljava/nio/file/Path;Lkotlin/ranges/LongRange;)Laws/smithy/kotlin/runtime/io/SdkSource;
886882
public static synthetic fun source$default (Ljava/io/File;JJILjava/lang/Object;)Laws/smithy/kotlin/runtime/io/SdkSource;
887883
public static synthetic fun source$default (Ljava/nio/file/Path;JJILjava/lang/Object;)Laws/smithy/kotlin/runtime/io/SdkSource;
888-
public static final fun write (Laws/smithy/kotlin/runtime/io/SdkBuffer;Ljava/nio/ByteBuffer;)I
889884
}
890885

891886
public final class aws/smithy/kotlin/runtime/io/SdkBuffer : aws/smithy/kotlin/runtime/io/SdkBufferedSink, aws/smithy/kotlin/runtime/io/SdkBufferedSource {
@@ -898,8 +893,12 @@ public final class aws/smithy/kotlin/runtime/io/SdkBuffer : aws/smithy/kotlin/ru
898893
public fun getBuffer ()Laws/smithy/kotlin/runtime/io/SdkBuffer;
899894
public final fun getSize ()J
900895
public fun hashCode ()I
896+
public fun inputStream ()Ljava/io/InputStream;
897+
public fun isOpen ()Z
898+
public fun outputStream ()Ljava/io/OutputStream;
901899
public fun peek ()Laws/smithy/kotlin/runtime/io/SdkBufferedSource;
902900
public fun read (Laws/smithy/kotlin/runtime/io/SdkBuffer;J)J
901+
public fun read (Ljava/nio/ByteBuffer;)I
903902
public fun read ([BII)I
904903
public fun readAll (Laws/smithy/kotlin/runtime/io/SdkSink;)J
905904
public fun readByte ()B
@@ -919,6 +918,7 @@ public final class aws/smithy/kotlin/runtime/io/SdkBuffer : aws/smithy/kotlin/ru
919918
public fun toString ()Ljava/lang/String;
920919
public fun write (Laws/smithy/kotlin/runtime/io/SdkBuffer;J)V
921920
public fun write (Laws/smithy/kotlin/runtime/io/SdkSource;J)V
921+
public fun write (Ljava/nio/ByteBuffer;)I
922922
public fun write ([BII)V
923923
public fun writeAll (Laws/smithy/kotlin/runtime/io/SdkSource;)J
924924
public fun writeByte (B)V
@@ -931,9 +931,11 @@ public final class aws/smithy/kotlin/runtime/io/SdkBuffer : aws/smithy/kotlin/ru
931931
public fun writeUtf8 (Ljava/lang/String;II)V
932932
}
933933

934-
public abstract interface class aws/smithy/kotlin/runtime/io/SdkBufferedSink : aws/smithy/kotlin/runtime/io/SdkSink {
934+
public abstract interface class aws/smithy/kotlin/runtime/io/SdkBufferedSink : aws/smithy/kotlin/runtime/io/SdkSink, java/nio/channels/WritableByteChannel {
935935
public abstract fun emit ()V
936+
public abstract fun flush ()V
936937
public abstract fun getBuffer ()Laws/smithy/kotlin/runtime/io/SdkBuffer;
938+
public abstract fun outputStream ()Ljava/io/OutputStream;
937939
public abstract fun write (Laws/smithy/kotlin/runtime/io/SdkSource;J)V
938940
public abstract fun write ([BII)V
939941
public static synthetic fun write$default (Laws/smithy/kotlin/runtime/io/SdkBufferedSink;[BIIILjava/lang/Object;)V
@@ -954,9 +956,10 @@ public final class aws/smithy/kotlin/runtime/io/SdkBufferedSink$DefaultImpls {
954956
public static synthetic fun writeUtf8$default (Laws/smithy/kotlin/runtime/io/SdkBufferedSink;Ljava/lang/String;IIILjava/lang/Object;)V
955957
}
956958

957-
public abstract interface class aws/smithy/kotlin/runtime/io/SdkBufferedSource : aws/smithy/kotlin/runtime/io/SdkSource {
959+
public abstract interface class aws/smithy/kotlin/runtime/io/SdkBufferedSource : aws/smithy/kotlin/runtime/io/SdkSource, java/nio/channels/ReadableByteChannel {
958960
public abstract fun exhausted ()Z
959961
public abstract fun getBuffer ()Laws/smithy/kotlin/runtime/io/SdkBuffer;
962+
public abstract fun inputStream ()Ljava/io/InputStream;
960963
public abstract fun peek ()Laws/smithy/kotlin/runtime/io/SdkBufferedSource;
961964
public abstract fun read ([BII)I
962965
public static synthetic fun read$default (Laws/smithy/kotlin/runtime/io/SdkBufferedSource;[BIIILjava/lang/Object;)I
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.io
7+
8+
import java.io.OutputStream
9+
import java.nio.ByteBuffer
10+
11+
internal actual class BufferedSinkAdapter actual constructor(
12+
sink: okio.BufferedSink,
13+
) : AbstractBufferedSinkAdapter(sink),
14+
SdkBufferedSink {
15+
override fun write(src: ByteBuffer): Int = delegate.write(src)
16+
17+
override fun isOpen(): Boolean = delegate.isOpen
18+
19+
override fun outputStream(): OutputStream = delegate.outputStream()
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.io
7+
8+
import java.io.InputStream
9+
import java.nio.ByteBuffer
10+
11+
internal actual class BufferedSourceAdapter actual constructor(
12+
source: okio.BufferedSource,
13+
) : AbstractBufferedSourceAdapter(source),
14+
SdkBufferedSource {
15+
16+
override fun read(dst: ByteBuffer): Int = delegate.read(dst)
17+
18+
override fun isOpen(): Boolean = delegate.isOpen
19+
20+
override fun inputStream(): InputStream = delegate.inputStream()
21+
}

runtime/runtime-core/jvm/src/aws/smithy/kotlin/runtime/io/JavaIO.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,3 @@ public fun InputStream.source(): SdkSource = okioSource().toSdk()
6060
* Create a new [SdkSource] that reads from this [InputStream]
6161
*/
6262
public fun OutputStream.sink(): SdkSink = okioSink().toSdk()
63-
64-
// BufferedSinkAdapter
65-
internal fun BufferedSinkAdapter.outputStream(): OutputStream = delegate.outputStream()
66-
internal fun BufferedSinkAdapter.write(src: ByteBuffer): Int = delegate.write(src)
67-
internal fun BufferedSinkAdapter.isOpen(): Boolean = delegate.isOpen
68-
69-
// BufferedSourceAdapter
70-
internal fun BufferedSourceAdapter.read(dst: ByteBuffer): Int = delegate.read(dst)
71-
internal fun BufferedSourceAdapter.isOpen(): Boolean = delegate.isOpen
72-
internal fun BufferedSourceAdapter.inputStream(): InputStream = delegate.inputStream()
73-
74-
// SdkBuffer
75-
public fun SdkBuffer.read(dst: ByteBuffer): Int = wrapOkio { inner.read(dst) }
76-
public fun SdkBuffer.write(src: ByteBuffer): Int = wrapOkio { inner.write(src) }
77-
public fun SdkBuffer.isOpen(): Boolean = inner.isOpen
78-
public fun SdkBuffer.inputStream(): InputStream = inner.inputStream()
79-
public fun SdkBuffer.outputStream(): OutputStream = inner.outputStream()
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.io
7+
8+
import aws.smithy.kotlin.runtime.io.internal.*
9+
import java.io.InputStream
10+
import java.io.OutputStream
11+
import java.nio.ByteBuffer
12+
13+
// FIXME Commonize with Native after removing WritableByteStream / ReadableByteStream in 1.6.x
14+
public actual class SdkBuffer :
15+
SdkBufferedSource,
16+
SdkBufferedSink {
17+
public actual constructor() : this(okio.Buffer())
18+
19+
internal actual val inner: okio.Buffer
20+
21+
internal actual constructor(buffer: okio.Buffer) {
22+
this.inner = buffer
23+
}
24+
25+
public actual val size: Long
26+
get() = inner.size
27+
28+
actual override val buffer: SdkBuffer
29+
get() = this
30+
31+
override fun toString(): String = inner.toString()
32+
33+
override fun hashCode(): Int = inner.hashCode()
34+
35+
override fun equals(other: Any?): Boolean {
36+
if (this === other) return true
37+
if (other !is SdkBuffer) return false
38+
return inner == other.inner
39+
}
40+
41+
actual override fun skip(byteCount: Long): Unit = commonSkip(byteCount)
42+
43+
actual override fun readByte(): Byte = commonReadByte()
44+
45+
actual override fun readShort(): Short = commonReadShort()
46+
47+
actual override fun readShortLe(): Short = commonReadShortLe()
48+
49+
actual override fun readLong(): Long = commonReadLong()
50+
51+
actual override fun readLongLe(): Long = commonReadLongLe()
52+
53+
actual override fun readInt(): Int = commonReadInt()
54+
55+
actual override fun readIntLe(): Int = commonReadIntLe()
56+
57+
actual override fun readAll(sink: SdkSink): Long = commonReadAll(sink)
58+
59+
actual override fun read(sink: ByteArray, offset: Int, limit: Int): Int =
60+
commonRead(sink, offset, limit)
61+
62+
actual override fun read(sink: SdkBuffer, limit: Long): Long =
63+
commonRead(sink, limit)
64+
65+
override fun read(dst: ByteBuffer): Int = inner.read(dst)
66+
67+
actual override fun readByteArray(): ByteArray = commonReadByteArray()
68+
69+
actual override fun readByteArray(byteCount: Long): ByteArray = commonReadByteArray(byteCount)
70+
71+
actual override fun readUtf8(): String = commonReadUtf8()
72+
73+
actual override fun readUtf8(byteCount: Long): String = commonReadUtf8(byteCount)
74+
75+
actual override fun peek(): SdkBufferedSource = commonPeek()
76+
77+
actual override fun exhausted(): Boolean = commonExhausted()
78+
actual override fun request(byteCount: Long): Boolean = commonRequest(byteCount)
79+
80+
actual override fun require(byteCount: Long): Unit = commonRequire(byteCount)
81+
82+
actual override fun write(source: ByteArray, offset: Int, limit: Int): Unit =
83+
commonWrite(source, offset, limit)
84+
85+
actual override fun write(source: SdkSource, byteCount: Long): Unit =
86+
commonWrite(source, byteCount)
87+
88+
actual override fun write(source: SdkBuffer, byteCount: Long): Unit =
89+
commonWrite(source, byteCount)
90+
91+
override fun write(src: ByteBuffer): Int = inner.write(src)
92+
93+
actual override fun writeAll(source: SdkSource): Long = commonWriteAll(source)
94+
95+
actual override fun writeUtf8(string: String, start: Int, endExclusive: Int): Unit =
96+
commonWriteUtf8(string, start, endExclusive)
97+
98+
actual override fun writeByte(x: Byte): Unit = commonWriteByte(x)
99+
100+
actual override fun writeShort(x: Short): Unit = commonWriteShort(x)
101+
102+
actual override fun writeShortLe(x: Short): Unit = commonWriteShortLe(x)
103+
104+
actual override fun writeInt(x: Int): Unit = commonWriteInt(x)
105+
106+
actual override fun writeIntLe(x: Int): Unit = commonWriteIntLe(x)
107+
108+
actual override fun writeLong(x: Long): Unit = commonWriteLong(x)
109+
110+
actual override fun writeLongLe(x: Long): Unit = commonWriteLongLe(x)
111+
112+
actual override fun flush(): Unit = commonFlush()
113+
114+
actual override fun emit() {
115+
inner.emit()
116+
}
117+
actual override fun close(): Unit = commonClose()
118+
override fun isOpen(): Boolean = inner.isOpen
119+
120+
override fun inputStream(): InputStream = inner.inputStream()
121+
override fun outputStream(): OutputStream = inner.outputStream()
122+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package aws.smithy.kotlin.runtime.io
7+
8+
import java.io.OutputStream
9+
import java.nio.channels.WritableByteChannel
10+
import kotlin.jvm.Throws
11+
12+
/**
13+
* A sink that keeps a buffer internally so that callers can do small writes without
14+
* a performance penalty.
15+
*/
16+
public actual sealed interface SdkBufferedSink :
17+
SdkSink,
18+
WritableByteChannel {
19+
/**
20+
* The underlying buffer for this sink
21+
*/
22+
public actual val buffer: SdkBuffer
23+
24+
/**
25+
* Write [limit] bytes from [source] starting at [offset]
26+
*/
27+
@Throws(IOException::class)
28+
public actual fun write(source: ByteArray, offset: Int, limit: Int): Unit
29+
30+
/**
31+
* Write all bytes from [source] to this sink.
32+
* @return the number of bytes read which will be 0 if [source] is exhausted
33+
*/
34+
@Throws(IOException::class)
35+
public actual fun writeAll(source: SdkSource): Long
36+
37+
/**
38+
* Removes [byteCount] bytes from [source] and writes them to this sink.
39+
*/
40+
@Throws(IOException::class)
41+
public actual fun write(source: SdkSource, byteCount: Long): Unit
42+
43+
/**
44+
* Write UTF8-bytes of [string] to this sink starting at [start] index up to [endExclusive] index.
45+
*/
46+
@Throws(IOException::class)
47+
public actual fun writeUtf8(string: String, start: Int, endExclusive: Int): Unit
48+
49+
/**
50+
* Writes byte [x] to this sink
51+
*/
52+
@Throws(IOException::class)
53+
public actual fun writeByte(x: Byte): Unit
54+
55+
/**
56+
* Writes short [x] as a big-endian bytes to this sink
57+
*/
58+
@Throws(IOException::class)
59+
public actual fun writeShort(x: Short): Unit
60+
61+
/**
62+
* Writes short [x] as a little-endian bytes to this sink
63+
*/
64+
@Throws(IOException::class)
65+
public actual fun writeShortLe(x: Short): Unit
66+
67+
/**
68+
* Writes int [x] as a big-endian bytes to this sink
69+
*/
70+
@Throws(IOException::class)
71+
public actual fun writeInt(x: Int): Unit
72+
73+
/**
74+
* Writes int [x] as a little-endian bytes to this sink
75+
*/
76+
@Throws(IOException::class)
77+
public actual fun writeIntLe(x: Int): Unit
78+
79+
/**
80+
* Writes long [x] as a big-endian bytes to this sink
81+
*/
82+
@Throws(IOException::class)
83+
public actual fun writeLong(x: Long): Unit
84+
85+
/**
86+
* Writes long [x] as a little-endian bytes to this sink
87+
*/
88+
@Throws(IOException::class)
89+
public actual fun writeLongLe(x: Long): Unit
90+
91+
/**
92+
* Return an output stream that writes to this sink
93+
*/
94+
public fun outputStream(): OutputStream
95+
96+
/**
97+
* Writes all buffered data to the underlying sink.
98+
*/
99+
@Throws(IOException::class)
100+
actual override fun flush(): Unit
101+
102+
/**
103+
* Writes all buffered data to the underlying sink. Like flush, but weaker (ensures data is pushed to the
104+
* underlying sink but not necessarily all the way down the chain like [flush] does). Call before this sink
105+
* goes out of scope to ensure any buffered data eventually gets to its final destination
106+
*/
107+
@Throws(IOException::class)
108+
public actual fun emit(): Unit
109+
}

0 commit comments

Comments
 (0)