All Manuals > LispWorks User Guide and Reference Manual > 48 The STREAM Package

NextPrevUpTopContentsIndex

with-stream-input-buffer

Macro
Summary

Allows access to the input buffer.

Package

stream

Signature

with-stream-input-buffer (buffer index limit) stream &body body => result

Arguments

buffer, index, limit

Variables.

stream

An input stream.

body

Code.

Values

result

The value returned by body.

Description

The macro with-stream-input-buffer allows access to the state of the input buffer for the given buffered stream.

stream should be an instance of a subclass of buffered-stream.

Within the code body, the variables buffer, index and limit are bound to the buffer of stream, its current index and the limit of the buffer. Setting buffer, index or limit will change the values in the stream stream but note that other changes to these values (for example, by calling other stream functions) will not affect the values bound within the macro. See the example for a typical use which shows how this restriction can be handled.

The buffer is always of type simple-string. The stream-element-type of stream depends on how it was constructed.

The index is the position of the next element to be read from the buffer and the limit is the position of the element after the end of the buffer. Therefore there is no data in the buffer when index is greater than or equal to length.

Example

This example function returns a string with exactly four characters read from a buffered stream. If end-of-file is reached before four characters have been read, it returns nil.

(defun read-4-chars (stream)
  (declare (type stream:buffered-stream stream))
  (let ((res (make-string 4))
        (elt 0))
    ;; Outer loop handles buffer filling.
    (loop
     ;; Inner loop handles buffer scanning.
     (loop (stream:with-stream-input-buffer (buf ind lim) stream
             (when (>= ind lim)
               ;; End of buffer: try to refill.
               (return))
             (setf (schar res elt) (schar buf ind))
             (incf elt)
             (incf ind)
             (when (= elt 4)
               (return-from read-4-chars res))))
     (unless (stream:stream-fill-buffer stream)
       (return-from read-4-chars nil)))))
See also

buffered-stream
with-stream-output-buffer


LispWorks User Guide and Reference Manual - 13 Feb 2015

NextPrevUpTopContentsIndex