scoop:scoop.platform:0.1.0
src/io/native_io.scoop
Selected production source · src/io/native_io.scoop
1
package scoop.platform.io3
import scoop.platform.memory.NativeByteBuffer4
import scoop.platform.fs.*5
import scoop.platform.system.*7
internal fun readFileHandle(8
fd: NativeFileHandle,9
buffer: NativeByteBuffer,10
count: Long,11
context: OsString? = None12
): Long {13
requireTransfer(count, buffer.writableBytes)14
if (count == 0L)15
return 0L16
@Unsafe17
{18
return buffer.withWritableBytes {19
pointer,20
_ -> @Unsafe21
{22
return@withWritableBytes nativeFileRead(fd, pointer!!, count).checked(23
"read",24
context25
)26
}27
}28
}29
}31
internal fun writeFileHandle(32
fd: NativeFileHandle,33
buffer: NativeByteBuffer,34
count: Long,35
context: OsString? = None36
): Long {37
requireTransfer(count, buffer.readableBytes)38
if (count == 0L)39
return 0L40
@Unsafe41
{42
val written =43
buffer.withReadableBytes {44
pointer,45
_ -> @Unsafe46
{47
return@withReadableBytes nativeFileWrite(fd, pointer!!, count).checked(48
"write",49
context50
)51
}52
}53
if (written == 0L)54
throw WriteZeroException()55
buffer.skip(written)56
return written57
}58
}