classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problems with java.io.BufferedReader


From: Thorsten Suckow-Homberg
Subject: Problems with java.io.BufferedReader
Date: Sat, 10 Sep 2005 01:05:03 +0200

Hello list,

right now I have some problems with the java.io.BufferedReader and I hope someone can help out.

The docblock says about the method read(char[] buf, int offset, int length):

* [...]
* This method can
* return before reading the number of chars requested.  The actual number
* of chars read is returned as an int.  A -1 is returned to indicate the
* end of the stream.
* [...]

Does this mean that it can happen that the buffer supplied by the caller doesn't even get filled?

Because I'm operating on some small files and from my understanding there is a problem with the
default buffersize (BufferedReader::DEFAULT_BUFFER_SIZE = 8192).

I'll try to explain:

After instantiating the BufferedReader the members are set to the following defaults:

markPos = -1;
pos = 0;
limit = 0;

The buffer has a default-size of 8192.

I then call the read-method

bufferedReader->read(myBuffer, 0, 20);

whereas the argument myBuffer has a size of 200 and the file to read from has a length of app. 300 bytes.

I now quote the source of bufferedReader::read() - beginning with line 310 - along with some comments to give you an idea of what happens wrong in my eyes:

[...]
// avail is set to 0
int avail = limit-pos:
// count is set to 20 via the calling of the methos
if (count > avail) {

// this doesnt happen, avail equals to 0
if (avail > 0)
count = avail:
else {
// this doesnt happen, limit equals to 0
if (limit == buffer.length)
markPos = -1;
//doesnt happen, pos equals to limit equals to 0
if (pos > limit) {
retAtEndOfBuffer = true;
--pos;
}

// valid, markPos equals to -1
if (markPos < 0) {
   // doesnt happen, count is 20, buffer.length is 8192
     if (count >= buffer.length && !retAtEndOfBuffer)
       return in.read(buf, offset, count);
   // both pos and limit are reset to 0
     pos = limit = 0;
}
// the buffer-member of bufferedReader gets filled
// avail is set to -1, since limit equals to 0, and buffer.length-limit
//equals to buffer.length - ergo, the buffer gets filled with the whole file-contents
avail = in.read(buffer, limit, buffer.length - limit);
// this doesnt happen, since avail equals to -1
if (retAtEndOfBuffer && avail > 0 && buffer[limit] == '\n')
   {
     --avail;
     limit++;
   }
//HERES the problem; avail equals to -1 and count equals to 20,
// so avail gets returned, but nothing has been written into the
// caller supplied buffer
 if (avail < count)
   {
     if (avail <= 0)
       return avail;
     count = avail;
   }
[...]


This documents a first call on the read method after the object has been created. Am I totally off the path, or is it possible that there is a compatibility break to the
original implementation done by SUN?

Any help/hints greatly appreciated.

Thorsten







reply via email to

[Prev in Thread] Current Thread [Next in Thread]