Skip to content

Commit 03eb9ba

Browse files
authored
Provide Okio adapters (#420)
Created a new `kotlinx-io-okio` module that adapts: - `kotlinx.io.RawSource` to `okio.Source` and vice versa; - `kotlinx.io.RawSink` to `okio.Sink` and vice versa; - `kotlinx.io.bytestring.ByteString` to `okio.ByteString` and vice versa. The module will be published only for targets supported by Okio. Closes #178
1 parent a9325d9 commit 03eb9ba

File tree

28 files changed

+1003
-13
lines changed

28 files changed

+1003
-13
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ which includes the `FileSystem` interface and its default implementation - `Syst
2828

2929
`FileSystem` provides basic operations for working with files and directories, which are represented by yet another class under the same package - `Path`.
3030

31-
There are two `kotlinx-io` modules:
31+
There are several `kotlinx-io` modules:
3232
- [kotlinx-io-bytestring](./bytestring) - provides `ByteString`.
3333
- [kotlinx-io-core](./core) - provides IO primitives (`Buffer`, `Source`, `Sink`), filesystems support, depends on `kotlinx-io-bytestring`.
34+
- [kotlinx-io-okio](./integration/okio) - bridges `kotlinx-io` and `Okio` `ByteString`, `kotlinx.io.RawSource` and `okio.Source`, `kotlinx.io.RawSink` and `okio.Sink`.
3435

3536
## Using in your projects
3637

@@ -81,6 +82,7 @@ Add the library to dependencies:
8182
On JVM, `kotlinx-io` supports Java Modules:
8283
- `kotlinx-io-bytestring` library provides `kotlinx.io.bytestring` module;
8384
- `kotlinx-io-core` library provides `kotlinx.io.core` module.
85+
- `kotlinx-io-okio` library provides `kotlinx.io.okio` module.
8486

8587
Read [this](https://kotlinlang.org/docs/gradle-configure-project.html#configure-with-java-modules-jpms-enabled) article
8688
for details on how to configure a Gradle project to utilize JPMS.

build-logic/src/main/kotlin/kotlinx/io/conventions/kotlinx-io-multiplatform.gradle.kts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ fun KotlinSourceSet.configureSourceSet() {
145145
}
146146

147147
private fun KotlinMultiplatformExtension.nativeTargets() {
148+
val configureAllTargets = project.findProperty("kotlinx.io.okio.compat.targets")?.toString()?.toBoolean() != true
149+
148150
iosX64()
149151
iosArm64()
150152
iosSimulatorArm64()
@@ -159,15 +161,19 @@ private fun KotlinMultiplatformExtension.nativeTargets() {
159161
watchosSimulatorArm64()
160162
watchosDeviceArm64()
161163

162-
androidNativeArm32()
163-
androidNativeArm64()
164-
androidNativeX64()
165-
androidNativeX86()
164+
if (configureAllTargets) {
165+
androidNativeArm32()
166+
androidNativeArm64()
167+
androidNativeX64()
168+
androidNativeX86()
169+
}
166170

167171
linuxX64()
168172
linuxArm64()
169-
@Suppress("DEPRECATION") // https://github.com/Kotlin/kotlinx-io/issues/303
170-
linuxArm32Hfp()
173+
if (configureAllTargets) {
174+
@Suppress("DEPRECATION") // https://github.com/Kotlin/kotlinx-io/issues/303
175+
linuxArm32Hfp()
176+
}
171177

172178
macosX64()
173179
macosArm64()

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ apiValidation {
3333
dependencies {
3434
kover(project(":kotlinx-io-core"))
3535
kover(project(":kotlinx-io-bytestring"))
36+
kover(project(":kotlinx-io-okio"))
3637
}
3738

3839
kover {

core/api/kotlinx-io-core.klib.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ open class kotlinx.io.files/FileNotFoundException : kotlinx.io/IOException { //
197197
open class kotlinx.io/EOFException : kotlinx.io/IOException { // kotlinx.io/EOFException|null[0]
198198
constructor <init>() // kotlinx.io/EOFException.<init>|<init>(){}[0]
199199
constructor <init>(kotlin/String?) // kotlinx.io/EOFException.<init>|<init>(kotlin.String?){}[0]
200+
constructor <init>(kotlin/String?, kotlin/Throwable?) // kotlinx.io/EOFException.<init>|<init>(kotlin.String?;kotlin.Throwable?){}[0]
200201
}
201202

202203
open class kotlinx.io/IOException : kotlin/Exception { // kotlinx.io/IOException|null[0]

core/js/src/-PlatformJs.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
44
*/
55

@@ -19,6 +19,8 @@ public actual open class EOFException : IOException {
1919
public actual constructor() : super()
2020

2121
public actual constructor(message: String?) : super(message)
22+
23+
public constructor(message: String?, cause: Throwable?) : super(message, cause)
2224
}
2325

2426
internal actual fun withCaughtException(block: () -> Unit): Throwable? {

core/native/src/-NonJvmPlatform.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2023 JetBrains s.r.o. and respective authors and developers.
2+
* Copyright 2017-2024 JetBrains s.r.o. and respective authors and developers.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENCE file.
44
*/
55

@@ -34,4 +34,6 @@ public actual open class EOFException : IOException {
3434
public actual constructor() : super()
3535

3636
public actual constructor(message: String?) : super(message)
37+
38+
public constructor(message: String?, cause: Throwable?) : super(message, cause)
3739
}

core/wasm/src/-PlatformWasm.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
2+
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
33
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
44
*/
55

@@ -19,4 +19,6 @@ public actual open class EOFException : IOException {
1919
public actual constructor() : super()
2020

2121
public actual constructor(message: String?) : super(message)
22+
23+
public constructor(message: String?, cause: Throwable?) : super(message, cause)
2224
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ benchmark = "0.4.11"
99
jmh = "1.37"
1010
coroutines = "1.7.3"
1111
animalsniffer = "1.7.1"
12+
okio = "3.9.1"
1213

1314
[libraries]
1415

@@ -17,6 +18,7 @@ kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-p
1718
dokka-gradle-plugin = { group = "org.jetbrains.dokka", name = "dokka-gradle-plugin", version.ref = "dokka" }
1819
kotlinx-coroutines-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutines" }
1920
animalsniffer-gradle-plugin = { group = "ru.vyarus", name = "gradle-animalsniffer-plugin", version.ref = "animalsniffer" }
21+
okio = { group = "com.squareup.okio", name = "okio", version.ref = "okio" }
2022

2123
[plugins]
2224

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Module kotlinx-io-okio
2+
3+
The module bridges `Okio` `3.x` interfaces with `kotlinx-io` and vice versa.
4+
5+
[okio.Source] could be wrapped into [kotlinx.io.RawSource]
6+
using [kotlinx.io.okio.asKotlinxIoRawSource], and [kotlinx.io.RawSource]
7+
could be wrapped into [okio.Source] using [kotlinx.io.okio.asOkioSource].
8+
9+
Similar pair of functions is also defined for [okio.Sink]
10+
and [kotlinx.io.RawSink]: [kotlinx.io.okio.asKotlinxIoRawSink], [kotlinx.io.okio.asOkioSink].
11+
12+
Wrappers translate IO-exceptions thrown from an underlying library into exceptions provided by a wrapping library.
13+
I.e, if an [okio.Source] wrapped into [kotlinx.io.RawSource]
14+
throws [okio.IOException]
15+
during [kotlinx.io.RawSource.readAtMostTo] (or any other) call, that exception will be caught and a [kotlinx.io.IOException] will be thrown.
16+
The same translation is performed in the opposite direction as well.
17+
18+
`ByteString` classes provided by both libraries could also be converted one into another: [kotlinx.io.okio.toKotlinxIoByteString],
19+
[kotlinx.io.okio.toOkioByteString].
20+
21+
# Package kotlinx.io.okio
22+
23+
`kotlinx-io` <-> `Okio` adapters.
24+
25+
#### Thread-safety guarantees
26+
27+
Until stated otherwise, types and functions provided by the library are not thread safe.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public final class kotlinx/io/okio/OkioAdaptersKt {
2+
public static final fun asKotlinxIoRawSink (Lokio/Sink;)Lkotlinx/io/RawSink;
3+
public static final fun asKotlinxIoRawSource (Lokio/Source;)Lkotlinx/io/RawSource;
4+
public static final fun asOkioSink (Lkotlinx/io/RawSink;)Lokio/Sink;
5+
public static final fun asOkioSource (Lkotlinx/io/RawSource;)Lokio/Source;
6+
public static final fun toKotlinxIoByteString (Lokio/ByteString;)Lkotlinx/io/bytestring/ByteString;
7+
public static final fun toOkioByteString (Lkotlinx/io/bytestring/ByteString;)Lokio/ByteString;
8+
}
9+

0 commit comments

Comments
 (0)