Add Stream wrappers around Java's stream classes
All checks were successful
PRs & Pushes / test (push) Successful in 3m6s
PRs & Pushes / build-jars (push) Successful in 3m5s
PRs & Pushes / build-apidoc (push) Successful in 3m20s

This commit is contained in:
JeremyStar™ 2024-12-27 23:53:38 +01:00
parent 14cf028bfc
commit 7b0b231a55
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
3 changed files with 239 additions and 0 deletions

View file

@ -0,0 +1,72 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* Licensed under the GNU General Public License v3.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.engine.base.implementation.stream
import de.staropensource.engine.base.annotation.NonKotlinContact
import de.staropensource.engine.base.exception.io.IOAccessException
import de.staropensource.engine.base.implementable.stream.ReadStream
import de.staropensource.engine.base.implementable.stream.Stream
import java.io.InputStream
/**
* A [Stream] implementation forwarding
* it's calls instances of Java's
* [InputStream] class.
*
* @param inputStream Java's [InputStream] to read from
* @param autoCloseJavaStream if to automatically close the supplied [InputStream] if [close] is invoked
* @since v1-alpha10
*/
@NonKotlinContact
open class JavaReadStream(
val inputStream: InputStream,
val autoCloseJavaStream: Boolean = false
) : ReadStream() {
// -----> Closure
/**
* Closes the Java's [InputStream]
* if [autoCloseJavaStream] is `true`.
*
* @throws IOAccessException on IO error
* @since v1-alpha10
*/
override fun close() = super.close()
override fun closeStream() {
if (!autoCloseJavaStream)
return
inputStream.close()
}
// -----> Reading
override fun remaining(): Boolean? = null
override fun available(): UInt = inputStream.available().toUInt()
override fun readNextByte(): Byte? {
val byte: Int = inputStream.read()
return if (byte == -1)
null
else
internalPipe(byte.toByte())
}
override fun readNBytes(n: UInt): ByteArray = internalPipe(inputStream.readNBytes(n.toInt()))
override fun readRemainingBytes(): ByteArray = internalPipe(inputStream.readAllBytes())
}

View file

@ -0,0 +1,88 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* Licensed under the GNU General Public License v3.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.engine.base.implementation.stream
import de.staropensource.engine.base.annotation.NonKotlinContact
import de.staropensource.engine.base.exception.io.IOAccessException
import de.staropensource.engine.base.implementable.stream.Stream
import java.io.InputStream
import java.io.OutputStream
/**
* A [Stream] implementation forwarding
* it's calls instances of Java's
* [InputStream] and [OutputStream]
* classes.
*
* @param inputStream Java's [InputStream] to read from
* @param outputStream Java's [OutputStream] to write to
* @param autoCloseJavaStreams if to automatically close the supplied [InputStream] and [OutputStream] if [close] is invoked
* @since v1-alpha10
*/
@NonKotlinContact
@OptIn(ExperimentalStdlibApi::class)
open class JavaStream(
val inputStream: InputStream,
val outputStream: OutputStream,
val autoCloseJavaStreams: Boolean = true
) : Stream(streamMode = StreamMode.READ_WRITE) {
// -----> Closure
/**
* Closes the Java's [InputStream]
* and [OutputStream] if
* [autoCloseJavaStreams] is `true`.
*
* @throws IOAccessException on IO error
* @since v1-alpha10
*/
override fun close() = super.close()
override fun closeStream() {
if (!autoCloseJavaStreams)
return
inputStream.close()
outputStream.close()
}
// -----> Writing
override fun writeByte(byte: Byte): Stream {
outputStream.write(byte.toInt())
return this
}
override fun writeBytes(bytes: ByteArray): Stream {
outputStream.write(bytes)
return this
}
override fun flush(): Stream {
outputStream.flush()
return this
}
// -----> Reading
override fun remaining(): Boolean? = null
override fun available(): UInt = inputStream.available().toUInt()
override fun readNextByte(): Byte? = inputStream.read().toByte()
override fun readNBytes(n: UInt): ByteArray = inputStream.readNBytes(n.toInt())
override fun readRemainingBytes(): ByteArray = inputStream.readAllBytes()
}

View file

@ -0,0 +1,79 @@
/*
* STAROPENSOURCE ENGINE SOURCE FILE
* Copyright (c) 2024 The StarOpenSource Engine Authors
* Licensed under the GNU General Public License v3.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.staropensource.engine.base.implementation.stream
import de.staropensource.engine.base.annotation.NonKotlinContact
import de.staropensource.engine.base.exception.io.IOAccessException
import de.staropensource.engine.base.implementable.stream.Stream
import de.staropensource.engine.base.implementable.stream.WriteStream
import java.io.OutputStream
/**
* A [Stream] implementation forwarding
* it's calls instances of Java's
* [OutputStream] class.
*
* @param outputStream Java's [OutputStream] to write to
* @param autoCloseJavaStream if to automatically close the supplied [OutputStream] if [close] is invoked
* @param autoFlushAfter after which amount of bytes to automatically flush. May be ignored by some implementations. Implementations should expose this argument in their constructor
* @since v1-alpha10
*/
@NonKotlinContact
@OptIn(ExperimentalStdlibApi::class)
open class JavaWriteStream(
val outputStream: OutputStream,
val autoCloseJavaStream: Boolean = false,
autoFlushAfter: ULong = 100UL,
) : WriteStream(
autoFlushAfter = autoFlushAfter,
) {
// -----> Closure
/**
* Closes the Java's [OutputStream]
* if [autoCloseJavaStream] is `true`.
*
* @throws IOAccessException on IO error
* @since v1-alpha10
*/
override fun close() = super.close()
override fun closeStream() {
if (!autoCloseJavaStream)
return
outputStream.close()
}
// -----> Writing
override fun writeByte(byte: Byte): Stream {
outputStream.write(internalRedirect(byte).toInt())
return this
}
override fun writeBytes(bytes: ByteArray): Stream {
outputStream.write(internalRedirect(bytes))
return this
}
override fun flush(): Stream {
outputStream.flush()
return this
}
}