get-bytevector-n!, read-bytevector! - read bytes from a port into a bytevector
(import (rnrs)) ;R6RS
(import (rnrs io ports)) ;R6RS
(import (scheme base)) ;R7RS
(get-bytevector-n! binary-input-port bytevector start count)
(read-bytevector! bytevector binary-input-port)
(read-bytevector! bytevector binary-input-port start)
(read-bytevector! bytevector binary-input-port start end)
blocking as necessary, until
bytes are available from
or until an end of file is reached. The return value depends on the
result of the read operation:
If count bytes are available before an end of file, they are
starting at index
and the result is
If fewer bytes are available
before the next end of file, the available bytes are written
starting at index
and the result is a number object representing the number of bytes
If an end of file is reached before any bytes are available, an
end-of-file object is returned.
The input port is updated to point just past the bytes read.
The procedure call
(read-bytevector! bv port start end)
procedure is analogous to
(get-bytevector-n! port bv start (- end start)).
defaults to the object returned by
defaults to 0, and
defaults to the length of
- Multiple end-of-file conditions
The port may return an end-of-file object and then still keep
producing data if another read is attempted. Files do not normally
work this way, but it is possible for other types of ports to do so,
such as terminals. R6RS explicitly mentions this situation while R7RS
Returns a single value; an exact nonnegative integer, or an
There is a single, unique end-of-file object.
;; Apologies for the nonsensical example.
(call-with-port (open-file-input-port "/dev/zero")
(let ((bv (make-bytevector 4 1)))
(get-bytevector-n! p bv 1 1)
=> #vu8(1 0 1 1)
This procedure is suitable when when you need to read binary data
directly into a buffer, which tends to be the case for some types of
binary structures. Other times it is more appropriate to use
which will automatically give you a bytevector limited to the
length of the data returned by the read operation.
This procedure can raise exceptions with the following condition types:
- &assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain.
must be exact, nonnegative integer objects,
must be a mutable bytevector with at least
- &i/o-read (R6RS)
There was an I/O error during the read operation.
- &i/o-port (R6RS)
This condition specifies the port object related to an
The assertions described above are errors.
Implementations may signal an error, extend the procedure's
domain of definition to include such arguments,
or fail catastrophically.
Reports prior to R6RS did not support binary I/O.
This page is part of the
It includes materials from the RnRS documents.
More information can be found at
The default port used by
is the current input port, which is normally a textual port that is
not expected to support binary I/O.
Markup created by unroff 1.0sc, March 04, 2023.