Quirrel4.41.0

iostream.blob.readblob

Method of blob, from module "iostream"
instance.readblob(size: int): instance

Reads up to size bytes and returns them as a blob

examples/iostream/blob/readblob-basic.nut
from "iostream" import blob

let cargo = blob(0)
cargo.writestring("ammo")
cargo.seek(0)
println("cargo.readblob(4).as_string() =", cargo.readblob(4).as_string())
Output:
cargo.readblob(4).as_string() = ammo

Parameters

sizeintthe largest number of bytes to read

Return value

See iostream.stream.readblob for the generic contract.

Errors

Throws no data left to read when the cursor is already at len and nothing can be read.

Notes

size is capped against len, not against what is left after the cursor, so asking for more than remains is not an error by itself: fewer bytes than requested come back in the result blob. Only asking with nothing left throws.

Example

examples/iostream/blob/readblob.nut
from "iostream" import blob

let b = blob(4)
b.writen(0x01020304, 'i')
b.seek(3)                 // only 1 byte remains before len

let got = b.readblob(4) // asking for more than remains is not an error
println("got.len() =", got.len())        // fewer bytes than asked for came back

try {
  b.readblob(1)            // now nothing at all remains
} catch (e) {
  println("b.readblob(1) throws:", e)
}
Output:
got.len() = 1
b.readblob(1) throws: no data left to read

See also

readblobReads up to size bytes and returns them as a blob
writeblobWrites the given blob and returns the number of bytes written
as_stringReturns the blob contents as a string
blobclass index