JDK-4112757 : java.io.BufferedInputStream does not work with DataInputStream
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_95
  • CPU: x86
  • Submitted: 1998-02-17
  • Updated: 1998-02-17
  • Resolved: 1998-02-17
Related Reports
Duplicate :  
Description

Name: rm29839			Date: 02/17/98


I reported a bug with java.io.BufferInputStream
when it is used in conjunction with 
java.io.DataInputStream. It was given the Bug Id 
4109067. I now find that 4109067 has been closed
as being a duplicate of 4030995. 4030995 has in turn
been closed as being "not a bug".

Yet, as demonstrated by the code attached to 
4109067, BufferInputStream DOES NOT WORK when 
used in conjunction with DataInputStream 
(as confirmed by your own reviewer on 02/03/98)!

Who are you kidding? Nothing in the specification or any JDK doco
says that BufferedInputStream should not be used
in conjunction with DataInputStream! 

If you persist in not treating this problem as a
bug could please treat it as a "Feature Request".
How can Java be taken seriously if there isn't a
class that can reliably read non-text data using 
buffering?


//////////////////////////////////////////////////////////////////
// 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: 24959)
======================================================================

Comments
EVALUATION The bug is in the test, which assumes that the read(byte[], int, int) method of an InputStream will always read the requested number of bytes. This assumption is contrary to the specification of the InputStream class in the Java Language Specification (p. 681). In the given test program, the line dIn.read(buf); reads a different number of bytes when a BufferedInputStream is used, because that class takes advantage of the latitude allowed by the specification. The test will work if it is corrected at that point to keep reading until the desired number of bytes are read: for (int off = 0; off < buf.length;) { int n = dIn.read(buf, off, buf.length - off); if (n == -1) break; off += n; } I'm closing this as a duplicate of 4030995. -- mr@eng 2/17/1998
17-02-1998