iostream.blob.resize
Resizes the blob to the given size
from "iostream" import blob
let cargo = blob(2)
cargo.resize(5)
println("cargo.len() =", cargo.len())Output:
cargo.len() = 5Parameters
size | int | the new length, in bytes |
Errors
Throws resize failed, cur size=<n>, requested=<size> when size is negative.
Notes
Every call reallocates the buffer, even to shrink it. Shrinking and then growing back does not recover the bytes that were cut off: the regrown tail comes back zero-filled, not with its old contents.
If the cursor (see tell) is beyond the new, smaller length, it is pulled back to size.
Example
from "iostream" import blob
let b = blob(4)
b.writen(0x11223344, 'i')
b.seek(0)
println("bytes before resize =", $"{b[0]},{b[1]},{b[2]},{b[3]}")
// shrinking then growing back does not bring the old tail back
b.resize(1)
b.resize(4)
println("bytes after resize(1) then resize(4) =", $"{b[0]},{b[1]},{b[2]},{b[3]}")
try {
b.resize(-1)
} catch (e) {
println("b.resize(-1) throws:", e)
}Output:
bytes before resize = 68,51,34,17
bytes after resize(1) then resize(4) = 68,0,0,0
b.resize(-1) throws: resize failed, cur size=4, requested=-1See also
constructor | Creates a blob of the given size (default 0) |
len | Returns the stream length |
seek | Seeks to the given offset; origin is 'b' (begin), 'c' (current) or 'e' (end) |
| blob | class index |