灰度流是允许用户定义的流的非标准扩展。它提供了用户可以扩展的类和方法。您应该检查您的实施手册,看看它是否提供了灰度流。
举一个简单的例子,返回随机字符的字符输入流可以这样实现:
(defclass random-character-input-stream (fundamental-character-input-stream) ((character-table :initarg :character-table :initform "abcdefghijklmnopqrstuvwxyz " ; The newline is necessary. :accessor character-table)) (:documentation "随机字符流。")) (defmethod stream-read-char ((stream random-character-input-stream)) (let ((table (character-table stream))) (aref table (random (length table))))) (let ((stream (make-instance 'random-character-input-stream))) (dotimes (i 5) (print (read-line stream)))) ; "gyaexyfjsqdcpciaaftoytsygdeycrrzwivwcfb" ; "gctnoxpajovjqjbkiqykdflbhfspmexjaaggonhydhayvknwpdydyiabithpt" ; "nvfxwzczfalosaqw" ; "sxeiejcovrtesbpmoppfvvjfvx" ; "hjplqgstbodbalnmxhsvxdox" ;=> NIL