Scoop API

scoop:scoop.platform:0.1.0

src/io/standard.scoop

Selected production source · src/io/standard.scoop

1package scoop.platform.io
3import scoop.platform.fs.*
4import scoop.platform.memory.NativeByteBuffer
6private class StandardSource: ByteSource {
7 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long {
8 requireTransfer(count, buffer.writableBytes)
9 if (count == 0L)
10 return 0L
11 return readFileHandle(standardHandle(0), buffer, count)
12 }
15private class StandardSink(private val stream: Int): ByteSink {
16 public override fun writeFrom(buffer: NativeByteBuffer, count: Long): Long {
17 requireTransfer(count, buffer.readableBytes)
18 if (count == 0L)
19 return 0L
20 return writeFileHandle(standardHandle(stream), buffer, count)
21 }
22 public override fun flush() {}
25/**
26 * A borrowed view of the process standard input stream; it does not expose ownership of the
27 * process descriptor.
28 */
29public val standardInput: ByteSource = StandardSource()
31/**
32 * A borrowed view of the process standard output stream; it does not expose ownership of the
33 * process descriptor.
34 */
35public val standardOutput: ByteSink = StandardSink(1)
37/**
38 * A borrowed view of the process standard error stream; it does not expose ownership of the
39 * process descriptor.
40 */
41public val standardError: ByteSink = StandardSink(2)
43/**
44 * Returns a separately owned duplicate of standard input, which the caller must close.
45 */
46public fun duplicateStandardInput(): File = File.duplicateHandle(standardHandle(0))
48/**
49 * Returns a separately owned duplicate of standard output, which the caller must close.
50 */
51public fun duplicateStandardOutput(): File = File.duplicateHandle(standardHandle(1))
53/**
54 * Returns a separately owned duplicate of standard error, which the caller must close.
55 */
56public fun duplicateStandardError(): File = File.duplicateHandle(standardHandle(2))
58private fun standardHandle(stream: Int): NativeFileHandle {
59 @Unsafe
60 {
61 return nativeFileStandard(stream).checked("standard stream")
62 }