Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BufferedReader read(char cbuf[], int off, int len) function

807588May 12 2009 — edited May 13 2009
Hi, I am a beginner in Java and I am trying to use the read(char cbuf[], int off, int len) function of the java.io.BufferedReader.
Here is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderTest
{
    static int BUFFER_SIZE = 1000;
    
    public BufferedReaderTest()
    {
        BufferedReader bufReader = null;
        try
        {
            bufReader = new BufferedReader(
                 new FileReader("bufferedReadTest.txt"), BUFFER_SIZE );
    
            try
            {
                // read the stream from the beginning
                char[] buffer = new char[BUFFER_SIZE];
                int i = 0;
                while (i != -1)
                {
                    System.out.println("reading from : "+i);
                    System.out.println("buffer size : "+buffer.length);
                    i = bufReader.read(buffer,i,BUFFER_SIZE);
                }
            }
            finally
            {
                if (bufReader!=null) bufReader.close();
            }
        }
        catch(IOException e)
        {
            System.err.println("Error: "+e.getMessage());
            e.printStackTrace();
        }   
    }


    public static void main(String[] args)
    {
        BufferedReaderTest test = new BufferedReaderTest();
    }
}    
Here is the output:

--------------------Configuration: test - JDK version 1.6.0_13 <Default> - <Default>--------------------
reading from : 0
buffer size : 1000
reading from : 1000
buffer size : 1000
Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.io.BufferedReader.read(BufferedReader.java:256)
at BufferedReaderTest.<init>(BufferedReaderTest.java:26)
at BufferedReaderTest.main(BufferedReaderTest.java:44)

Process completed.


And here is where the Exception is beeing thrown in BufferedReader.java:
    public int read(char cbuf[], int off, int len) throws IOException {
        synchronized (lock) {
	    ensureOpen();
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
                throw new IndexOutOfBoundsException();
            } else if (len == 0) {
                return 0;
            }

	    int n = read1(cbuf, off, len);
	    if (n <= 0) return n;
	    while ((n < len) && in.ready()) {
		int n1 = read1(cbuf, off + n, len - n);
		if (n1 <= 0) break;
		n += n1;
	    }
	    return n;
	}
    }
And to be honest the second line in the if statement does not make any sense to me:
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
*(off +++ len) > cbuf.length* is going to fail every time off is not equal to 0, that is, if I do not start reading from the beginning of the file, because the length of my buffer (*len*) is always equal to... the length of my buffer (*cbuf.length*)!

Moreover, I do not see the point of testing *(off +++ len) < 0* either as we already tested at the first line off < 0 and len < 0.

Please, tell me what I am missing here!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 10 2009
Added on May 12 2009
26 comments
4,097 views