United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4684515 : InputStreamReader throws Error or loops forever if underlying stream returns 0

Details
Type:
Bug
Submit Date:
2002-05-14
Status:
Resolved
Updated Date:
2002-11-20
Project Name:
JDK
Resolved Date:
2002-11-20
Component:
core-libs
OS:
windows_nt,generic
Sub-Component:
java.nio
CPU:
x86,generic
Priority:
P3
Resolution:
Fixed
Affected Versions:
1.2.2,1.4.0
Fixed Versions:
1.4.2 (mantis)

Related Reports
Duplicate:
Relates:

Sub Tasks

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
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
                                     
2002-11-14
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
mantis

FIXED IN:
mantis

INTEGRATED IN:
mantis
mantis-b08


                                     
2004-06-14



Hardware and Software, Engineered to Work Together