Name: rm29839 Date: 02/03/98
This bug has been reported before as 4068955 but
now shows as "Closed, not reproducible". This is
a serious bug. I have attached some code that will
reproduce the problem. Run the code as it stands
and you will see that it reports errors. Then
change the code by commenting out the creation of
the DataInputStream from a BufferedInputStream and
uncomment the line that creates the DataInputStream
directly from a FileInputStream. Run the code again
and there are no errors.
If you have any problems reproducing this problem
please email me immediately.
//////////////////////////////////////////////////////////////////
// This class demonstrates a bug in BufferedInputStream. If the //
// DataInputStream (dIn)is created using a FileInputStream then no //
// errors are detected. If it is created with a //
// BufferedInputStream then errors are detected. //
//////////////////////////////////////////////////////////////////
import java.io.*;
class BufferedInputTest {
static String TEST_FILE = "testFile";
static int REPETITIONS = 395;
static byte testByte = 'X';
static byte[] testByteArray = {'A','B','C','D','E','F'};
static short testShort = 1;
static int testInt = 2;
static long testLong = 3L;
public static void main(String[] args) {
DataOutputStream dOut = null;
DataInputStream dIn = null;
//1st create a test file...
try {
dOut = new DataOutputStream(new FileOutputStream(TEST_FILE));
for (int i=0; i < REPETITIONS; i++) {
dOut.write(testByte);
dOut.write(testByteArray,0,testByteArray.length);
dOut.writeShort(testShort);
dOut.writeInt(testInt);
dOut.writeLong(testLong);
}
//dIn = new DataInputStream(new FileInputStream(TEST_FILE));
dIn = new DataInputStream(new BufferedInputStream(new FileInputStream(TEST_FILE)));
System.out.println(dIn.available() + " bytes in file");
//Now read the test file and test it's correctness...
for (int i=0; i < REPETITIONS; i++) {
byte[] buf = new byte[testByteArray.length];
byte readByte = dIn.readByte();
if (readByte != testByte) {
System.out.println("byte different at " + i + " " + readByte);
}
dIn.read(buf);
for (int j=0; j < buf.length; j++) {
if (buf[j] != testByteArray[j]) {
System.out.println("byte array different at " + i);
for (int k=0; k < buf.length; k++) {
System.out.print((char)(buf[k]));
}
System.out.println("\n");
break;
}
}
short readShort = dIn.readShort();
if (readShort != testShort) {
System.out.println("testShort different at " + i + " " + readShort);
}
int readInt = dIn.readInt();
if (readInt != testInt) {
System.out.println("testInt different at " + i + " " + readInt);
}
long readLong = dIn.readLong();
if (readLong != testLong) {
System.out.println("testLong different at " + i + " " + readLong);
}
}
}
catch(IOException e) {
System.out.println(e);
}
finally {
if (dOut != null) {
try {dOut.close();} catch(IOException e) {};
}
if (dIn != null) {
try {dIn.close();} catch(IOException e) {};
}
}
}
}
(Review ID: 24344)
======================================================================