readbytes!

readbytes!(stream, b::Vector{UInt8}, nb=length(b); all=true)

Read at most nb bytes from the stream into b, returning the number of bytes read (increasing the size of b as needed).

See readbytes for a description of the all option.

Examples

In the Julia programming language, the function readbytes!(stream, b::Vector{UInt8}, nb=length(b); all=true)

Read at most nb bytes from the stream into b, modifying b in-place, and return the number of bytes read. The size of b will be increased as needed to accommodate the read bytes.

julia> io = IOBuffer("Hello, world!")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=13, maxsize=Inf, ptr=1, mark=-1)

julia> bytes = Vector{UInt8}(undef, 5)
5-element Array{UInt8,1}:
 0x00
 0x00
 0x00
 0x00
 0x00

julia> readbytes!(io, bytes, 5)
5

julia> bytes
5-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f

Here are some common examples of its use:

  1. Read bytes from an IOStream:

    julia> io = IOBuffer("Hello, Julia!")
    IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=13, maxsize=Inf, ptr=1, mark=-1)
    
    julia> bytes = Vector{UInt8}(undef, 7)
    7-element Array{UInt8,1}:
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    
    julia> readbytes!(io, bytes, 7)
    7
    
    julia> bytes
    7-element Array{UInt8,1}:
    0x48
    0x65
    0x6c
    0x6c
    0x6f
    0x20
    0x4a
  2. Read a specific number of bytes from a file:

    julia> file = open("data.bin", "r")
    IOStream(<file data.bin>)
    
    julia> bytes = Vector{UInt8}(undef, 10)
    10-element Array{UInt8,1}:
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    0x00
    
    julia> readbytes!(file, bytes, 10)
    10
    
    julia> bytes
    10-element Array{UInt8,1}:
    0x01
    0x02
    0x03
    0x04
    0x05
    0x06
    0x07
    0x08
    0x09
    0x0a
    
    julia> close(file)

Common mistake example:

julia> io = IOBuffer("Hello, Julia!")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=13, maxsize=Inf, ptr=1, mark=-1)

julia> bytes = Vector{UInt8}(undef, 10)
10-element Array{UInt8,1}:
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00

julia> readbytes!(io, bytes, 15)
ERROR: MethodError: no method matching readbytes!(::IOBuffer, ::Array{UInt8,1}, ::Int64)

In this example, the nb argument is set to a value larger than the available bytes in the stream. It's important to ensure that nb is within the valid range to avoid such errors.

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.

*Required Field
Details

Checking you are not a robot: