Scoop API

scoop:scoop.platform:0.1.0

src/io/native_io.scoop

Selected production source · src/io/native_io.scoop

1package scoop.platform.io
3import scoop.platform.memory.NativeByteBuffer
4import scoop.platform.fs.*
5import scoop.platform.system.*
7internal fun readFileHandle(
8 fd: NativeFileHandle,
9 buffer: NativeByteBuffer,
10 count: Long,
11 context: OsString? = None
12): Long {
13 requireTransfer(count, buffer.writableBytes)
14 if (count == 0L)
15 return 0L
16 @Unsafe
17 {
18 return buffer.withWritableBytes {
19 pointer,
20 _ -> @Unsafe
21 {
22 return@withWritableBytes nativeFileRead(fd, pointer!!, count).checked(
23 "read",
24 context
25 )
26 }
27 }
28 }
31internal fun writeFileHandle(
32 fd: NativeFileHandle,
33 buffer: NativeByteBuffer,
34 count: Long,
35 context: OsString? = None
36): Long {
37 requireTransfer(count, buffer.readableBytes)
38 if (count == 0L)
39 return 0L
40 @Unsafe
41 {
42 val written =
43 buffer.withReadableBytes {
44 pointer,
45 _ -> @Unsafe
46 {
47 return@withReadableBytes nativeFileWrite(fd, pointer!!, count).checked(
48 "write",
49 context
50 )
51 }
52 }
53 if (written == 0L)
54 throw WriteZeroException()
55 buffer.skip(written)
56 return written
57 }