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

NextPrevUpTopContentsIndex

with-stream-output-buffer

Macro
Summary

Allows access to the output buffer.

Package

stream

Signature

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

Arguments

buffer, index, limit

Variables

stream

An output stream

body

Code

Values

result

The value returned by body.

Description

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

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

Within the code body, the variable names 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 buffers are always of type simple-string. The stream-element-type of stream depends on how the stream was constructed.

The index is the position of the next free element in the buffer and the limit is the position of the element after the end of the buffer. Therefore the buffer is full when index is greater than or equal to length.

Example

This example function writes a four character string to a buffered stream.

(defun write-4-chars (stream string)
  (declare (type stream:buffered-stream stream))
  (let ((elt 0))
    ;; Outer loop handles buffer flushing.
    (loop
     ;; Inner loop handles buffer updating.
     (loop (stream:with-stream-output-buffer (buf ind lim) stream
             (when (>= ind lim)
               ;; Buffer full: try to flush.
               (return))
             (setf (schar buf ind) (schar string elt))
             (incf elt)
             (incf ind)
             (when (= elt 4)
               (return-from write-4-chars))))
     (stream:stream-flush-buffer stream))))
See also

buffered-stream
with-stream-input-buffer


LispWorks User Guide and Reference Manual - 20 Sep 2017

NextPrevUpTopContentsIndex