R6RS binary input ports can be created with the procedures described below.
Return an input port whose contents are drawn from bytevector bv (see Bytevectors).
The transcoder argument is currently not supported.
Return a new custom binary input port14 named id (a
string) whose input is drained by invoking read! and passing it a
bytevector, an index where bytes should be written, and the number of
bytes to read. The
read! procedure must return an integer
indicating the number of bytes read, or
0 to indicate the
Optionally, if get-position is not
#f, it must be a thunk
that will be called when
port-position is invoked on the custom
binary port and should return an integer indicating the position within
the underlying data stream; if get-position was not supplied, the
returned port does not support
Likewise, if set-position! is not
#f, it should be a
one-argument procedure. When
set-port-position! is invoked on the
custom binary input port, set-position! is passed an integer
indicating the position of the next byte is to read.
Finally, if close is not
#f, it must be a thunk. It is
invoked when the custom binary input port is closed.
The returned port is fully buffered by default, but its buffering mode
can be changed using
Using a custom binary input port, the
procedure could be implemented as follows:
(define (open-bytevector-input-port source) (define position 0) (define length (bytevector-length source)) (define (read! bv start count) (let ((count (min count (- length position)))) (bytevector-copy! source position bv start count) (set! position (+ position count)) count)) (define (get-position) position) (define (set-position! new-position) (set! position new-position)) (make-custom-binary-input-port "the port" read! get-position set-position!)) (read (open-bytevector-input-port (string->utf8 "hello"))) ⇒ hello
Binary input is achieved using the procedures below:
Return an octet read from port, a binary input port, blocking as necessary, or the end-of-file object.
get-u8 but does not update port’s position to point
past the octet.
Read count octets from port, blocking as necessary and return a bytevector containing the octets read. If fewer bytes are available, a bytevector smaller than count is returned.
Read count bytes from port and store them in bv starting at index start. Return either the number of bytes actually read or the end-of-file object.
Read from port, blocking as necessary, until bytes are available or an end-of-file is reached. Return either the end-of-file object or a new bytevector containing some of the available bytes (at least one), and update the port position to point just past these bytes.
Read from port, blocking as necessary, until the end-of-file is reached. Return either a new bytevector containing the data read or the end-of-file object (if no data were available).
(ice-9 binary-ports) module provides the following procedure
as an extension to
(rnrs io ports):
Place the contents of bv in port, optionally starting at index start and limiting to count octets, so that its bytes will be read from left-to-right as the next bytes from port during subsequent read operations. If called multiple times, the unread bytes will be read again in last-in first-out order.