JDK-4684515 : InputStreamReader throws Error or loops forever if underlying stream returns 0
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.2.2,1.4.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 2002-05-14
  • Updated: 2002-11-20
  • Resolved: 2002-11-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 mantisFixed
Related Reports
Duplicate :  
Relates :  
Description

Name: rmT116609			Date: 05/14/2002


FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)


Windows NT Version 4.0
Build 1381 Service pack 4

 DESCRIPTION OF THE PROBLEM :
If the wrapped stream of an InputStreamReader returns 0 in its read() method, InputStreamReader throws java.lang.Error.  This can be confusing and difficult to isolate.  It appears no such error was thrown in 1.3.0, 1.3.1_03.  A more graceful handling or additional documentation explicitly stating this may prevent others from making the same mistake.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See sample code.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.Error
        at sun.nio.cs.StreamDecoder$CharsetSD.readBytes(StreamDecoder.java:417)
        at sun.nio.cs.StreamDecoder$CharsetSD.implRead(StreamDecoder.java:442)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:179)
        at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:129)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:172)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at Test.go(Test.java:20)
        at Test.main(Test.java:6)

---------- BEGIN SOURCE ----------
import java.io.*;

public class Test {

  public static void main(String args[]) throws java.io.FileNotFoundException,
java.io.IOException {
    Test t=new Test(); t.go();
  }

  public void go() throws java.io.FileNotFoundException, java.io.IOException {
    InputStream inputStream=new FileInputStream("anyfile") {
      public int read(byte[] b, int off, int len) {
        //...
        return 0;
      }
    };

    inputStream.read(new byte[1],0,1); //ok

    InputStreamReader inputStreamr=new InputStreamReader(inputStream);
    inputStreamr.read(new char[1],0,1); //error
  }
}

---------- END SOURCE ----------
(Review ID: 146523) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: mantis FIXED IN: mantis INTEGRATED IN: mantis mantis-b08
14-06-2004

EVALUATION With the current 1.4.1 build, the test case provided in the description loops forever. This is another fencepost case in the StreamDecoder logic. -- ###@###.### 2002/5/14 If InputStream.read returns zero, we now throw an IOException with the following message: Underlying input stream returned zero bytes. Bug 4779065 has been filed to request clarification of the spec for InputStream.read if no bytes are read. -- iag@sfbay 2002-11-14
14-11-2002