ByteStream, File & Network IO
ByteStream
For input / output of binary data.
Writing
out.close()out.write(Byte[])out.flush()writes the data buffer (theostreamuser-level cache) to the operating system.- This protects against data loss in the event of a program crash.
out.flushAndSync()callsflush(), then- calls
fsync()to write the kernel buffers to the file system and then to the hard disk/SSD (the write cache should be written/cleared, too). - This protects against data loss in the event of a program or system crash.
- calls
Reading
in.read() -> Byte[]reads- everything from the
istreamuser-level cache, if not0,
otherwise everything from the kernel buffer/cache:- With pipes/sockets this is everything currently in the kernel pipe/socket buffer (typically 64 KB).
- Blocks when this buffer is empty.
- When the pipe/socket is closed (and no data is buffered anymore), then it returns an empty array.
- With files this is everything currently in the kernel “read ahead” cache (typically 64 to 256 KB).
- Blocks when this cache is empty.
- When the end of file is reached (and no data is cached anymore), then it returns an empty array.
- With pipes/sockets this is everything currently in the kernel pipe/socket buffer (typically 64 KB).
- everything from the
in.read(Int n) -> Byte[]reads exactly n bytes.- Blocks until the given number of bytes are read.
- Throws an exception if end of file is reached (or pipe/socket closed) before n bytes are read.
in.read(Int minimum, maximum) -> Byte[]reads everything that is currently available, up to the givenmaximumnumber of bytes.- Blocks until (at least) the
minimumnumber of bytes are read (may return immediately with an empty array whenminimumis0). in.read(minimum..maximum) -> Byte[]
- Blocks until (at least) the
in.readAll() -> Byte[]reads everything until the end of the stream.- With pipes/sockets, it blocks until the pipe/socket is closed.
in.readInto(Span<Byte> buffer, Int minimum = 1)reads into the given buffer.- Blocks until (at least) the
minimumnumber of bytes are read (may return immediately with an empty array whenminimumis0). - Throws an exception if end of file reached (or pipe/socket closed) before
minimumbytes are read. - The effective
maximumif defined bybuffer.size().- You may limit the maximum number of bytes to read by using
buffer.subspan(0, 4096), or configure the starting point (in the buffer) by usingbuffer.subspan(100).
- You may limit the maximum number of bytes to read by using
- Usually more efficient, as the buffer is reused and less allocations are necessary.
- Blocks until (at least) the
in.available() -> Intsays how many bytes are immediately available for reading.- Returns the size of the
istreamcache, if not 0,
otherwise reports the size of the kernel cache/buffer. - As that is the number of bytes you would get with the next
in.read().
- Returns the size of the
in.peek(Int n) -> Byte[]- Blocks until (at least) the given minimum (
n) number of bytes are read. - May throw an
ArgumentException("Unable to peek() more than ... bytes."). - TODO Limited to 16 bytes or to the buffer size?
- Blocks until (at least) the given minimum (
in.ignore(Int n)ignores/discards n bytes from the input stream.in.ignoreAll()ignores/discards everything that is currently in the input stream.in.atEnd()returnsTrueif- the end of the file is reached (or the pipe/socket is closed),
- and no data is buffered anymore (neither in the
istreamuser-level cache, nor in the kernel cache/buffer).
File IO
File, derived from ByteStream.
file.size() -> Intfile.position() -> Intfile.setPosition(Int n)(AKA)file.seekFromStart()- A common position for read and write.
file.seek(Int offsetToCurrentPos)offsetToCurrentPoscan be positive (moving towards the end) or negative (moving towards the beginning).
file.seekFromEnd(Int distanceToEnd)distanceToEndis0or positive (here moving from the end towards the beginning).
file.truncate()truncates the file at the current position.file.truncateAt(Int n)truncates the file at the given position.
file.path() -> String
Network & Device IO
Note
This is not necessarily part of the core language. I’m just thinking about what a good API might look like.
NetworkConnection, derived fromByteStream,- a base class for TCP/IP, Bluetooth RFCOMM, infrared, …
connection.connect(...)connection.disconnect()connection.isConnected() -> Boolconnection.remoteAddress() -> Stringconnection.localAddress() -> Stringfor finding out which interface (WLAN, LAN, VPN) the connection is actually running on.connection.readTimeout() -> Durationconnection.setReadTimeout(Duration)
TcpConnection, derived fromNetworkConnectionconnection.shutdownWrite()sends FIN (half-close), allows further reading.connection.connectionTimeout() -> Durationconnection.setConnectionTimeout(Duration)
connection.remotePort() -> UInt16connection.localPort() -> UInt16connection.noDelay() -> Boolconnection.setNoDelay(Bool disableNagle)to disable the Nagle algorithm.
connection.keepAlive() -> Boolconnection.setKeepAlive(Bool)prevents connection termination due to inactivity.
connection.protocolVersion() -> Intreturns4or6.connection.receiveBufferSize() -> Intconnection.setReceiveBufferSize(Int bytes)
connection.sendBufferSize() -> Intconnection.setSendBufferSize(Int bytes)
LocalConnection, derived fromByteStreamconnection.path() -> Stringreturns the file system path (for Unix sockets) or the name (for pipes).connection.peerCredentials() -> Stringreturns the process ID (PID) or user ID of the other party.- TODO Move to
UnixDomainSocket? But on Windows this info is available for pipes, too.
- TODO Move to
SerialConnection(RS-232/UART)setBaudRate(Int)setParity(Parity)setDataBits(Int)
Class Hierarchy
ByteStreamFileMemoryStreamas RAM buffer.NetworkConnectionTcpConnectionTlsConnection/SslConnection
SshConnection
LocalConnectionfor interprocess communication.PipeUnixDomainConnectionin stream configuration.
BluetoothRfcommConnectionBluetooth RFCOMMDeviceConnectionSerialConnectionfor RS-232/UART.UsbConnectionfor USB bulk transfers.
MessageChannelfor message/packet/frame/datagram-based protocols (i.e. not only a stream of bytes).UdpSocketfor UDP over IP.UnixDomainSocketin datagram configuration.- Communication with sensors on microcontrollers
I2CDevice(register read/write cycles)SpiDevice(chip-select-controlled frames)CanBusNode
BluetoothL2CapConnectionBluetooth L2CAPZigbeeEndpointWebSocketConnection(message frames over TCP)