Skip to content

Commit 3e736f3

Browse files
committed
Rename eof to exhauested. Add description about performance penalty
1 parent acf219d commit 3e736f3

File tree

11 files changed

+42
-41
lines changed

11 files changed

+42
-41
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,12 @@ public abstract class Input : Closeable {
209209

210210
/**
211211
* Check if at least one byte is available to read.
212+
*
213+
* This method can use [fill] if no bytes are available in the cache. Please consider using [readAvailableTo] in
214+
* performance-critical code.
212215
*/
213216
@Suppress("NOTHING_TO_INLINE")
214-
public inline fun eof(): Boolean = !prefetch(1)
217+
public inline fun exhausted(): Boolean = !prefetch(1)
215218

216219
/**
217220
* Checks that [size] bytes are fetched in [Input].

core/commonMain/src/kotlinx/io/text/TextInput.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public fun Input.readUtf8Lines(): List<String> {
2525
checkExhausted()
2626
val list = ArrayList<String>()
2727
use {
28-
while (!eof()) {
28+
while (!exhausted()) {
2929
list += readUtf8Line()
3030
}
3131
}
@@ -40,12 +40,12 @@ public fun Input.readUtf8Lines(): List<String> {
4040
*/
4141
public inline fun Input.forEachUtf8Line(action: (String) -> Unit) {
4242
use {
43-
while (!eof()) {
43+
while (!exhausted()) {
4444
action(readUtf8Line())
4545
}
4646
}
4747
}
4848

4949
private fun Input.checkExhausted() {
50-
if (eof()) throw EOFException("Unexpected EOF while reading UTF-8 line")
50+
if (exhausted()) throw EOFException("Unexpected EOF while reading UTF-8 line")
5151
}

core/commonMain/src/kotlinx/io/text/TextInputObsolete.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private inline fun Input.decodeUtf8Chars(consumer: (Char) -> Boolean): Int {
107107
var state = STATE_UTF_8
108108
var count = 0
109109

110-
while (state != STATE_FINISH && !eof()) {
110+
while (state != STATE_FINISH && !exhausted()) {
111111
readBufferRange { buffer, startOffset, endOffset ->
112112
for (offset in startOffset until endOffset) {
113113
val byte = buffer.loadByteAt(offset).toInt() and 0xff

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class InputOutputTest {
9696
checkException { ErrorInput.preview { } }
9797
checkException { ErrorInput.prefetch(1) }
9898
checkException { ErrorInput.discardExact(1) }
99-
checkException { ErrorInput.eof() }
99+
checkException { ErrorInput.exhausted() }
100100
checkException {
101101
ErrorInput.readAvailableTo(
102102
object : Output() {
@@ -124,7 +124,7 @@ class InputOutputTest {
124124

125125
val count = input.copyTo(output)
126126

127-
assertTrue(input.eof())
127+
assertTrue(input.exhausted())
128128
assertEquals(content.size, count)
129129

130130
assertArrayEquals(content, output.toByteArray())
@@ -138,7 +138,7 @@ class InputOutputTest {
138138

139139
val count = input.copyTo(output, size)
140140

141-
assertTrue(!input.eof())
141+
assertTrue(!input.exhausted())
142142
assertEquals(size, count)
143143

144144
assertArrayEquals(content.sliceArray(0 until size), output.toByteArray())

core/commonTest/src/kotlinx/io/InputPrimitivesTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class InputPrimitivesTest {
3030
assertReadLong(0x0001020304050607)
3131
assertReadLong(0x08090A0B0C0D0E0F)
3232
assertReadLong(0x1011121314151617)
33-
assertFalse(eof(), "EOF")
33+
assertFalse(exhausted(), "EOF")
3434
assertFails { readLong() }
3535
}
3636

@@ -39,7 +39,7 @@ class InputPrimitivesTest {
3939
assertReadULong(0x7071727374757677u)
4040
assertReadULong(0x78797a7b7c7d7e7fu)
4141
assertReadULong(0x8081828384858687u)
42-
assertFalse(eof(), "EOF")
42+
assertFalse(exhausted(), "EOF")
4343
assertFails { readULong() }
4444
}
4545

@@ -48,7 +48,7 @@ class InputPrimitivesTest {
4848
assertReadInt(0x00010203)
4949
assertReadInt(0x04050607)
5050
assertReadInt(0x08090a0b)
51-
assertFalse(eof(), "EOF")
51+
assertFalse(exhausted(), "EOF")
5252
assertFails { readInt() }
5353
}
5454

@@ -57,7 +57,7 @@ class InputPrimitivesTest {
5757
assertReadUInt(0x00010203u)
5858
assertReadUInt(0x04050607u)
5959
assertReadUInt(0x08090a0bu)
60-
assertFalse(eof(), "EOF")
60+
assertFalse(exhausted(), "EOF")
6161
assertFails { readInt() }
6262
}
6363

@@ -66,7 +66,7 @@ class InputPrimitivesTest {
6666
assertReadShort(0x0001)
6767
assertReadShort(0x0203)
6868
assertReadShort(0x0405)
69-
assertFalse(eof(), "EOF")
69+
assertFalse(exhausted(), "EOF")
7070
assertFails { readShort() }
7171
}
7272

@@ -75,7 +75,7 @@ class InputPrimitivesTest {
7575
assertReadUShort(0x0001u)
7676
assertReadUShort(0x0203u)
7777
assertReadUShort(0x0405u)
78-
assertFalse(eof(), "EOF")
78+
assertFalse(exhausted(), "EOF")
7979
assertFails { readShort() }
8080
}
8181

@@ -84,7 +84,7 @@ class InputPrimitivesTest {
8484
assertReadByte(0x0)
8585
assertReadByte(0x1)
8686
assertReadByte(0x2)
87-
assertTrue(eof(), "EOF")
87+
assertTrue(exhausted(), "EOF")
8888
assertFails { readByte() }
8989
}
9090

@@ -93,7 +93,7 @@ class InputPrimitivesTest {
9393
assertReadUByte(0x0u)
9494
assertReadUByte(0x1u)
9595
assertReadUByte(0x2u)
96-
assertTrue(eof(), "EOF")
96+
assertTrue(exhausted(), "EOF")
9797
assertFails { readUByte() }
9898
}
9999

core/commonTest/src/kotlinx/io/LimitingInputTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package kotlinx.io
22

3-
import kotlinx.io.*
43
import kotlinx.io.buffer.*
54
import kotlinx.io.text.*
65
import kotlin.test.*
@@ -30,7 +29,7 @@ class LimitingInputTest {
3029
@Test
3130
fun testZeroLimit() {
3231
val input = StringInput("long\nlong\nline").limit(0)
33-
assertTrue(input.eof())
32+
assertTrue(input.exhausted())
3433
}
3534

3635
@Test

core/commonTest/src/kotlinx/io/OutputTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class OutputTest {
1515
writeInt(0x00010203)
1616
}
1717
bytes.input().apply {
18-
assertFalse(eof())
18+
assertFalse(exhausted())
1919
assertReadLong(0x0001020304050607)
2020
assertReadLong(0x08090A0B0C0D0E0F)
2121
assertReadInt(0x08090A0B)
2222
assertReadInt(0x00010203)
23-
assertTrue(eof())
23+
assertTrue(exhausted())
2424
}
2525
}
2626

@@ -34,13 +34,13 @@ class OutputTest {
3434
writeInt(0xDEAD) // by writing unit tests
3535
}
3636
bytes.input().apply {
37-
assertFalse(eof())
37+
assertFalse(exhausted())
3838
assertReadByte(0xFF.toByte())
3939
assertReadInt(0x08090A0B)
4040
assertReadInt(0x00010203)
4141
assertReadInt(0xAB023F3)
4242
assertReadInt(0xDEAD)
43-
assertTrue(eof())
43+
assertTrue(exhausted())
4444
}
4545
}
4646

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class BytesTest {
7575

7676
assertEquals("OK", readUtf8Line())
7777
assertEquals("1|2|3", readUtf8Line())
78-
assertTrue { eof() }
78+
assertTrue { exhausted() }
7979
}
8080
}
8181

@@ -85,7 +85,7 @@ class BytesTest {
8585
writeByteArray(ByteArray(9999))
8686
}.useInput {
8787
readByteArray(ByteArray(9999))
88-
assertTrue { eof() }
88+
assertTrue { exhausted() }
8989
}
9090
}
9191

@@ -96,7 +96,7 @@ class BytesTest {
9696
}.useInput {
9797
readByteArray(ByteArray(3))
9898
assertEquals("123", readUtf8Line())
99-
assertTrue { eof() }
99+
assertTrue { exhausted() }
100100
}
101101
}
102102

@@ -107,7 +107,7 @@ class BytesTest {
107107
}.useInput {
108108
readByteArray(ByteArray(3))
109109
assertEquals("123", readUtf8String(3))
110-
assertTrue { eof() }
110+
assertTrue { exhausted() }
111111
}
112112
}
113113

@@ -119,7 +119,7 @@ class BytesTest {
119119
assertFailsWith<EOFException> {
120120
readByteArray(ByteArray(1000))
121121
}
122-
assertTrue { eof() }
122+
assertTrue { exhausted() }
123123
}
124124
}
125125

@@ -129,7 +129,7 @@ class BytesTest {
129129
writeByteArray(ByteArray(99999))
130130
}.useInput {
131131
discardExact(99999)
132-
assertTrue { eof() }
132+
assertTrue { exhausted() }
133133
}
134134

135135
}
@@ -142,7 +142,7 @@ class BytesTest {
142142
}.useInput {
143143
readByteArray(ByteArray(99999 + 3))
144144
assertEquals("123", readUtf8Line())
145-
assertTrue { eof() }
145+
assertTrue { exhausted() }
146146
}
147147
}
148148

@@ -155,7 +155,7 @@ class BytesTest {
155155
}.useInput {
156156
readByteArray(ByteArray(PACKET_BUFFER_SIZE - 1))
157157
assertEquals(0x01010101, readInt())
158-
assertTrue { eof() }
158+
assertTrue { exhausted() }
159159
}
160160
}
161161

core/commonTest/src/kotlinx/io/text/LinesTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package kotlinx.io.text
22

33
import kotlinx.io.*
44
import kotlinx.io.buffer.*
5-
import kotlinx.io.text.*
65
import kotlin.test.*
76

87
class LinesTest {
@@ -53,7 +52,7 @@ class LinesTest {
5352
private fun testLineByLine(data: ByteArray, expectedLines: List<String>) {
5453
val input = ByteArrayInput(data)
5554
val result = ArrayList<String>()
56-
while (!input.eof()) {
55+
while (!input.exhausted()) {
5756
result += input.readUtf8Line()
5857
}
5958
assertEquals(expectedLines, result)

core/commonTest/src/kotlinx/io/text/OutputStringTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ open class OutputStringTest {
1919
val input = bytes.input()
2020
val read = UByteArray(expected.size)
2121
input.readByteArray(read)
22-
assertTrue(input.eof(), "EOF")
22+
assertTrue(input.exhausted(), "EOF")
2323
assertEquals(expected.contentToString(), read.contentToString())
2424
}
2525

@@ -36,7 +36,7 @@ open class OutputStringTest {
3636
val input = bytes.input()
3737
val read = UByteArray(expected.size)
3838
input.readByteArray(read)
39-
assertTrue(input.eof(), "EOF")
39+
assertTrue(input.exhausted(), "EOF")
4040
assertEquals(expected.contentToString(), read.contentToString())
4141
}
4242

@@ -65,7 +65,7 @@ open class OutputStringTest {
6565
val input = bytes.input()
6666
val read = UByteArray(expected.size)
6767
input.readByteArray(read)
68-
assertTrue(input.eof(), "EOF")
68+
assertTrue(input.exhausted(), "EOF")
6969
assertEquals(expected.contentToString(), read.contentToString())
7070
}
7171

@@ -76,7 +76,7 @@ open class OutputStringTest {
7676
}.input()
7777

7878
assertEquals("ABC\u0422", input.readUtf8String(4))
79-
assertTrue(input.eof(), "EOF")
79+
assertTrue(input.exhausted(), "EOF")
8080
}
8181

8282
@Test
@@ -87,7 +87,7 @@ open class OutputStringTest {
8787

8888
try {
8989
assertEquals("1", input.readUtf8String(1))
90-
assertTrue(input.eof(), "EOF")
90+
assertTrue(input.exhausted(), "EOF")
9191
} finally {
9292
input.close()
9393
}
@@ -106,7 +106,7 @@ open class OutputStringTest {
106106
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
107107
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
108108
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
109-
assertTrue(input.eof(), "EOF")
109+
assertTrue(input.exhausted(), "EOF")
110110
assertEquals("1234", sb.toString())
111111
assertEquals(listOf(1, 2, 0, 1), counts)
112112
}
@@ -123,7 +123,7 @@ open class OutputStringTest {
123123
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
124124
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
125125
counts.add(input.readUtf8StringUntilDelimitersTo(sb, "|,."))
126-
assertTrue(input.eof(), "EOF")
126+
assertTrue(input.exhausted(), "EOF")
127127
assertEquals("\u0422\u0423\u0424", sb.toString())
128128
assertEquals(listOf(1, 1, 1), counts)
129129
}
@@ -138,7 +138,7 @@ open class OutputStringTest {
138138
assertEquals("22", input.readUtf8Line())
139139
assertEquals("333", input.readUtf8Line())
140140
assertEquals("4444", input.readUtf8Line())
141-
assertTrue(input.eof(), "EOF")
141+
assertTrue(input.exhausted(), "EOF")
142142
}
143143
}
144144

playground/jvmMain/src/kotlinx/io/gzip/GzipInput.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import java.util.zip.*
88
class GzipInput(private val original: Input) : Input() {
99
private val inputStream = object : InputStream() {
1010
override fun read(): Int {
11-
if (original.eof()) return -1
11+
if (original.exhausted()) return -1
1212
return original.readByte().toInt()
1313
}
1414
}

0 commit comments

Comments
 (0)