|
Quick Lists
|
|
Bug ID:
|
4090383
|
|
Votes
|
6
|
|
Synopsis
|
java.io.Buffered{InputStream,Reader}: Change spec to fill buffers when possible
|
|
Category
|
java:classes_io
|
|
Reported Against
|
1.1.4
, 1.1.5
, 1.2beta3
|
|
Release Fixed
|
1.2(1.2fcs)
|
|
State
|
11-Closed,
Unverified,
request for enhancement
|
|
Priority:
|
3-Medium
|
|
Related Bugs
|
4100022
|
|
Submit Date
|
03-NOV-1997
|
|
Description
|
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)
======================================================================
|
|
Work Around
|
N/A
|
|
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.
-- xxxxx@xxxxx 6/18/1998
|
|
Comments
|
Submitted On 03-DEC-1997
Baz
I have found the same problem. A possible workaround is only using the read
method without parameters. This method does work correctly when the end of the
buffer is reached.
BufferedReader seems to be pretty buggy.
Baz
Submitted On 11-DEC-1997
Baz
It seems that BufferedInputstream suffers from the same problem.
Submitted On 08-AUG-1998
MartDesruisseaux
Maybe tweaking the specifications of BufferedReader and BufferedInputStream
would be a good idea - I don't know. But I think tweaking the specifications of
only those two class would not be enough. Would it be possible to add a new
method in Reader (for example Reader.readFully(int n)) which will keep reading
from any underlying stream (not only buffered stream) as long as possible?
Submitted On 08-MAY-2008
Praveena_prabha
import java.io.*;
class reverse
{
public static void main(String args[])throws Exception
{
int n,n1,rev=0;
try
{
Reader Stream=new BufferedReader(new FileReader(args[0]));
int n=Stream.read;
}
catch(FileNotFoundException fnfe)
{
System.out.print("nooo");
System.exit(0);
}
while(n>0)
{
n1=n%10;
rev=rev*10+n1;
n=n/10;
}
{
System.out.print("the reverse number="+rev);
}
}
}
PLEASE NOTE: JDK6 is formerly known as Project Mustang
|
|
|
 |