Skip to content

Commit acf219d

Browse files
committed
Introduce and relax restriction
1 parent ba13a61 commit acf219d

File tree

6 files changed

+36
-15
lines changed

6 files changed

+36
-15
lines changed

TODO.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33
- Input: bypass source exceptions in `Input.fill`
44
- Input: `eof` vs `exhausted`
55
- Output: Appendable
6-
- `Input.discard()`, discardExact
76
- Idempotent close
87
- Make `fill` public (and then rename it) to enable zero-copy delegates
98
- Implementation
10-
- Input
11-
- Output
12-
- Index preconditions
13-
- `bytesInput = buildInput { BytesOutput -> }`
14-
- `Bytes.createInput()` copy on consume
15-
- Introduce `BytesInput` and `BytesOutput` `(Input/Output + size, remaining)`
16-
- Introduce `readByteArray(), copyTo(output)`
17-
- Introduce `Input.copyAvailableTo(Output | Buffer)`: consume single buffer(wait if no buffer available)
9+
- Index preconditions
1810
- Prototype `PipedOutput`
1911
- Test
2012
- Verify pool has no leaks

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ internal fun checkSize(size: Int) {
3131
internal fun checkCount(count: Int) {
3232
require(count >= 0) { "Count ($count) should be positive." }
3333
}
34+
35+
internal fun unexpectedEOF(text: String): Nothing = throw EOFException(text)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,10 @@ public abstract class Input : Closeable {
267267

268268
/**
269269
* Skips [count] bytes from input.
270+
*
271+
* @return skipped bytes count. It could be less than [count] if no more bytes available.
270272
*/
271-
public fun discard(count: Int) {
273+
public fun discard(count: Int): Int {
272274
checkCount(count)
273275

274276
var remaining = count
@@ -279,11 +281,13 @@ public abstract class Input : Closeable {
279281
}
280282

281283
if (skipCount == 0) {
282-
throw EOFException("Fail to skip $count bytes, remaining $remaining.")
284+
break
283285
}
284286

285287
remaining -= skipCount
286288
}
289+
290+
return count - remaining
287291
}
288292

289293
/**

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ public fun Input.readByteArray(array: UByteArray, startIndex: Int = 0, length: I
130130
readByteArray(array.asByteArray(), startIndex, length)
131131
}
132132

133+
/**
134+
* Discards exact [count] of bytes.
135+
*
136+
* @throws [EOFException] if this input ends before discard finished.
137+
* @return discarded bytes count
138+
*/
139+
public fun Input.discardExact(count: Int): Int {
140+
checkCount(count)
141+
142+
val skipped = discard(count)
143+
if (skipped < count) {
144+
unexpectedEOF("Fail to skip $count bytes, remaining ${count - skipped}.")
145+
}
146+
147+
return count
148+
}
149+
150+
133151
/**
134152
* Reads a [Byte] from this Input.
135153
*

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@ class InputOutputTest {
6969
}
7070

7171
@Test
72-
fun testDiscardOnEmpty() {
72+
fun testDiscardExactOnEmpty() {
7373
assertFails {
74-
EmptyInput.discard(1)
74+
EmptyInput.discardExact(1)
7575
}
7676
}
7777

78+
@Test
79+
fun testDiscardOnEmpty() {
80+
assertEquals(0, EmptyInput.discard(1))
81+
}
82+
7883
@Test
7984
fun testPrefetchOnEmpty() {
8085
assertFalse { EmptyInput.prefetch(1) }
@@ -90,7 +95,7 @@ class InputOutputTest {
9095
checkException { ErrorInput.readByte() }
9196
checkException { ErrorInput.preview { } }
9297
checkException { ErrorInput.prefetch(1) }
93-
checkException { ErrorInput.discard(1) }
98+
checkException { ErrorInput.discardExact(1) }
9499
checkException { ErrorInput.eof() }
95100
checkException {
96101
ErrorInput.readAvailableTo(

core/commonTest/src/kotlinx/io/bytes/BytesTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class BytesTest {
128128
buildBytes {
129129
writeByteArray(ByteArray(99999))
130130
}.useInput {
131-
discard(99999)
131+
discardExact(99999)
132132
assertTrue { eof() }
133133
}
134134

0 commit comments

Comments
 (0)