2 @comment node-name, next, previous, up
5 Streams which read or write Lisp character data from or to the outside
6 world -- files, sockets or other external entities -- require the
7 specification of a conversion between the external, binary data and the
8 Lisp characters. In ANSI Common Lisp, this is done by specifying the
9 @code{:external-format} argument when the stream is created. The major
10 information required is an @emph{encoding}, specified by a keyword
11 naming that encoding; however, it is also possible to specify
12 refinements to that encoding as additional options to the external
15 In addition, SBCL supports various extensions of ANSI Common Lisp
19 @item Bivalent Streams
20 A type of stream that can read and write both @code{character} and
21 @code{(unsigned-byte 8)} values.
24 User-overloadable CLOS classes whose instances can be used as Lisp
25 streams (e.g. passed as the first argument to @code{format}).
28 The bundled contrib module @dfn{sb-simple-streams} implements a subset
29 of the Franz Allegro simple-streams proposal.
40 @node External Formats
41 @section External Formats
42 @cindex External formats
44 @findex @cl{stream-external-format}
45 The encodings supported by SBCL as external formats are named by
46 keyword. Each encoding has a canonical name, which will be encoding
47 returned by @code{stream-external-format}, and a number of aliases for
48 convenience, as shown in the table below:
50 @include encodings.texi-temp
53 @findex @cl{with-open-file}
54 In situations where an external file format designator is required, such
55 as the @code{:external-format} argument in calls to @code{open} or
56 @code{with-open-file}, users may supply the name of an encoding to
57 denote the external format which is applying that encoding to Lisp
60 In addition to the basic encoding for an external format, options
61 controlling various special cases may be passed, by using a list (whose
62 first element must be an encoding name and whose rest is a plist) as an
63 external file format designator. At present, the only supported key in
64 the plist is @code{:replacement}, where the corresponding value is a
65 string designator which is used as a replacement when an encoding or
66 decoding error happens, handling those errors without user intervention;
69 (with-open-file (i pathname :external-format '(:utf-8 :replacement #\?))
72 will read the first line of @var{pathname}, replacing any invalid utf-8
73 sequences with question marks.
75 @node Bivalent Streams
76 @section Bivalent Streams
78 A @dfn{bivalent stream} can be used to read and write both
79 @code{character} and @code{(unsigned-byte 8)} values. A bivalent
80 stream is created by calling @code{open} with the argument @code{:element-type
81 :default}. On such a stream, both binary and character data can be
82 read and written with the usual input and output functions.
84 @c Horrible visual markup
86 Streams are @emph{not} created bivalent by default for performance
87 reasons. Bivalent streams are incompatible with
88 @code{fast-read-char}, an internal optimization in sbcl's stream
89 machinery that bulk-converts octets to characters and implements a
90 fast path through @code{read-char}.
97 The Gray Streams interface is a widely supported extension that
98 provides for definition of CLOS-extensible stream classes. Gray
99 stream classes are implemented by adding methods to generic functions
100 analogous to Common Lisp's standard I/O functions. Instances of Gray
101 stream classes may be used with any I/O operation where a non-Gray
102 stream can, provided that all required methods have been implemented
106 * Gray Streams classes::
107 * Methods common to all streams::
108 * Input stream methods::
109 * Output stream methods::
110 * Binary stream methods::
111 * Character input stream methods::
112 * Character output stream methods::
113 * Gray Streams examples::
116 @node Gray Streams classes
117 @subsection Gray Streams classes
119 The defined Gray Stream classes are these:
121 @include class-sb-gray-fundamental-stream.texinfo
122 @include class-sb-gray-fundamental-input-stream.texinfo
125 The function input-stream-p will return true of any generalized
126 instance of fundamental-input-stream.
128 @include class-sb-gray-fundamental-output-stream.texinfo
131 The function output-stream-p will return true of any generalized
132 instance of fundamental-output-stream.
134 @include class-sb-gray-fundamental-binary-stream.texinfo
137 Note that instantiable subclasses of fundamental-binary-stream should
138 provide (or inherit) an applicable method for the generic function
141 @include class-sb-gray-fundamental-character-stream.texinfo
142 @include class-sb-gray-fundamental-binary-input-stream.texinfo
143 @include class-sb-gray-fundamental-binary-output-stream.texinfo
144 @include class-sb-gray-fundamental-character-input-stream.texinfo
145 @include class-sb-gray-fundamental-character-output-stream.texinfo
147 @node Methods common to all streams
148 @subsection Methods common to all streams
150 These generic functions can be specialized on any generalized instance
151 of fundamental-stream.
153 @include fun-common-lisp-stream-element-type.texinfo
154 @include fun-common-lisp-close.texinfo
155 @include fun-sb-gray-stream-file-position.texinfo
159 @node Input stream methods
160 @subsection Input stream methods
162 These generic functions may be specialized on any generalized instance
163 of fundamental-input-stream.
165 @include fun-sb-gray-stream-clear-input.texinfo
166 @include fun-sb-gray-stream-read-sequence.texinfo
168 @node Character input stream methods
169 @subsection Character input stream methods
171 These generic functions are used to implement subclasses of
172 fundamental-input-stream:
174 @include fun-sb-gray-stream-peek-char.texinfo
175 @include fun-sb-gray-stream-read-char-no-hang.texinfo
176 @include fun-sb-gray-stream-read-char.texinfo
177 @include fun-sb-gray-stream-read-line.texinfo
178 @include fun-sb-gray-stream-listen.texinfo
179 @include fun-sb-gray-stream-unread-char.texinfo
181 @node Output stream methods
182 @subsection Output stream methods
184 These generic functions are used to implement subclasses of
185 fundamental-output-stream:
187 @include fun-sb-gray-stream-clear-output.texinfo
188 @include fun-sb-gray-stream-finish-output.texinfo
189 @include fun-sb-gray-stream-force-output.texinfo
190 @include fun-sb-gray-stream-write-sequence.texinfo
192 @node Binary stream methods
193 @subsection Binary stream methods
195 The following generic functions are available for subclasses of
196 fundamental-binary-stream:
198 @include fun-sb-gray-stream-read-byte.texinfo
199 @include fun-sb-gray-stream-write-byte.texinfo
201 @node Character output stream methods
202 @subsection Character output stream methods
204 These generic functions are used to implement subclasses of
205 fundamental-character-output-stream:
207 @include fun-sb-gray-stream-advance-to-column.texinfo
208 @include fun-sb-gray-stream-fresh-line.texinfo
209 @include fun-sb-gray-stream-line-column.texinfo
210 @include fun-sb-gray-stream-line-length.texinfo
211 @include fun-sb-gray-stream-start-line-p.texinfo
212 @include fun-sb-gray-stream-terpri.texinfo
213 @include fun-sb-gray-stream-write-char.texinfo
214 @include fun-sb-gray-stream-write-string.texinfo
216 @include gray-streams-examples.texinfo
219 @comment node-name, next, previous, up
220 @section Simple Streams
221 @include sb-simple-streams/sb-simple-streams.texinfo