Index: src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java
===================================================================
--- src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java	(revision 887316)
+++ src/main/java/org/apache/pdfbox/pdfparser/BaseParser.java	(working copy)
@@ -386,47 +386,44 @@
      */
     private void readUntilEndStream( OutputStream out ) throws IOException
     {
-        int byteRead = 0;
         byte[] buffer = new byte[ENDSTREAM.length];
-        int nextIdx = pdfSource.read(buffer) % buffer.length; 
-        if (nextIdx == -1)
-        { 
-            return;
-        }
-        while(byteRead != -1 ) 
+        int readIndex = 0;
+        int byteRead;
+        while( (byteRead = pdfSource.read()) != -1 )
         {
-            if (cmpCircularBuffer( buffer, (nextIdx-ENDSTREAM.length + buffer.length)%buffer.length, ENDSTREAM )) 
+            int writeIndex = readIndex - buffer.length;
+            if( writeIndex >= 0 )
             {
+                out.write( buffer[writeIndex % buffer.length] );
+            }
+            buffer[readIndex % buffer.length] = (byte) byteRead;
+            if( endWithContent(buffer, readIndex, ENDSTREAM) )
+            {
                 pdfSource.unread( ENDSTREAM );
                 return;
             }
             /*
-             * occasionally steam objects do not write the endstream tag and just terminate
-             * the object with an endobj tag so we want to stop there as well. 
+             * occasionally stream objects do not write the endstream tag and just terminate
+             * the object with an endobj tag so we want to stop there as well.
              */
-            int endObjStart = (nextIdx-ENDOBJ.length+ buffer.length)%buffer.length;
-            if (cmpCircularBuffer( buffer, endObjStart, ENDOBJ )) 
+            if( endWithContent(buffer, readIndex, ENDOBJ) )
             {
-                // data is written to out only when it is going to be overwritten.
-                // write out the rest of the data in the buffer since ENDOBJ is smaller then the buffer
-                for (int i = nextIdx; i < buffer.length && i < endObjStart; i++ ) 
+                // write out the rest of the data in the buffer since ENDOBJ is smaller than the buffer
+                for (int i = Math.max(writeIndex + 1, 0), e = readIndex - ENDOBJ.length; i <= e; i++)
                 {
-                    out.write(buffer[i]);
+                    out.write( buffer[i % buffer.length] );
                 }
                 pdfSource.unread( ENDOBJ );
                 return;
             }
+            readIndex++;
+        }
+    }
 
-            out.write( buffer[nextIdx] );
-
-            byteRead = pdfSource.read();
-            buffer[nextIdx] = (byte)byteRead;
-
-            if (++nextIdx == buffer.length) 
-            {
-                nextIdx = 0;
-            }
-        }   
+    private boolean endWithContent( byte[] buffer, int endIndex, byte[] content )
+    {
+        int contentStart = (endIndex - content.length + 1) % buffer.length;
+        return contentStart >= 0 && cmpCircularBuffer( buffer, contentStart, content );
     }
 
     /**
