All Manuals > LispWorks User Guide and Reference Manual > 20 User Defined Streams > 20.2 An illustrative example of user defined streams


20.2.4 Stream input

The following method for stream-read-char reads a character from the stream. If the character read is a #\Line-Separator , then the method returns #\Newline , otherwise the character read is returned. stream-read-char returns :eof at the end of the file.

(defmethod stream:stream-read-char ((stream unicode-ls-stream))
  (let ((char (read-char (ls-stream-file-stream stream) 
               nil :eof)))
    (if (eq char #\Line-Separator)

There is no need to define a new method for stream-read-line as the default method uses stream-read-char repeatedly to read a line, and our implementation of stream-read-char ensures that this will work.

We also need to make sure that if a #\Newline is unread, it is unread as a #\Line-Separator . The following method for stream-unread-char uses the Common Lisp file stream function unread-char to achieve this.

(defmethod stream:stream-unread-char ((stream unicode-ls-stream)
  (unread-char (if (eq char #\Newline) #\Line-Separator char)
               (ls-stream-file-stream stream)))

Finally, although the default methods for stream-listen and stream-clear-input would work for our stream, it is faster to use the functions provided by file-stream , again using our accessor ls-stream-file-stream .

(defmethod stream:stream-listen ((stream unicode-ls-stream))
  (listen (ls-stream-file-stream stream)))
(defmethod stream:stream-clear-input ((stream unicode-ls-stream))
  (clear-input (ls-stream-file-stream stream)))

LispWorks User Guide and Reference Manual - 21 Dec 2011