JDK-4090383 : java.io.Buffered{InputStream,Reader}: Change spec to fill buffers when possible
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.1.4,1.1.5,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1997-11-03
  • Updated: 1999-01-15
  • Resolved: 1999-01-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.2.0 1.2fcsFixed
Related Reports
Duplicate :  
Description

Name: bk70084			Date: 11/03/97


The BufferedReader will not read as many characters as were requested if the buffer is
emptied during a read().  For the example below, the buffer size is 10 characters, but
only 8 characters at a time are read from the stream.  The result is, on the first read() 
8 characters are read, but on the second read() only 2 characters are read because the 
buffer is empty.


//================= Test.java ======================
import java.io.*;
public class Test 
{
    public static void main(String args[]) throws IOException
    {
        Reader stream=new BufferedReader(new FileReader("TEST.TXT"), 10);
        char cbuf[]=new char[8];
        for (int j=0; j<10; j++)
        {
            int n=stream.read(cbuf);
            System.out.println("Have read "+n+" characters: "+cbuf);
        }
        System.out.println("Press any key");
        System.in.read();
    }
}


This is the code of BufferedReader.read(char[], int, int). I see a problem if I want to read more characters than the 
buffer capacity:

public int read(char cbuf[], int off, int len) throws IOException {
        synchronized (lock) {
            ensureOpen();
            if (nextChar >= nChars) {
                if (len >= cb.length && markedChar <= UNMARKED) {
                    return in.read(cbuf, off, len);
                }
                fill();
            }
            if (nextChar >= nChars)
                return -1;
            /* ==============================================
             * PROBLEM AHEAD!!!!
             * What about if I want to read 20 characters but
             * only 10 characters are left in the buffer?????
             * ============================================== */
            int n = Math.min(len, nChars - nextChar);
            System.arraycopy(cb, nextChar,
                             cbuf, off, n);
            nextChar += n;
            return n;
        }
    }

(Review ID: 15048)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.2fcs FIXED IN: 1.2fcs INTEGRATED IN: 1.2fcs
14-06-2004

EVALUATION Strictly speaking this is not a bug, since the specifications of the read methods in java.io.Reader (which are inherited by BufferedReader) clearly do not guarantee that the buffer will be filled even if data is available from the underlying stream. This is a common confusion, which also occurs with the BufferedInputStream class. Given the confusion level, however, it's worth considering tweaking the specifications of these two classes to say that they'll keep reading from the underlying stream as long as that stream is ready. I'll change this to an RFE. -- mr@eng 6/18/1998
18-06-1998