56
56
(defun byte-array-bout (stream byte)
57
57
(let ((current (byte-array-output-stream-index stream))
58
(workspace (byte-array-output-stream-buffer stream)))
58
(workspace (byte-array-output-stream-buffer stream)))
59
59
(if (= current (length workspace))
60
(let ((new-workspace (make-array (* current 2) :element-type '(unsigned-byte 8))))
61
(replace new-workspace workspace)
62
(setf (aref new-workspace current) byte)
63
(setf (byte-array-output-stream-buffer stream) new-workspace))
64
(setf (aref workspace current) byte))
60
(let ((new-workspace (make-array (* current 2) :element-type '(unsigned-byte 8))))
61
(replace new-workspace workspace)
62
(setf (aref new-workspace current) byte)
63
(setf (byte-array-output-stream-buffer stream) new-workspace))
64
(setf (aref workspace current) byte))
65
65
(setf (byte-array-output-stream-index stream) (1+ current))))
67
67
(defun byte-array-out-misc (stream operation &optional arg1 arg2)
97
97
(eval-when (:compile-toplevel :load-toplevel :execute)
98
98
(sb-ext:without-package-locks
99
99
(defvar *system-copy-fn* (if (fboundp (intern "COPY-SYSTEM-AREA" "SB-KERNEL"))
100
(intern "COPY-SYSTEM-AREA" "SB-KERNEL")
101
(intern "COPY-SYSTEM-UB8-AREA" "SB-KERNEL")))
100
(intern "COPY-SYSTEM-AREA" "SB-KERNEL")
101
(intern "COPY-SYSTEM-UB8-AREA" "SB-KERNEL")))
102
102
(defconstant +system-copy-multiplier+ (if (fboundp (intern "COPY-FROM-SYSTEM-AREA" "SB-KERNEL"))
108
108
(defstruct (byte-array-input-stream
109
(:include #+cmu system:lisp-stream
110
;;#+sbcl sb-impl::file-stream
111
#+(and sbcl old-sb-file-stream) sb-impl::file-stream
112
#+(and sbcl (not old-sb-file-stream)) sb-sys:fd-stream
113
(in #'byte-array-inch)
114
(bin #'byte-array-binch)
115
(n-bin #'byte-array-stream-read-n-bytes)
116
(misc #'byte-array-in-misc))
117
(:print-function %print-byte-array-input-stream)
119
(:constructor internal-make-byte-array-input-stream
120
(byte-array current end)))
109
(:include #+cmu system:lisp-stream
110
;;#+sbcl sb-impl::file-stream
111
#+(and sbcl old-sb-file-stream) sb-impl::file-stream
112
#+(and sbcl (not old-sb-file-stream)) sb-sys:fd-stream
113
(in #'byte-array-inch)
114
(bin #'byte-array-binch)
115
(n-bin #'byte-array-stream-read-n-bytes)
116
(misc #'byte-array-in-misc))
117
(:print-function %print-byte-array-input-stream)
119
(:constructor internal-make-byte-array-input-stream
120
(byte-array current end)))
121
121
(byte-array nil :type vector)
126
126
(defun %print-byte-array-input-stream (s stream d)
127
127
(declare (ignore s d))
128
128
(write-string "#<Byte-Array-Input Stream>" stream))
130
130
(defun byte-array-inch (stream eof-errorp eof-value)
131
131
(let ((byte-array (byte-array-input-stream-byte-array stream))
132
(index (byte-array-input-stream-current stream)))
132
(index (byte-array-input-stream-current stream)))
133
133
(cond ((= index (byte-array-input-stream-end stream))
135
(eof-or-lose stream eof-errorp eof-value)
137
(sb-impl::eof-or-lose stream eof-errorp eof-value)
140
(setf (byte-array-input-stream-current stream) (1+ index))
141
(aref byte-array index)))))
135
(eof-or-lose stream eof-errorp eof-value)
137
(sb-impl::eof-or-lose stream eof-errorp eof-value)
140
(setf (byte-array-input-stream-current stream) (1+ index))
141
(aref byte-array index)))))
143
143
(defun byte-array-binch (stream eof-errorp eof-value)
144
144
(let ((byte-array (byte-array-input-stream-byte-array stream))
145
(index (byte-array-input-stream-current stream)))
145
(index (byte-array-input-stream-current stream)))
146
146
(cond ((= index (byte-array-input-stream-end stream))
148
(eof-or-lose stream eof-errorp eof-value)
150
(sb-impl::eof-or-lose stream eof-errorp eof-value)
153
(setf (byte-array-input-stream-current stream) (1+ index))
154
(aref byte-array index)))))
148
(eof-or-lose stream eof-errorp eof-value)
150
(sb-impl::eof-or-lose stream eof-errorp eof-value)
153
(setf (byte-array-input-stream-current stream) (1+ index))
154
(aref byte-array index)))))
156
156
(defun byte-array-stream-read-n-bytes (stream buffer start requested eof-errorp)
157
157
(declare (type byte-array-input-stream stream))
158
158
(let* ((byte-array (byte-array-input-stream-byte-array stream))
159
(index (byte-array-input-stream-current stream))
160
(available (- (byte-array-input-stream-end stream) index))
161
(copy (min available requested)))
159
(index (byte-array-input-stream-current stream))
160
(available (- (byte-array-input-stream-end stream) index))
161
(copy (min available requested)))
162
162
(when (plusp copy)
163
163
(setf (byte-array-input-stream-current stream)
166
166
(system:without-gcing
167
167
(system::system-area-copy (system:vector-sap byte-array)
168
(* index vm:byte-bits)
169
(if (typep buffer 'system::system-area-pointer)
171
(system:vector-sap buffer))
172
(* start vm:byte-bits)
173
(* copy vm:byte-bits)))
168
(* index vm:byte-bits)
169
(if (typep buffer 'system::system-area-pointer)
171
(system:vector-sap buffer))
172
(* start vm:byte-bits)
173
(* copy vm:byte-bits)))
175
175
(sb-sys:without-gcing
176
176
(funcall *system-copy-fn* (sb-sys:vector-sap byte-array)
177
(* index +system-copy-multiplier+)
178
(if (typep buffer 'sb-sys::system-area-pointer)
180
(sb-sys:vector-sap buffer))
181
(* start +system-copy-multiplier+)
182
(* copy +system-copy-multiplier+))))
177
(* index +system-copy-multiplier+)
178
(if (typep buffer 'sb-sys::system-area-pointer)
180
(sb-sys:vector-sap buffer))
181
(* start +system-copy-multiplier+)
182
(* copy +system-copy-multiplier+))))
183
183
(if (and (> requested copy) eof-errorp)
184
(error 'end-of-file :stream stream)
184
(error 'end-of-file :stream stream)
187
187
(defun byte-array-in-misc (stream operation &optional arg1 arg2)
188
188
(declare (ignore arg2))
192
(setf (byte-array-input-stream-current stream) arg1)
193
(byte-array-input-stream-current stream)))
192
(setf (byte-array-input-stream-current stream) arg1)
193
(byte-array-input-stream-current stream)))
194
194
(:file-length (length (byte-array-input-stream-byte-array stream)))
195
195
(:unread (decf (byte-array-input-stream-current stream)))
196
196
(:listen (or (/= (the fixnum (byte-array-input-stream-current stream))
197
(the fixnum (byte-array-input-stream-end stream)))
197
(the fixnum (byte-array-input-stream-end stream)))
199
199
(:element-type 'base-char)))
201
201
(defun make-byte-array-input-stream (buffer &optional (start 0) (end (length buffer)))
202
202
"Returns an input stream which will supply the bytes of BUFFER between
203
203
Start and End in order."
228
228
"Returns an array of all data sent to a stream made by
229
229
Make-Byte-Array-Output-Stream since the last call to this function
230
230
and clears buffer."
232
(dump-output-stream-data stream)
232
(dump-output-stream-data stream)
233
233
(file-position stream 0)))
235
235
(defun dump-output-stream-data (stream)
236
236
"Returns an array of all data sent to a stream made by
237
237
Make-Byte-Array-Output-Stream since the last call to this function."
238
238
(force-output stream)
239
239
(let* ((length (file-position stream))
240
(result (make-array length :element-type '(unsigned-byte 8))))
240
(result (make-array length :element-type '(unsigned-byte 8))))
241
241
(replace result (slot-value stream 'excl::buffer))
244
(defmethod excl:device-extend ((stream extendable-buffer-output-stream)
246
(declare (ignore action))
247
(let* ((len (file-position stream))
248
(new-len (max (+ len need) (* 2 len)))
249
(old-buf (slot-value stream 'excl::buffer))
250
(new-buf (make-array new-len :element-type '(unsigned-byte 8))))
251
(declare (fixnum len)
252
(optimize (speed 3) (safety 0)))
254
(setf (aref new-buf i) (aref old-buf i)))
255
(setf (slot-value stream 'excl::buffer) new-buf)
256
(setf (slot-value stream 'excl::buffer-ptr) new-len)
244
(excl::without-package-locks
245
(defmethod excl:device-extend ((stream extendable-buffer-output-stream)
247
(declare (ignore action))
248
(let* ((len (file-position stream))
249
(new-len (max (+ len need) (* 2 len)))
250
(old-buf (slot-value stream 'excl::buffer))
251
(new-buf (make-array new-len :element-type '(unsigned-byte 8))))
252
(declare (fixnum len)
253
(optimize (speed 3) (safety 0)))
255
(setf (aref new-buf i) (aref old-buf i)))
256
(setf (slot-value stream 'excl::buffer) new-buf)
257
(setf (slot-value stream 'excl::buffer-ptr) new-len)
264
265
(defun make-byte-array-input-stream (buffer &optional (start 0)
265
(end (length buffer)))
266
(end (length buffer)))
266
267
(excl:make-buffer-input-stream buffer start end :octets))