Skip to content

Commit ba13a61

Browse files
committed
Fix readAvailableTo indexes; Add test
1 parent cf4346d commit ba13a61

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

core/commonMain/src/kotlinx/io/Input.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ public abstract class Input : Closeable {
151151
* If no bytes are available in the input, [fill] method will be called directly on
152152
* the [destination] buffer without an extra copy.
153153
* Otherwise, available bytes are copied to the destination.
154+
*
155+
* @return number of bytes written in the [destination].
154156
*/
155157
public fun readAvailableTo(
156158
destination: Buffer,
157159
startIndex: Int = 0,
158-
endIndex: Int = destination.size - startIndex
160+
endIndex: Int = destination.size
159161
): Int {
160162
checkBufferAndIndexes(destination, startIndex, endIndex)
161163

core/commonTest/src/kotlinx/io/InputOutputTest.common.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@file:Suppress("FORBIDDEN_IDENTITY_EQUALS")
12
package kotlinx.io
23

34
import kotlinx.io.buffer.*
@@ -138,6 +139,26 @@ class InputOutputTest {
138139
assertArrayEquals(content.sliceArray(0 until size), output.toByteArray())
139140
}
140141

142+
@Test
143+
fun testReadAvailableToRange() {
144+
var executed = false
145+
val input: Input = object : Input() {
146+
override fun fill(buffer: Buffer, startIndex: Int, endIndex: Int): Int {
147+
assertEquals(1024, endIndex)
148+
executed = true
149+
return endIndex - startIndex
150+
}
151+
152+
override fun closeSource() {
153+
}
154+
155+
}
156+
val buffer = bufferOf(ByteArray(1024))
157+
val end = input.readAvailableTo(buffer, 1)
158+
assertTrue(executed)
159+
assertEquals(1023, end)
160+
}
161+
141162
private fun checkException(block: () -> Unit) {
142163
var fail = false
143164
try {

0 commit comments

Comments
 (0)