-;;; old version, not good for implementing READ-SEQUENCE (and just complex)
-;;; FIXME: Remove once new FD-STREAM-READ-N-BYTES (below) is stable.
-#+nil
-(defun fd-stream-read-n-bytes (stream buffer start requested eof-error-p)
- (declare (type stream stream) (type index start requested))
- (let* ((sap (fd-stream-ibuf-sap stream))
- (offset start)
- (head (fd-stream-ibuf-head stream))
- (tail (fd-stream-ibuf-tail stream))
- (available (- tail head))
- (copy (min requested available)))
- (declare (type index offset head tail available copy))
- (unless (zerop copy)
- (if (typep buffer 'system-area-pointer)
- (system-area-copy sap (* head sb!vm:byte-bits)
- buffer (* offset sb!vm:byte-bits)
- (* copy sb!vm:byte-bits))
- (copy-from-system-area sap (* head sb!vm:byte-bits)
- buffer (+ (* offset sb!vm:byte-bits)
- (* sb!vm:vector-data-offset
- sb!vm:word-bits))
- (* copy sb!vm:byte-bits)))
- (incf (fd-stream-ibuf-head stream) copy))
- (cond
- ((or (= copy requested)
- (and (not eof-error-p) (/= copy 0)))
- copy)
- (t
- (setf (fd-stream-ibuf-head stream) 0)
- (setf (fd-stream-ibuf-tail stream) 0)
- (setf (fd-stream-listen stream) nil)
- (let ((now-needed (- requested copy))
- (len (fd-stream-ibuf-length stream)))
- (declare (type index now-needed len))
- (cond
- ((> now-needed len)
- ;; If the desired amount is greater than the stream buffer size, then
- ;; read directly into the destination, incrementing the start
- ;; accordingly. In this case, we never leave anything in the stream
- ;; buffer.
- (sb!sys:without-gcing
- (loop
- (multiple-value-bind (count err)
- (sb!unix:unix-read (fd-stream-fd stream)
- (sap+ (if (typep buffer
- 'system-area-pointer)
- buffer
- (vector-sap buffer))
- (+ offset copy))
- now-needed)
- (declare (type (or index null) count))
- (unless count
- (error "error reading ~S: ~A"
- stream
- (sb!unix:get-unix-error-msg err)))
- (if eof-error-p
- (when (zerop count)
- (error 'end-of-file :stream stream))
- (return (- requested now-needed)))
- (decf now-needed count)
- (when (zerop now-needed)
- (return requested))
- (incf offset count)))))
- (t
- ;; If we want less than the buffer size, then loop trying to fill the
- ;; stream buffer and copying what we get into the destination. When
- ;; we have enough, we leave what's left in the stream buffer.
- (loop
- (multiple-value-bind (count err)
- (sb!unix:unix-read (fd-stream-fd stream) sap len)
- (declare (type (or index null) count))
- (unless count
- (error "error reading ~S: ~A"
- stream
- (sb!unix:get-unix-error-msg err)))
- (when (and eof-error-p (zerop count))
- (error 'end-of-file :stream stream))
-
- (let* ((copy (min now-needed count))
- (copy-bits (* copy sb!vm:byte-bits))
- (buffer-start-bits
- (* (+ offset available) sb!vm:byte-bits)))
- (declare (type index copy copy-bits buffer-start-bits))
- (if (typep buffer 'system-area-pointer)
- (system-area-copy sap 0
- buffer buffer-start-bits
- copy-bits)
- (copy-from-system-area sap 0
- buffer (+ buffer-start-bits
- (* sb!vm:vector-data-offset
- sb!vm:word-bits))
- copy-bits))
-
- (decf now-needed copy)
- (when (or (zerop now-needed) (not eof-error-p))
- (setf (fd-stream-ibuf-head stream) copy)
- (setf (fd-stream-ibuf-tail stream) count)
- (return (- requested now-needed)))
- (incf offset copy)))))))))))
-
-;;; the N-BIN method for FD-STREAMs. This blocks in UNIX-READ. It is generally
-;;; used where there is a definite amount of reading to be done, so blocking
-;;; isn't too problematical.