Commit b718bde6612afc8e3d5d36930f75fd07b9f29386

Thomas de Grivel 2018-06-29T12:42:11

stream-flush-output

diff --git a/unistd-output-stream.lisp b/unistd-output-stream.lisp
index 84d6065..c21e3b6 100644
--- a/unistd-output-stream.lisp
+++ b/unistd-output-stream.lisp
@@ -14,27 +14,26 @@
   (setf (stream-output-buffer stream) nil))
 
 (defmethod stream-flush-output ((stream unistd-output-stream))
-  (let ((buffer (stream-output-buffer stream)))
-    (let* ((fd (stream-fd stream))
-           (index (stream-output-index stream))
-           (buf (cffi:mem-aptr buffer :unsigned-char index))
-           (buflen (- (stream-output-length stream) index))
-           (r (if (stream-blocking-p stream)
-                  (unistd:write fd buf buflen)
-                  (unistd:write-non-blocking fd buf buflen))))
-      (cond ((eq :non-blocking r)
-             :non-blocking)
-            ((= 0 r)
-             :eof)
-            ((< r 0)
-             (error 'stream-output-error :stream stream))
-            (t
-             (incf (stream-output-index stream) r)
-             (when (= (stream-output-index stream)
-                      (stream-output-length stream))
-               (setf (stream-output-index stream) 0
-                     (stream-output-length stream) 0))
-             nil)))))
+  (let* ((buffer (stream-output-buffer stream))
+         (index (stream-output-index stream))
+         (length (stream-output-length stream))
+         (buflen (- length index)))
+    (declare (type fixnum index length buflen))
+    (when (< 0 buflen)
+      (let* ((fd (stream-fd stream))
+             (buf (cffi:mem-aptr buffer :unsigned-char index))
+             (r (if (stream-blocking-p stream)
+                    (unistd:write fd buf buflen)
+                    (unistd:write-non-blocking fd buf buflen))))
+        (cond ((eq :non-blocking r) :non-blocking)
+              ((= 0 r) :eof)
+              ((< r 0) (error 'stream-output-error :stream stream))
+              (t (incf (the fixnum (stream-output-index stream)) r)
+                 (when (= (the fixnum (stream-output-index stream))
+                          (the fixnum (stream-output-length stream)))
+                   (setf (stream-output-index stream) 0
+                         (stream-output-length stream) 0))
+               nil))))))
 
 (defmethod stream-write-element-to-buffer ((stream unistd-output-stream)
                                            element)