(defun custom-read-line (stream)
  (let ((line (make-array 512
			  :element-type 'character
			  :adjustable t
			  :fill-pointer 0)))
    (flet ((add-char (c)
	     (declare (type character c))
	     (unless (vector-push c line)
	       (adjust-array line (* 2 (array-dimension line 0))
			     :element-type 'character)
	       (vector-push c line)))
	   (finalize-line ()
	     (let ((len (length line)))
	       (when (and (> len 0)
			  (char= #\Return (aref line (1- len))))
		 (vector-pop line)))
	     line))
      (loop
	 do
	   (let ((c #+ecl(handler-case
			     (read-char stream)
			   (simple-error ()
			     #\UFFFD))
		    #+sbcl(handler-bind
			      ((sb-int:stream-decoding-error
				#'(lambda (e)
				    ;; Treat invalid UTF-8 octets as
				    ;; ISO-8859 characters.
				    (mapcar #'(lambda (c)
						(when (> c 127)
						  (add-char (code-char c))))
					    (sb-int:character-decoding-error-octets e))
				    (invoke-restart 'sb-int:attempt-resync))))
			    (read-char stream))))
	     (when (char= #\Newline c)
	       (return (values (finalize-line) t)))
	     (add-char c))))))
