← index

RandomAccessFile

inherits Object

Positioned reads over a file — pread-style, no cursor: every readAt:count: names its own offset, so reads are independent and there is no hidden position state. This is the substrate for random-access FORMATS (zip's central directory lives at the end of the file), where a sequential ByteStream is the wrong shape. Read-only. Together with size, this is the informal random-access read protocol — Bytes speaks it too (core/17-zip.qn), so code written against it reads a file or a byte buffer alike.

var out = [IO]File.create:'/tmp/ra-doc.bin'
out.writeAll:'0123456789'.asBytes
out.close
var f = ([IO]File.open:'/tmp/ra-doc.bin').randomAccess
(f.readAt:3 count:4).asString     "* -> 3456
f.size                            "* -> 10
f.close
[IO]File.delete:'/tmp/ra-doc.bin'

Instance methods

close

Close the file (idempotent). Further reads throw. Returns nil.

native

closed?

Whether the handle has been closed.

native

readAt:Integer count:Integer

Up to count bytes starting at byte offset — short only when the file ends first (an offset at or past the end answers empty Bytes). Reads park the task, not the scheduler; one read at a time per handle.

native

s

A short description: size and closed-ness.

native

size

The file's length in bytes, from the stat taken when the handle opened.

native