read(stream, t)
read(stream, type)
Read a value of the given type from a stream, in canonical binary representation.
Examples
-
Read a single value from a stream:
julia> io = IOBuffer([0x41, 0x42, 0x43]); julia> read(io, UInt8) 0x41This example reads a single
UInt8value from theIOBufferio. -
Read multiple values from a stream into an array:
julia> io = IOBuffer([0x41, 0x42, 0x43, 0x44, 0x45, 0x46]); julia> read(io, UInt8, (2, 3)) 2×3 Matrix{UInt8}: 0x41 0x43 0x45 0x42 0x44 0x46This example reads a 2x3 matrix of
UInt8values from theIOBufferio. - Read values into a dynamically sized array:
julia> io = IOBuffer([0x41, 0x42, 0x43, 0x44]); julia> dims = (2, 2); julia> read(io, UInt8, dims) 2×2 Matrix{UInt8}: 0x41 0x43 0x42 0x44Here, we specify the dimensions
dimsas a tuple and read the values into a dynamically sized matrix.
Common mistake example:
julia> io = IOBuffer([0x41, 0x42, 0x43]);
julia> read(io, UInt16)
ERROR: ArgumentError: Cannot read 2 bytes from a 1-byte stream
In this example, the type provided (UInt16) requires reading 2 bytes, but the stream (IOBuffer) has only 1 byte remaining. It's important to ensure that the provided type aligns with the expected size of the data in the stream.
See Also
deserialize, eachline, eof, fd, flush, IOBuffer, ismarked, isopen, isreadonly, mark, nb_available, open, pipeline, position, read, read!, readavailable, readbytes, readbytes!, readline, redirect_stderr, redirect_stdin, reset, seek, seekend, seekstart, serialize, skip, skipchars, TextDisplay, unmark, write, writemime,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.