Setup: ikvm 0.24.0.1 (as JVM), Java 1.4.2_10 Of the three constructors for InputStreamReader that allow the app to set the Charset, the one where the Charset is passed directly does not give a Reader that can read the data. Charset cs = Charset.forName("utf-8"); new InputStreamReader(ins, cs.newDecoder()); // Works new InputStreamReader(ins, "utf-8"); // Works new InputStreamReader(ins, cs); // Does not work The test program should print 104 each time but does not do so in the third case. Workaround: use the constructor with cs.newDecoder(). Couldn't find a bug like this in bugzilla - apologies if it is a duplicate. Andy Test program: public static void main(String[] argv) { System.out.println("<<<<<<<<<<<<<<<"); try { Charset cs = Charset.forName("utf-8"); { ByteArrayInputStream ins = new ByteArrayInputStream("hello world".getBytes()) ; Reader r = new InputStreamReader(ins, cs.newDecoder()); System.out.println("With decoder: .read() => "+r.read()) ; } System.out.println("---------------"); // ---- { ByteArrayInputStream ins = new ByteArrayInputStream("hello world".getBytes()) ; Reader r = new InputStreamReader(ins, "utf-8"); System.out.println("With charset name: .read() => "+r.read()) ; } System.out.println("---------------"); // ---- { ByteArrayInputStream ins = new ByteArrayInputStream("hello world".getBytes()) ; Reader r = new InputStreamReader(ins, cs); System.out.println("With charset object: .read() => "+r.read()) ; } } catch (Exception ex) { System.err.println("IO exception: "+ex.getMessage()) ; } System.out.println(">>>>>>>>>>>>>>>"); }
Forgot to be clear about the effect: The reader created by new InputStreamReader(ins, cs); returns -1 on the .read() when it should be 104 (the "h")
Thanks for the bug report. maxBytesPerChar is not initialized in InputStreamReader(InputStream, CharSet) constructor.
Fixed.
Subject: Bug 26220 CVSROOT: /cvsroot/classpath Module name: classpath Branch: Changes by: Jeroen Frijters <jfrijters@savannah.gnu.org> 06/02/12 09:36:21 Modified files: . : ChangeLog java/io : InputStreamReader.java Log message: 2006-02-12 Jeroen Frijters <jeroen@frijters.net> Fixes PR 26220 * java/io/InputStreamReader.java (InputStreamReader(InputStream)): Use SystemProperties. (InputStreamReader(InputStream,Charset)): Corrected @since tag. Throw NullPointerException if in is null. Added maxBytesPerChar initialisation. (InputStreamReader(InputStream,CharsetDecoder)): Corrected @since tag. Throw NullPointerException if in is null. CVSWeb URLs: http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.6340&tr2=1.6341&r1=text&r2=text http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/java/io/InputStreamReader.java.diff?tr1=1.28&tr2=1.29&r1=text&r2=text