Scoop API

scoop:scoop.platform:0.1.0

src/io/pipe.scoop

Selected production source · src/io/pipe.scoop

1package scoop.platform.io
3import scoop.platform.fs.*
4import scoop.platform.system.failOperatingSystem
5import scoop.platform.memory.NativeByteBuffer
7/**
8 * The owned read side of a native pipe.
9 */
10public class PipeSource internal constructor(internal val file: File): ByteSource, Closeable {
11 /**
12 * Whether this owned resource has been closed.
13 */
14 public val isClosed: Boolean get() = file.isClosed
15 /**
16 * Reads into the selected writable region, returning actual bytes and permitting short reads.
17 * Zero for a positive request denotes EOF.
18 */
19 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long =
20 file.readInto(buffer, count)
21 /**
22 * Closes the owned native descriptor; subsequent I/O through this owner is invalid.
23 */
24 public override fun close() {
25 file.close()
26 }
27 internal fun borrowed(): ByteSource = BorrowedSource(file)
30/**
31 * The owned write side of a native pipe.
32 */
33public class PipeSink internal constructor(internal val file: File): ByteSink, Closeable {
34 /**
35 * Whether this owned resource has been closed.
36 */
37 public val isClosed: Boolean get() = file.isClosed
38 /**
39 * Consumes the selected readable region, returning the actual accepted byte count and
40 * permitting short writes.
41 */
42 public override fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =
43 file.writeFrom(buffer, count)
44 /**
45 * Checks the unbuffered pipe endpoint state; it does not wait for the reader to consume bytes.
46 */
47 public override fun flush() {
48 file.flush()
49 }
50 /**
51 * Closes the owned native descriptor; subsequent I/O through this owner is invalid.
52 */
53 public override fun close() {
54 file.close()
55 }
56 internal fun borrowed(): ByteSink = BorrowedSink(file)
59private class BorrowedSource(private val file: File): ByteSource {
60 public override fun readInto(buffer: NativeByteBuffer, count: Long): Long =
61 file.readInto(buffer, count)
64private class BorrowedSink(private val file: File): ByteSink {
65 public override fun writeFrom(buffer: NativeByteBuffer, count: Long): Long =
66 file.writeFrom(buffer, count)
67 public override fun flush() {
68 file.flush()
69 }
72/**
73 * A native pipe with independently owned read and write endpoints.
74 */
75public class Pipe private constructor(
76 /**
77 * The read endpoint, which the caller must close.
78 */
79 public val source: PipeSource,
80 /**
81 * The write endpoint, which the caller must close.
82 */
83 public val sink: PipeSink
84) {
85 /**
86 * Creates native pipes with separate endpoint ownership.
87 */
88 public companion object {
89 /**
90 * Creates a pipe and returns both owned endpoints.
91 */
92 public fun open(): Pipe {
93 val pipe = Pipe(PipeSource(File.newOwner()), PipeSink(File.newOwner()))
94 @Unsafe
95 {
96 val opened = nativePipe()
97 if (opened.error != 0)
98 failOperatingSystem("create pipe", opened.error)
99 pipe.source.file.adoptHandle(opened.source)
100 pipe.sink.file.adoptHandle(opened.sink)
101 }
102 return pipe
103 }
104 }
107@CLayout
108private struct NativePipeOpened(
109 val source: NativeFileHandle,
110 val sink: NativeFileHandle,
111 val error: Int
114@Extern(name = "scoop_platform_handle_pipe")
115private fun nativePipe(): NativePipeOpened