[armedbear-cvs] r11972 - trunk/abcl/src/org/armedbear/lisp/util

Erik Huelsmann ehuelsmann at common-lisp.net
Sun May 31 22:12:33 UTC 2009


Author: ehuelsmann
Date: Sun May 31 18:12:22 2009
New Revision: 11972

Log:
Factor out bbuf-updating from read(byte[]) and read(char[])
into ensureReadBbuf().

Modified:
   trunk/abcl/src/org/armedbear/lisp/util/RandomAccessCharacterFile.java

Modified: trunk/abcl/src/org/armedbear/lisp/util/RandomAccessCharacterFile.java
==============================================================================
--- trunk/abcl/src/org/armedbear/lisp/util/RandomAccessCharacterFile.java	(original)
+++ trunk/abcl/src/org/armedbear/lisp/util/RandomAccessCharacterFile.java	Sun May 31 18:12:22 2009
@@ -320,31 +320,40 @@
         internalFlush(false);
     }
 
+    private final boolean ensureReadBbuf(boolean force) throws IOException {
+        boolean bufReady = true;
+
+        if ((bbuf.remaining() == 0) || force) {
+            // need to read from the file.
+
+            if (bbufIsDirty) {
+                bbuf.flip();
+                fcn.position(bbufpos);
+                fcn.write(bbuf);
+                bbufpos = bbufpos+bbuf.position();
+                bbuf.clear();
+            } else {
+                fcn.position(bbufpos + bbuf.limit());
+                bbufpos += bbuf.position();
+                bbuf.compact();
+            }
+
+            bufReady = (fcn.read(bbuf) != -1);
+            fcnpos = fcn.position();
+            // update bbufpos.
+            bbuf.flip();
+        }
+
+        return bufReady;
+    }
+
     private int read(char[] cb, int off, int len) throws IOException {
         CharBuffer cbuf = CharBuffer.wrap(cb, off, len);
         boolean decodeWasUnderflow = false;
         boolean atEof = false;
         while ((cbuf.remaining() > 0) && dataIsAvailableForRead() && ! atEof) {
-            if ((bbuf.remaining() == 0) || decodeWasUnderflow) {
-                // need to read from the file.
 
-                if (bbufIsDirty) {
-                    bbuf.flip();
-                    fcn.position(bbufpos);
-                    fcn.write(bbuf);
-                    bbufpos = bbufpos+bbuf.position();
-                    bbuf.clear();
-                } else {
-                    fcn.position(bbufpos + bbuf.limit());
-                    bbufpos += bbuf.position();
-                    bbuf.compact();
-                }
-
-                atEof = (fcn.read(bbuf) == -1);
-                fcnpos = fcn.position();
-                // update bbufpos.
-                bbuf.flip();
-            }
+            atEof = ! ensureReadBbuf(decodeWasUnderflow);
             CoderResult r = cdec.decode(bbuf, cbuf, pointingAtEOF() );
             decodeWasUnderflow = (CoderResult.UNDERFLOW == r);
         }
@@ -457,21 +466,8 @@
         boolean atEof = false;
         while (pos - off < len && dataIsAvailableForRead()
                && ! atEof) {
-            if (bbuf.remaining() == 0) {
-                // need to read from the file.
-                flushBbuf(); // in case bbuf is dirty.
-                // update bbufpos.
-                bbufpos += bbuf.limit();
-                // if reads and writes are mixed, we may need to seek first.
-                if (bbufpos != fcnpos) {
-                    fcn.position(bbufpos);
-                }
-                // need to read data from file.
-                bbuf.clear();
-                atEof = (fcn.read(bbuf) == -1);
-                bbuf.flip();
-                fcnpos = bbufpos + bbuf.remaining();
-            }
+
+            atEof = ! ensureReadBbuf(false);
             int want = len - pos;
             if (want > bbuf.remaining()) {
                 want = bbuf.remaining();




More information about the armedbear-cvs mailing list