Skip to content

Commit 1b483b3

Browse files
author
Sergey Mashkov
committed
Fix tryPeek implementation to not consume byte
1 parent 07f0007 commit 1b483b3

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

kotlinx-io-js/src/main/kotlin/kotlinx/io/core/IoBufferJS.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,6 @@ actual class IoBuffer internal constructor(
826826
val writePosition = writePosition
827827
if (readPosition == writePosition) return -1
828828

829-
this.readPosition = readPosition + 1
830829
return i8[readPosition].toInt() and 0xff
831830
}
832831

kotlinx-io-jvm/src/main/kotlin/kotlinx/io/core/IoBufferJVM.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,12 @@ actual class IoBuffer private constructor(
659659
}
660660
}
661661

662+
/*
663+
* Returns next byte (unsigned) or `-1` if no more bytes available
664+
*/
662665
actual final override fun tryPeek(): Int {
663666
val bb = readBuffer
664-
return if (bb.hasRemaining()) bb.get().toInt() and 0xff else -1
667+
return if (bb.hasRemaining()) bb.get(bb.position()).toInt() and 0xff else -1
665668
}
666669

667670
actual final override fun peekTo(buffer: IoBuffer): Int {

kotlinx-io-native/src/main/kotlin/kotlinx/io/core/IoBufferNative.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ actual class IoBuffer internal constructor(
907907
val writePosition = writePosition
908908
if (readPosition == writePosition) return -1
909909

910-
this.readPosition = readPosition + 1
911910
return content[readPosition].toInt() and 0xff
912911
}
913912

src/test/kotlin/kotlinx/io/tests/BytePacketReadTest.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ class BytePacketReadTest {
231231
writeByte(2)
232232
}.use { pkt ->
233233
assertEquals(1, pkt.tryPeek())
234+
pkt.discardExact(1)
234235
assertEquals(2, pkt.tryPeek())
236+
pkt.discardExact(1)
235237
assertEquals(-1, pkt.tryPeek())
236238
}
237239

@@ -246,6 +248,7 @@ class BytePacketReadTest {
246248

247249
ByteReadPacket(segment1, pool).use { pkt ->
248250
assertEquals(1, pkt.tryPeek())
251+
pkt.discardExact(1)
249252
assertEquals(-1, pkt.tryPeek())
250253
}
251254
}
@@ -261,4 +264,4 @@ class BytePacketReadTest {
261264
}
262265
}
263266

264-
}
267+
}

0 commit comments

Comments
 (0)