From 683582386075aa1903141b220b02f597a96a796b Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Thu, 26 Dec 2024 00:37:31 +0100 Subject: [PATCH] Remove nullStream, add stdin, stdout & stderr --- .../base/implementable/stream/Stream.kt | 94 ++++++++++++++++++- .../base/implementation/stream/NullStream.kt | 3 +- 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt index 4015fa0..26bbcab 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementable/stream/Stream.kt @@ -21,7 +21,6 @@ package de.staropensource.engine.base.implementable.stream import de.staropensource.engine.base.annotation.NonKotlinContact import de.staropensource.engine.base.exception.io.IOAccessException -import de.staropensource.engine.base.implementation.stream.NullStream import java.io.IOException import java.io.InputStream import java.io.OutputStream @@ -40,11 +39,96 @@ abstract class Stream(val streamMode: StreamMode) : AutoCloseable { */ companion object { /** - * Returns a [Stream] which does nothing. + * Contains the standard input as a [Stream]. + * It cannot be closed. * + * @see stdin(3) * @since v1-alpha10 */ - val nullStream: Stream = NullStream.instance + val standardInput: ReadStream = object : ReadStream() { + /** + * Contains the Java [InputStream] + * provided by [System.in]. + * + * @since v1-alpha10 + */ + val stream: InputStream = System.`in` + + // -----> Closure + override fun close() = Unit + override fun closeStream() = Unit + + + // -----> Reading + override fun remaining(): Boolean? = null + override fun available(): UInt = stream.available().toUInt() + override fun readNextByte(): Byte? = stream.read().toByte() + override fun readNBytes(n: UInt): ByteArray = stream.readNBytes(n.toInt()) + override fun readRemainingBytes(): ByteArray = stream.readAllBytes() + } + + /** + * Contains the standard output as a [Stream]. + * It cannot be closed. + * + * @see stdout(3) + * @since v1-alpha10 + */ + val standardOutput: WriteStream = object : WriteStream() { + /** + * Contains the Java [OutputStream] + * provided by [System.out]. + * + * @since v1-alpha10 + */ + val stream: OutputStream = System.out + + // -----> Closure + override fun close() = Unit + override fun closeStream() = Unit + + + // -----> Writing + override fun writeByte(byte: Byte): Stream { + stream.write(byte.toInt()) + return this + } + override fun writeBytes(bytes: ByteArray): Stream { + stream.write(bytes) + return this + } + } + + /** + * Contains the standard error as a [Stream]. + * + * @see stderr(3) + * @since v1-alpha10 + */ + val standardError: WriteStream = object : WriteStream() { + /** + * Contains the Java [OutputStream] + * provided by [System.err]. + * + * @since v1-alpha10 + */ + val stream: OutputStream = System.err + + // -----> Closure + override fun close() = Unit + override fun closeStream() = Unit + + + // -----> Writing + override fun writeByte(byte: Byte): Stream { + stream.write(byte.toInt()) + return this + } + override fun writeBytes(bytes: ByteArray): Stream { + stream.write(bytes) + return this + } + } } @@ -58,7 +142,7 @@ abstract class Stream(val streamMode: StreamMode) : AutoCloseable { private set /** - * Contains the [java.io.InputStream] + * Contains the Java [InputStream] * for this [Stream] instance. * * @since v1-alpha10 @@ -66,7 +150,7 @@ abstract class Stream(val streamMode: StreamMode) : AutoCloseable { private var inputStream: InputFileStream? = null /** - * Contains the [java.io.OutputStream] + * Contains the Java [OutputStream] * for this [Stream] instance. * * @since v1-alpha10 diff --git a/base/src/main/kotlin/de/staropensource/engine/base/implementation/stream/NullStream.kt b/base/src/main/kotlin/de/staropensource/engine/base/implementation/stream/NullStream.kt index 05dccff..a15c41c 100644 --- a/base/src/main/kotlin/de/staropensource/engine/base/implementation/stream/NullStream.kt +++ b/base/src/main/kotlin/de/staropensource/engine/base/implementation/stream/NullStream.kt @@ -22,7 +22,8 @@ package de.staropensource.engine.base.implementation.stream import de.staropensource.engine.base.implementable.stream.Stream /** - * A [Stream] which does nothing. + * A [Stream] which does nothing + * and cannot even be closed. * * @since v1-alpha10 */