6.12.2 Binary I/O

Guile’s ports are fundamentally binary in nature: at the lowest level, they work on bytes. This section describes Guile’s core binary I/O operations. See Textual I/O, for input and output of strings and characters.

To use these routines, first include the binary I/O module:

(use-modules (ice-9 binary-ports))

Note that although this module’s name suggests that binary ports are some different kind of port, that’s not the case: all ports in Guile are both binary and textual ports.

Scheme Procedure: get-u8 port
C Function: scm_get_u8 (port)

Return an octet read from port, an input port, blocking as necessary, or the end-of-file object.

Scheme Procedure: lookahead-u8 port
C Function: scm_lookahead_u8 (port)

Like get-u8 but does not update port’s position to point past the octet.

The end-of-file object is unlike any other kind of object: it’s not a pair, a symbol, or anything else. To check if a value is the end-of-file object, use the eof-object? predicate.

Scheme Procedure: eof-object? x
C Function: scm_eof_object_p (x)

Return #t if x is an end-of-file object, or #f otherwise.

Note that unlike other procedures in this module, eof-object? is defined in the default environment.

Scheme Procedure: get-bytevector-n port count
C Function: scm_get_bytevector_n (port, count)

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.

Scheme Procedure: get-bytevector-n! port bv start count
C Function: scm_get_bytevector_n_x (port, bv, start, count)

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.

Scheme Procedure: get-bytevector-some port
C Function: scm_get_bytevector_some (port)

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.

Scheme Procedure: get-bytevector-some! port bv start count
C Function: scm_get_bytevector_some_x (port, bv, start, count)

Read up to count bytes from port, blocking as necessary until at least one byte is available or an end-of-file is reached. Store them in bv starting at index start. Return the number of bytes actually read, or an end-of-file object.

Scheme Procedure: get-bytevector-all port
C Function: scm_get_bytevector_all (port)

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).

Scheme Procedure: unget-bytevector port bv [start [count]]
C Function: scm_unget_bytevector (port, bv, start, count)

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.

To perform binary output on a port, use put-u8 or put-bytevector.

Scheme Procedure: put-u8 port octet
C Function: scm_put_u8 (port, octet)

Write octet, an integer in the 0–255 range, to port, a binary output port.

Scheme Procedure: put-bytevector port bv [start [count]]
C Function: scm_put_bytevector (port, bv, start, count)

Write the contents of bv to port, optionally starting at index start and limiting to count octets.

Binary I/O in R7RS

R7RS defines the following binary I/O procedures. Access them with

(use-modules (scheme base))
Scheme Procedure: open-output-bytevector

Returns a binary output port that will accumulate bytes for retrieval by get-output-bytevector.

Scheme Procedure: write-u8 byte [out]

Writes byte to the given binary output port out and returns an unspecified value. out defaults to (current-output-port).

See also put-u8.

Scheme Procedure: read-u8 [in]

Returns the next byte available from the binary input port in, updating the port to point to the following byte. If no more bytes are available, an end-of-file object is returned. in defaults to (current-input-port).

See also get-u8.

Scheme Procedure: peek-u8 [in]

Returns the next byte available from the binary input port in, but without updating the port to point to the following byte. If no more bytes are available, an end-of-file object is returned. in defaults to (current-input-port).

See also lookahead-u8.

Scheme Procedure: get-output-bytevector port

Returns a bytevector consisting of the bytes that have been output to port so far in the order they were output. It is an error if port was not created with open-output-bytevector.

(define out (open-output-bytevector))
(write-u8 1 out)
(write-u8 2 out)
(write-u8 3 out)
(get-output-bytevector out) ⇒ #vu8(1 2 3)
Scheme Procedure: open-input-bytevector bv

Takes a bytevector bv and returns a binary input port that delivers bytes from bv.

(define in (open-input-bytevector #vu8(1 2 3)))
(read-u8 in) ⇒ 1
(peek-u8 in) ⇒ 2
(read-u8 in) ⇒ 2
(read-u8 in) ⇒ 3
(read-u8 in) ⇒ #<eof>
Scheme Procedure: read-bytevector! bv [port [start [end]]]

Reads the next end - start bytes, or as many as are available before the end of file, from the binary input port into the bytevector bv in left-to-right order beginning at the start position. If end is not supplied, reads until the end of bv has been reached. If start is not supplied, reads beginning at position 0.

Returns the number of bytes read. If no bytes are available, an end-of-file object is returned.

(define in (open-input-bytevector #vu8(1 2 3)))
(define bv (make-bytevector 5 0))
(read-bytevector! bv in 1 3) ⇒ 2
bv ⇒ #vu8(0 1 2 0 0 0)
Scheme Procedure: read-bytevector k in

Reads the next k bytes, or as many as are available before the end of file if that is less than k, from the binary input port in into a newly allocated bytevector in left-to-right order, and returns the bytevector. If no bytes are available before the end of file, an end-of-file object is returned.

(define bv #vu8(1 2 3))
(read-bytevector 2 (open-input-bytevector bv)) ⇒ #vu8(1 2)
(read-bytevector 10 (open-input-bytevector bv)) ⇒ #vu8(1 2 3)
Scheme Procedure: write-bytevector bv [port [start [end]]]

Writes the bytes of bytevector bv from start to end in left-to-right order to the binary output port. start defaults to 0 and end defaults to the length of bv.

(define out (open-output-bytevector))
(write-bytevector #vu8(0 1 2 3 4) out 2 4)
(get-output-bytevector out) ⇒ #vu8(2 3)