Skip to content

Commit

Permalink
KTOR-7651 Add missing writeFloat/readFloat, writeDouble/readDouble fo…
Browse files Browse the repository at this point in the history
…r channels
  • Loading branch information
e5l committed Jan 9, 2025
1 parent f046ae3 commit b693e9d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ktor-io/api/ktor-io.api
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public final class io/ktor/utils/io/ByteReadChannelOperationsKt {
public static final fun readBuffer (Lio/ktor/utils/io/ByteReadChannel;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun readByte (Lio/ktor/utils/io/ByteReadChannel;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun readByteArray (Lio/ktor/utils/io/ByteReadChannel;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun readDouble (Lio/ktor/utils/io/ByteReadChannel;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun readFloat (Lio/ktor/utils/io/ByteReadChannel;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun readFully (Lio/ktor/utils/io/ByteReadChannel;[BIILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun readFully$default (Lio/ktor/utils/io/ByteReadChannel;[BIILkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun readInt (Lio/ktor/utils/io/ByteReadChannel;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down Expand Up @@ -143,6 +145,8 @@ public final class io/ktor/utils/io/ByteWriteChannelOperationsKt {
public static final fun writeBuffer (Lio/ktor/utils/io/ByteWriteChannel;Lkotlinx/io/RawSource;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun writeByte (Lio/ktor/utils/io/ByteWriteChannel;BLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun writeByteArray (Lio/ktor/utils/io/ByteWriteChannel;[BLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun writeDouble (Lio/ktor/utils/io/ByteWriteChannel;DLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun writeFloat (Lio/ktor/utils/io/ByteWriteChannel;FLkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun writeFully (Lio/ktor/utils/io/ByteWriteChannel;[BIILkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun writeFully$default (Lio/ktor/utils/io/ByteWriteChannel;[BIILkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun writeInt (Lio/ktor/utils/io/ByteWriteChannel;ILkotlin/coroutines/Continuation;)Ljava/lang/Object;
Expand Down
4 changes: 4 additions & 0 deletions ktor-io/api/ktor-io.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readBuffer
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readBuffer(kotlin/Int): kotlinx.io/Buffer // io.ktor.utils.io/readBuffer|[email protected](kotlin.Int){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readByte(): kotlin/Byte // io.ktor.utils.io/readByte|[email protected](){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readByteArray(kotlin/Int): kotlin/ByteArray // io.ktor.utils.io/readByteArray|[email protected](kotlin.Int){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readDouble(): kotlin/Double // io.ktor.utils.io/readDouble|[email protected](){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readFloat(): kotlin/Float // io.ktor.utils.io/readFloat|[email protected](){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readFully(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...) // io.ktor.utils.io/readFully|[email protected](kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readInt(): kotlin/Int // io.ktor.utils.io/readInt|[email protected](){}[0]
final suspend fun (io.ktor.utils.io/ByteReadChannel).io.ktor.utils.io/readLong(): kotlin/Long // io.ktor.utils.io/readLong|[email protected](){}[0]
Expand All @@ -447,6 +449,8 @@ final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/write(kot
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeBuffer(kotlinx.io/RawSource) // io.ktor.utils.io/writeBuffer|[email protected](kotlinx.io.RawSource){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeByte(kotlin/Byte) // io.ktor.utils.io/writeByte|[email protected](kotlin.Byte){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeByteArray(kotlin/ByteArray) // io.ktor.utils.io/writeByteArray|[email protected](kotlin.ByteArray){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeDouble(kotlin/Double) // io.ktor.utils.io/writeDouble|[email protected](kotlin.Double){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeFloat(kotlin/Float) // io.ktor.utils.io/writeFloat|[email protected](kotlin.Float){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeFully(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...) // io.ktor.utils.io/writeFully|[email protected](kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeInt(kotlin/Int) // io.ktor.utils.io/writeInt|[email protected](kotlin.Int){}[0]
final suspend fun (io.ktor.utils.io/ByteWriteChannel).io.ktor.utils.io/writeLong(kotlin/Long) // io.ktor.utils.io/writeLong|[email protected](kotlin.Long){}[0]
Expand Down
18 changes: 18 additions & 0 deletions ktor-io/common/src/io/ktor/utils/io/ByteReadChannelOperations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,30 @@ public suspend fun ByteReadChannel.readInt(): Int {
return readBuffer.readInt()
}

/**
* Reads a 32-bit floating-point value from the current [ByteReadChannel].
*/
@OptIn(InternalAPI::class)
public suspend fun ByteReadChannel.readFloat(): Float {
awaitUntilReadable(Float.SIZE_BYTES)
return readBuffer.readFloat()
}

@OptIn(InternalAPI::class)
public suspend fun ByteReadChannel.readLong(): Long {
awaitUntilReadable(Long.SIZE_BYTES)
return readBuffer.readLong()
}

/**
* Reads a 32-bit floating-point value from the current [ByteReadChannel].
*/
@OptIn(InternalAPI::class)
public suspend fun ByteReadChannel.readDouble(): Double {
awaitUntilReadable(Double.SIZE_BYTES)
return readBuffer.readDouble()
}

private suspend fun ByteReadChannel.awaitUntilReadable(numberOfBytes: Int) {
while (availableForRead < numberOfBytes && awaitContent(numberOfBytes)) {
yield()
Expand Down
22 changes: 22 additions & 0 deletions ktor-io/common/src/io/ktor/utils/io/ByteWriteChannelOperations.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ public suspend fun ByteWriteChannel.writeInt(value: Int) {
flushIfNeeded()
}

/**
* Writes a 32-bit floating-point value to the current [ByteWriteChannel].
*
* @param value The floating-point value to be written to the channel.
*/
@OptIn(InternalAPI::class)
public suspend fun ByteWriteChannel.writeFloat(value: Float) {
writeBuffer.writeFloat(value)
flushIfNeeded()
}

/**
* Writes a 64-bit floating-point value to the current [ByteWriteChannel].
*
* @param value The floating-point value to be written to the channel.
*/
@OptIn(InternalAPI::class)
public suspend fun ByteWriteChannel.writeDouble(value: Double) {
writeBuffer.writeDouble(value)
flushIfNeeded()
}

@OptIn(InternalAPI::class)
public suspend fun ByteWriteChannel.writeLong(value: Long) {
writeBuffer.writeLong(value)
Expand Down

0 comments on commit b693e9d

Please sign in to comment.