;(setf lisp::*enable-package-locked-errors* nil)

(in-package "LISP")

(defun read-into-simple-string (s stream start end)
  (declare (type simple-string s))
  (declare (type stream stream))
  (declare (type index start end))
  (unless (subtypep (stream-element-type stream) 'character)
    (error 'type-error
       :datum (read-char stream nil #\Null)
       :expected-type (stream-element-type stream)
       :format-control "Trying to read characters from a binary stream."))
  ;; Let's go as low level as it seems reasonable.
  (let* ((numbytes (- end start))
     (total-bytes 0))
    ;; read-n-bytes may return fewer bytes than requested, so we need
    ;; to keep trying.
    (loop while (plusp numbytes) do
      (let ((bytes-read (system:read-n-bytes stream s start numbytes nil)))
        (when (zerop bytes-read)
          (return-from read-into-simple-string total-bytes))
        (incf total-bytes bytes-read)
        (incf start bytes-read)
        (decf numbytes bytes-read)))
    total-bytes))
