Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 9ab810d

Browse files
committed
Introduced Binary interface and modified BytesTest to use it.
1 parent bf99b37 commit 9ab810d

File tree

8 files changed

+53
-31
lines changed

8 files changed

+53
-31
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
buildscript {
22
repositories {
3+
mavenLocal()
34
jcenter()
45
gradlePluginPortal()
56
maven { url "https://dl.bintray.com/kotlin/kotlin-eap" }
@@ -16,7 +17,7 @@ buildscript {
1617
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
1718
classpath "org.jetbrains.kotlinx:kotlinx.benchmark.gradle:$benchmarks_version"
1819
classpath "com.vanniktech:gradle-android-junit-jacoco-plugin:0.15.0"
19-
classpath "scientifik:gradle-tools:0.2.4"
20+
classpath "scientifik:gradle-tools:0.2.5"
2021
}
2122
}
2223

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package kotlinx.io
2+
3+
typealias BinarySize = Int
4+
5+
/**
6+
* A generic representation of reusable input.
7+
*/
8+
interface Binary {
9+
/**
10+
* The size of the input in bytes or [INFINITE] in case the size could not be estimated
11+
*/
12+
val size: BinarySize
13+
14+
/**
15+
* Open the input, read and transform it to given result type then close it.
16+
* This method ensures input is closed properly in case of exception inside and prevents leaking of input.
17+
*/
18+
fun <R> read(reader: Input.() -> R): R
19+
20+
companion object {
21+
/**
22+
* Designates that the binary does not have fixed size, but instead is read until EOF
23+
*/
24+
val INFINITE = BinarySize.MAX_VALUE
25+
}
26+
}

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package kotlinx.io
22

3-
import kotlinx.io.buffer.*
4-
import kotlinx.io.pool.*
3+
import kotlinx.io.buffer.Buffer
4+
import kotlinx.io.pool.ObjectPool
55

66
internal typealias BytesPointer = Int
77

@@ -19,7 +19,7 @@ internal typealias BytesPointer = Int
1919
* 2. close
2020
* 3. example
2121
*/
22-
class Bytes internal constructor(internal val bufferPool: ObjectPool<Buffer>) : Closeable {
22+
class Bytes internal constructor(internal val bufferPool: ObjectPool<Buffer>) : Closeable, Binary {
2323
private var buffers: Array<Buffer?> = arrayOfNulls(initialPreviewSize)
2424
private var limits: IntArray = IntArray(initialPreviewSize)
2525
private var head: Int = 0
@@ -28,7 +28,7 @@ class Bytes internal constructor(internal val bufferPool: ObjectPool<Buffer>) :
2828
/**
2929
* Calculate size of [Bytes].
3030
*/
31-
fun size(): Int = size(StartPointer)
31+
override val size: BinarySize by lazy { size(StartPointer) }
3232

3333
/**
3434
* Create [Input] view on content.
@@ -38,6 +38,9 @@ class Bytes internal constructor(internal val bufferPool: ObjectPool<Buffer>) :
3838
override fun fill(buffer: Buffer): Int = 0
3939
}
4040

41+
@Suppress("OVERRIDE_BY_INLINE")
42+
override inline fun <R> read(reader: Input.() -> R): R = input().use(reader)
43+
4144
override fun toString() = "Bytes($head..$tail)"
4245

4346
/**

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BytesTest {
2626
writeUTF8String("OK\n")
2727
}
2828

29-
assertEquals(2 + 2 + 2 + 4 + 8 + 4 + 8 + 8 + 3, bytes.size())
29+
assertEquals(2 + 2 + 2 + 4 + 8 + 4 + 8 + 8 + 3, bytes.size)
3030

3131
val input = bytes.input()
3232
val ba = ByteArray(2)
@@ -63,7 +63,7 @@ class BytesTest {
6363
writeUTF8String("OK\n")
6464
val text = listOf(1, 2, 3).joinToString(separator = "|")
6565
writeUTF8String("$text\n")
66-
}.useInput {
66+
}.read {
6767
readArray(ByteArray(9999))
6868
assertEquals(0x12, readByte())
6969
assertEquals(0x1234, readShort())
@@ -93,7 +93,7 @@ class BytesTest {
9393
fun testSingleBufferSkip() {
9494
buildBytes {
9595
writeArray("ABC123\n".toByteArray0())
96-
}.useInput {
96+
}.read {
9797
readArray(ByteArray(3))
9898
assertEquals("123", readUTF8Line())
9999
assertTrue { eof() }
@@ -102,9 +102,9 @@ class BytesTest {
102102

103103
@Test
104104
fun testSingleBufferSkipExact() {
105-
val p = buildBytes {
105+
buildBytes {
106106
writeArray("ABC123".toByteArray0())
107-
}.useInput {
107+
}.read {
108108
readArray(ByteArray(3))
109109
assertEquals("123", readUTF8String(3))
110110
assertTrue { eof() }
@@ -116,7 +116,7 @@ class BytesTest {
116116
fun testSingleBufferSkipExactTooMuch() {
117117
buildBytes {
118118
writeArray("ABC123".toByteArray0())
119-
}.useInput {
119+
}.read {
120120
assertFailsWith<EOFException> {
121121
readArray(ByteArray(1000))
122122
}
@@ -131,7 +131,7 @@ class BytesTest {
131131
fun testMultiBufferSkipTooMuch() {
132132
buildBytes {
133133
writeArray(ByteArray(99999))
134-
}.useInput {
134+
}.read {
135135
assertTrue { eof() }
136136
}
137137

@@ -142,7 +142,7 @@ class BytesTest {
142142
buildBytes {
143143
writeArray(ByteArray(99999))
144144
writeArray("ABC123\n".toByteArray0())
145-
}.useInput {
145+
}.read {
146146
readArray(ByteArray(99999 + 3))
147147
assertEquals("123", readUTF8Line())
148148
assertTrue { eof() }
@@ -155,7 +155,7 @@ class BytesTest {
155155
repeat(PACKET_BUFFER_SIZE + 3) {
156156
writeByte(1)
157157
}
158-
}.useInput {
158+
}.read {
159159
readArray(ByteArray(PACKET_BUFFER_SIZE - 1))
160160
assertEquals(0x01010101, readInt())
161161
assertTrue { eof() }
@@ -168,7 +168,7 @@ class BytesTest {
168168
repeat(PACKET_BUFFER_SIZE + 1) {
169169
writeByte(1)
170170
}
171-
}.useInput {
171+
}.read {
172172
readArray(ByteArray(PACKET_BUFFER_SIZE - 1))
173173

174174
try {
@@ -182,15 +182,15 @@ class BytesTest {
182182
@Test
183183
fun testReadByteEmptyPacket() {
184184
assertFailsWith<EOFException> {
185-
buildBytes { }.useInput {
185+
buildBytes { }.read {
186186
readInt()
187187
}
188188
}
189189

190190
assertFailsWith<EOFException> {
191191
buildBytes {
192192
writeInt(1)
193-
}.useInput {
193+
}.read {
194194
readInt()
195195
readByte()
196196
}

core/commonTest/src/kotlinx/io/tests/TestUtils.kt

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open class OutputStringTest {
1515
writeUTF8String(text)
1616
}
1717

18-
assertEquals(expected.size, bytes.size(), "Size $size")
18+
assertEquals(expected.size, bytes.size, "Size $size")
1919
val input = bytes.input()
2020
val read = UByteArray(expected.size)
2121
input.readArray(read)
@@ -32,7 +32,7 @@ open class OutputStringTest {
3232
writeUTF8String(text)
3333
}
3434

35-
assertEquals(expected.size, bytes.size(), "Size $size")
35+
assertEquals(expected.size, bytes.size, "Size $size")
3636
val input = bytes.input()
3737
val read = UByteArray(expected.size)
3838
input.readArray(read)
@@ -58,7 +58,7 @@ open class OutputStringTest {
5858
writeUTF8String(text)
5959
}
6060

61-
assertEquals(expected.size, bytes.size(), "Size $size")
61+
assertEquals(expected.size, bytes.size, "Size $size")
6262

6363
val input = bytes.input()
6464
val read = UByteArray(expected.size)

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group=org.jetbrains.kotlinx
2-
version=0.2.0-npm-dev-1
2+
version=0.2.0-npm-dev-2
33
org.gradle.parallel=true
44

55
# kotlin

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pluginManagement {
22
repositories {
3+
mavenLocal()
34
mavenCentral()
45
jcenter()
56
gradlePluginPortal()
@@ -8,7 +9,7 @@ pluginManagement {
89
}
910
}
1011

11-
//enableFeaturePreview('GRADLE_METADATA')
12+
enableFeaturePreview('GRADLE_METADATA')
1213

1314
rootProject.name = 'kotlinx-io-package'
1415

0 commit comments

Comments
 (0)