Skip to content

Latest commit

 

History

History
121 lines (98 loc) · 6.8 KB

README.md

File metadata and controls

121 lines (98 loc) · 6.8 KB

Kotlin Multiplatform IO Library

This library is a port of JDK's IO classes to Kotlin Multiplatform (KMP). It allows you to work with common Java-style IO operations on Kotlin code that runs across multiple platforms, including JVM, Android, iOS, and more.

Kotlin License Maven Central

badge-jvm badge-android badge-ios badge-mac badge-tvos badge-tvos badge-linux badge-windows badge-js badge-wasm

Features

This library is split into modules, each covering specific sets of classes to support Java-style IO in Kotlin Multiplatform. Key features include:

Note

This library does not include APIs for direct file reading and writing but provides comprehensive support for InputStream operations derived from strings or byte arrays. You can extend this functionality using kotlinx-io or okio, which enables seamless file operations with Source and InputStream conversions. Please check the extension modules

IO Core Module (com.fleeksoft.io:io-core)

Provides foundational classes for buffer and charset management.

  • Buffer: Base container for data, compatible with java.nio.Buffer.
  • ByteBuffer: Buffer specifically for byte data.
  • CharBuffer: Buffer for character data.
  • CharBufferFactory: Utility for creating CharBuffer and ByteBuffer instances.
  • Closeable: Interface for resources that require closing after usage.
  • Readable: Interface for sources that can be read into a CharBuffer.

Main IO Module (com.fleeksoft.io:io)

Includes IO core classes for reading character and byte streams.

  • Reader: Abstract class for reading character streams.
  • InputStream: Abstract class for reading raw byte streams.
  • ByteArrayInputStream: Creates an InputStream from a ByteArray.
  • InputStreamReader: Bridges byte streams to character streams.
  • BufferedReader: Efficient reader that wraps character streams.
  • StringReader: Reads from a string as if it were a character stream.
  • CharArrayReader: Reads characters from an array.
  • FilterInputStream and FilterReader: Wraps InputStream and Reader for additional functionality.
  • PushbackReader: Enables pushing back characters into the stream for re-reading.
  • Charset: Encodes and decodes text with support from com.fleeksoft.charset.
    • Extensions Functions: InputStream and Reader extension functions:
      • String.byteInputStream(charset: Charset = Charsets.UTF8): ByteArrayInputStream
      • ByteArray.inputStream(): ByteArrayInputStream
      • ByteArray.inputStream(off: Int, len: Int): ByteArrayInputStream
      • InputStream.reader(charset: Charset = Charsets.UTF8): InputStreamReader
      • Reader.buffered(bufferSize: Int = Constants.DEFAULT_BYTE_BUFFER_SIZE): BufferedReader
      • InputStream.bufferedReader(charset: Charset = Charsets.UTF8): BufferedReader
      • Reader.forEachLine(action: (String) -> Unit): Unit
      • Reader.readLines(): List<String>
      • String.reader(): StringReader
      • BufferedReader.lineSequence(): Sequence<String>
      • BufferedReader.readString(count: Int): String

URI Module (com.fleeksoft.io:uri)

Provides a port of java.net.URI for Kotlin Multiplatform.

  • URI: A class representing a Uniform Resource Identifier (URI) reference, as defined by RFC 2396 and updated by RFC 2732. This class provides constructors and methods for manipulating URIs, parsing components, resolving relative URIs, and normalizing URIs.
  • URIFactory: Utility class for creating URI instances. Instead of using URI.create(str), you should use URIFactory.create(str).

kotlinx-io Integration (com.fleeksoft.io:kotlinx-io)

Provides seamless interoperation with kotlinx-io, extending its functionality with InputStream and Source conversions.

  • Source.asInputStream(): InputStream: Converts a Source into an InputStream.
  • RawSource.asInputStream(): InputStream: Converts a RawSource into an InputStream.
  • InputStream.asSource(): RawSource: Converts an InputStream into a RawSource.

Okio Integration (com.fleeksoft.io:okio)

Provides seamless interoperation with Okio, extending its functionality with InputStream and Source conversions.

  • Source.asInputStream(): InputStream: Converts a Source into an InputStream.
  • InputStream.asSource(): Source: Converts an InputStream into a Source.

Charset Modules

  • Standard charsets: com.fleeksoft.charset:charset:<version>
  • Extended charsets: com.fleeksoft.charset:charset-ext:<version>

Check here for more info

Installation

Latest Version

Maven Central

To integrate this library into your Kotlin Multiplatform project, add the relevant dependencies in your build.gradle.kts:

commonMain.dependencies {
  implementation("com.fleeksoft.io:io-core:<version>")
  implementation("com.fleeksoft.io:io:<version>")
  // Optional: kotlinx-io integration
  implementation("com.fleeksoft.io:kotlinx-io:<version>")
}

Usage Examples

Here’s a basic usage example:

val str = "Hello, World!"
val byteArray = ByteArray(10)
val charArray = CharArray(10)

val byteArrayInputStream: ByteArrayInputStream = str.byteInputStream()
val stringReader: StringReader = str.reader()

val byteArrayInputStream2 = byteArray.inputStream()

val bufferedReader: BufferedReader = stringReader.buffered()
val bufferedReader2: BufferedReader = byteArrayInputStream.bufferedReader()

val byteBuffer: ByteBuffer = ByteBufferFactory.wrap(byteArray)
val charBuffer: CharBuffer = CharBufferFactory.wrap(charArray)

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any enhancements or bug fixes.

License

This project is licensed under the Apache License 2.0. See the LICENSE file for more details.