Quirrel4.41.0

iostream.stream.writeblob

Method of stream, from module "iostream"
instance.writeblob(blob: instance): int

Writes the given blob and returns the number of bytes written

examples/iostream/stream/writeblob-basic.nut
from "iostream" import blob

let src = blob(0)
src.writestring("gold")

let dst = blob(0)
println("dst.writeblob(src) =", dst.writeblob(src))
println("dst.as_string() =", dst.as_string())
Output:
dst.writeblob(src) = 4
dst.as_string() = gold

Parameters

blobinstancethe source blob; all of it is written

Return value

The number of bytes written, which is always blob.len().

Errors

Throws invalid parameter when blob is an instance but not a blob. Passing something that is not an instance, such as a number or a string, is rejected earlier by the declared parameter type and never reaches this message.

writeblob also throws io error if the underlying stream cannot store the full amount, for example a file opened read-only; a blob's own storage never refuses a write, so this case cannot happen on a blob.

Notes

The same method works on a file. Writing past the current len grows a blob to fit, the same as writen; writing past the end of a writable file extends it the same way.

Example

examples/iostream/stream/writeblob.nut
from "iostream" import blob

let src = blob(3)
src.writen(1, 'c')
src.writen(2, 'c')
src.writen(3, 'c')

let dst = blob(0)
println("dst.writeblob(src) =", dst.writeblob(src)) // returns the number of bytes written
println("dst.len() =", dst.len())

class Foo {}
try { dst.writeblob(Foo()) } catch (e) { println("dst.writeblob(Foo()) throws:", e) } // an instance, but not a blob
Output:
dst.writeblob(src) = 3
dst.len() = 3
dst.writeblob(Foo()) throws: invalid parameter

See also

writeblobWrites the given blob and returns the number of bytes written
readblobReads up to size bytes and returns them as a blob
writestringWrites the string and returns the number of characters written
streamclass index