Re: relying on 'available()' (was: RE: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character)
Mikhail, OK, I'll prepare new patch. I've found other 2 issues that can affect InputStreamReader and I'll submit it soon. Thanks. Vladimir. On 4/25/06, Mikhail Loenko <[EMAIL PROTECTED]> wrote: > Vladimir, > > >Method available() is still used to determine endOfInput parameter. > According to >specification the > >method should be overridden by subclasses. > > 'should' does not mean 'must' , moreover it might be a user class who > does not care what the spec says. There could be cases when you do not > know how many bytes are available and you have to return 0. > > So I think the patch should be modified to avoid 'available()' > > Thanks, > Mikhail > > >-Original Message- > >From: Vladimir Strigun (JIRA) [mailto:[EMAIL PROTECTED] > >Sent: Monday, April 24, 2006 5:14 PM > >To: [EMAIL PROTECTED] > >Subject: [jira] Updated: (HARMONY-166) method read() in > InputStreamReader failed to read one > >character > > > > [ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] > > > >Vladimir Strigun updated HARMONY-166: > >- > > > >Attachment: InputStreamReader.java.patch > > > >Mikhail, > > > >please review my patch. Now method read() is not modified. I just > add decoding operations to > >fillBuf() method. > >Method available() is still used to determine endOfInput parameter. > According to specification the > >method should be overridden by subclasses. > > > >> method read() in InputStreamReader failed to read one character > >> --- > >> > >> Key: HARMONY-166 > >> URL: http://issues.apache.org/jira/browse/HARMONY-166 > >> Project: Harmony > >> Type: Bug > > > >> Components: Classlib > >> Reporter: Vladimir Strigun > >> Attachments: InputStreamReader.java.patch, InputStreamReader.patch.txt, > >InputStreamReaderTest.java > >> > >> I've started to play with harmony-57 contribution and found bug in > InputStreamReader class. > >Method read() should read a single character from input stream but it > works incorrectly for 2 > >bytes-per-char charsets. Example below shows that it failed to read > one character in UTF-16 > >charset. Sorry for so ugly test, it's just a part of > InputStreamReaderTest from Harmony-57 > >contribution. > >> import java.io.*; > >> import junit.framework.TestCase; > >> public class InputStreamReaderTest extends TestCase { > >> public static void main(String[] args) { > >> junit.textui.TestRunner.run(InputStreamReaderTest.class); > >> } > >> public void test_ISR_read() throws Exception { > >> InputStream in; > >> InputStreamReader reader; > >> try { > >> in = new LimitedByteArrayInputStream(0); > >> reader = new InputStreamReader(in, "UTF-16BE"); > >> int xx = reader.read(); > >> assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); > >> } catch (UnsupportedEncodingException e) { > >> // Can't test without the converter > >> System.out.println(e); > >> } catch (IOException e) { > >> e.printStackTrace(); > >> fail("UTF-16BE unexpected 1: " + e); > >> } > >> try { > >> in = new LimitedByteArrayInputStream(0); > >> reader = new InputStreamReader(in, "UTF-16LE"); > >> int xx = reader.read(); > >> assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); > >> } catch (UnsupportedEncodingException e) { > >> // Can't test without the converter > >> } catch (IOException e) { > >> fail("UTF-16BE unexpected 2: " + e); > >> } > >> try { > >> in = new LimitedByteArrayInputStream(1); > >> reader = new InputStreamReader(in, "UTF-16"); > >> assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261'); > >> } catch (UnsupportedEncodingException e) { > >> // Can't test without the converter > >> } catch (IOException e) { > >> fail("UTF-16BE unexpected 3: " + e); > >> } > >> try { > >> in = new LimitedByteArrayInputStream(2); > >> reader = new InputStreamReader(in, "ISO2022JP"); > >> int ch = reader.read(); > >> assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == '\u4e5d'); > >>
[humor] Re: [admin] Re: relying on 'available()' (was: RE: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character)
very funny Mikhail Loenko wrote: ops. sorry 2006/4/25, Geir Magnusson Jr <[EMAIL PROTECTED]>: psst... hey, Mikhail... how about a "[classlib]" on this subject line? :) Mikhail Loenko wrote: Vladimir, Method available() is still used to determine endOfInput parameter. According to >specification the method should be overridden by subclasses. 'should' does not mean 'must' , moreover it might be a user class who does not care what the spec says. There could be cases when you do not know how many bytes are available and you have to return 0. So I think the patch should be modified to avoid 'available()' Thanks, Mikhail -Original Message- From: Vladimir Strigun (JIRA) [mailto:[EMAIL PROTECTED] Sent: Monday, April 24, 2006 5:14 PM To: [EMAIL PROTECTED] Subject: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character [ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] Vladimir Strigun updated HARMONY-166: - Attachment: InputStreamReader.java.patch Mikhail, please review my patch. Now method read() is not modified. I just add decoding operations to fillBuf() method. Method available() is still used to determine endOfInput parameter. According to specification the method should be overridden by subclasses. method read() in InputStreamReader failed to read one character --- Key: HARMONY-166 URL: http://issues.apache.org/jira/browse/HARMONY-166 Project: Harmony Type: Bug Components: Classlib Reporter: Vladimir Strigun Attachments: InputStreamReader.java.patch, InputStreamReader.patch.txt, InputStreamReaderTest.java I've started to play with harmony-57 contribution and found bug in InputStreamReader class. Method read() should read a single character from input stream but it works incorrectly for 2 bytes-per-char charsets. Example below shows that it failed to read one character in UTF-16 charset. Sorry for so ugly test, it's just a part of InputStreamReaderTest from Harmony-57 contribution. import java.io.*; import junit.framework.TestCase; public class InputStreamReaderTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(InputStreamReaderTest.class); } public void test_ISR_read() throws Exception { InputStream in; InputStreamReader reader; try { in = new LimitedByteArrayInputStream(0); reader = new InputStreamReader(in, "UTF-16BE"); int xx = reader.read(); assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); } catch (UnsupportedEncodingException e) { // Can't test without the converter System.out.println(e); } catch (IOException e) { e.printStackTrace(); fail("UTF-16BE unexpected 1: " + e); } try { in = new LimitedByteArrayInputStream(0); reader = new InputStreamReader(in, "UTF-16LE"); int xx = reader.read(); assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); } catch (UnsupportedEncodingException e) { // Can't test without the converter } catch (IOException e) { fail("UTF-16BE unexpected 2: " + e); } try { in = new LimitedByteArrayInputStream(1); reader = new InputStreamReader(in, "UTF-16"); assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261'); } catch (UnsupportedEncodingException e) { // Can't test without the converter } catch (IOException e) { fail("UTF-16BE unexpected 3: " + e); } try { in = new LimitedByteArrayInputStream(2); reader = new InputStreamReader(in, "ISO2022JP"); int ch = reader.read(); assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == '\u4e5d'); ch = reader.read(); assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == '\u7b2c'); } catch (UnsupportedEncodingException e) { // Can't test without the converter System.out.println(e); } catch (IOException e) { fail("ISO2022JP unexpected: " + e); } } static class LimitedByteArrayInputStream extends ByteArrayInputStream { // A ByteArrayInputStream that only returns a single byte per read byte[] bytes; int count; public LimitedByteArrayInputStream(int type) { super(new byte[0]); switch (type) { case 0: bytes = new byte[] { 0x61, 0x72 }; break; case 1: bytes = new byte[] { (byte) 0xff, (byte) 0xfe, 0x61, 0x72 }; break; case 2: bytes = new byte[] { '\u001b', '$', 'B', '6', 'e', 'B', 'h', '\u001b', '(', 'B' }; break; } count = bytes.length; } public int read() { if (count == 0) return -1; count--; return bytes[bytes.length - count]; } public int read(byte[] buffer, int offset, int length) { if (count == 0) return -1; if (length == 0) return 0; buffer[offset] =
[admin] Re: relying on 'available()' (was: RE: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character)
ops. sorry 2006/4/25, Geir Magnusson Jr <[EMAIL PROTECTED]>: > psst... hey, Mikhail... how about a "[classlib]" on this subject line? :) > > > Mikhail Loenko wrote: > > Vladimir, > > > >> Method available() is still used to determine endOfInput parameter. > > According to >specification the > >> method should be overridden by subclasses. > > > > 'should' does not mean 'must' , moreover it might be a user class who > > does not care what the spec says. There could be cases when you do not > > know how many bytes are available and you have to return 0. > > > > So I think the patch should be modified to avoid 'available()' > > > > Thanks, > > Mikhail > > > >> -Original Message- > >> From: Vladimir Strigun (JIRA) [mailto:[EMAIL PROTECTED] > >> Sent: Monday, April 24, 2006 5:14 PM > >> To: [EMAIL PROTECTED] > >> Subject: [jira] Updated: (HARMONY-166) method read() in > > InputStreamReader failed to read one > >> character > >> > >> [ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] > >> > >> Vladimir Strigun updated HARMONY-166: > >> - > >> > >>Attachment: InputStreamReader.java.patch > >> > >> Mikhail, > >> > >> please review my patch. Now method read() is not modified. I just > > add decoding operations to > >> fillBuf() method. > >> Method available() is still used to determine endOfInput parameter. > > According to specification the > >> method should be overridden by subclasses. > >> > >>> method read() in InputStreamReader failed to read one character > >>> --- > >>> > >>> Key: HARMONY-166 > >>> URL: http://issues.apache.org/jira/browse/HARMONY-166 > >>> Project: Harmony > >>> Type: Bug > >>> Components: Classlib > >>> Reporter: Vladimir Strigun > >>> Attachments: InputStreamReader.java.patch, InputStreamReader.patch.txt, > >> InputStreamReaderTest.java > >>> I've started to play with harmony-57 contribution and found bug in > > InputStreamReader class. > >> Method read() should read a single character from input stream but it > > works incorrectly for 2 > >> bytes-per-char charsets. Example below shows that it failed to read > > one character in UTF-16 > >> charset. Sorry for so ugly test, it's just a part of > > InputStreamReaderTest from Harmony-57 > >> contribution. > >>> import java.io.*; > >>> import junit.framework.TestCase; > >>> public class InputStreamReaderTest extends TestCase { > >>> public static void main(String[] args) { > >>> junit.textui.TestRunner.run(InputStreamReaderTest.class); > >>> } > >>> public void test_ISR_read() throws Exception { > >>> InputStream in; > >>> InputStreamReader reader; > >>> try { > >>> in = new LimitedByteArrayInputStream(0); > >>> reader = new InputStreamReader(in, "UTF-16BE"); > >>> int xx = reader.read(); > >>> assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); > >>> } catch (UnsupportedEncodingException e) { > >>> // Can't test without the converter > >>> System.out.println(e); > >>> } catch (IOException e) { > >>> e.printStackTrace(); > >>> fail("UTF-16BE unexpected 1: " + e); > >>> } > >>> try { > >>> in = new LimitedByteArrayInputStream(0); > >>> reader = new InputStreamReader(in, "UTF-16LE"); > >>> int xx = reader.read(); > >>> assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); > >>> } catch (UnsupportedEncodingException e) { > >>> // Can't test without the converter > >>> } catch (IOException e) { > >>> fail("UTF-16BE unexpected 2: " + e); > >>> } > >>> try { > >>> in = new LimitedByteArrayInputStream(1); > >>> reader = new InputStreamReader(in, "UTF-16"); > >>> assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261'); > >>> } catch (UnsupportedEncodingException e) { > >>> // Can't test without the converter > >>> } catch (IOExc
Re: relying on 'available()' (was: RE: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character)
psst... hey, Mikhail... how about a "[classlib]" on this subject line? :) Mikhail Loenko wrote: Vladimir, Method available() is still used to determine endOfInput parameter. According to >specification the method should be overridden by subclasses. 'should' does not mean 'must' , moreover it might be a user class who does not care what the spec says. There could be cases when you do not know how many bytes are available and you have to return 0. So I think the patch should be modified to avoid 'available()' Thanks, Mikhail -Original Message- From: Vladimir Strigun (JIRA) [mailto:[EMAIL PROTECTED] Sent: Monday, April 24, 2006 5:14 PM To: [EMAIL PROTECTED] Subject: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character [ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] Vladimir Strigun updated HARMONY-166: - Attachment: InputStreamReader.java.patch Mikhail, please review my patch. Now method read() is not modified. I just add decoding operations to fillBuf() method. Method available() is still used to determine endOfInput parameter. According to specification the method should be overridden by subclasses. method read() in InputStreamReader failed to read one character --- Key: HARMONY-166 URL: http://issues.apache.org/jira/browse/HARMONY-166 Project: Harmony Type: Bug Components: Classlib Reporter: Vladimir Strigun Attachments: InputStreamReader.java.patch, InputStreamReader.patch.txt, InputStreamReaderTest.java I've started to play with harmony-57 contribution and found bug in InputStreamReader class. Method read() should read a single character from input stream but it works incorrectly for 2 bytes-per-char charsets. Example below shows that it failed to read one character in UTF-16 charset. Sorry for so ugly test, it's just a part of InputStreamReaderTest from Harmony-57 contribution. import java.io.*; import junit.framework.TestCase; public class InputStreamReaderTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(InputStreamReaderTest.class); } public void test_ISR_read() throws Exception { InputStream in; InputStreamReader reader; try { in = new LimitedByteArrayInputStream(0); reader = new InputStreamReader(in, "UTF-16BE"); int xx = reader.read(); assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); } catch (UnsupportedEncodingException e) { // Can't test without the converter System.out.println(e); } catch (IOException e) { e.printStackTrace(); fail("UTF-16BE unexpected 1: " + e); } try { in = new LimitedByteArrayInputStream(0); reader = new InputStreamReader(in, "UTF-16LE"); int xx = reader.read(); assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); } catch (UnsupportedEncodingException e) { // Can't test without the converter } catch (IOException e) { fail("UTF-16BE unexpected 2: " + e); } try { in = new LimitedByteArrayInputStream(1); reader = new InputStreamReader(in, "UTF-16"); assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261'); } catch (UnsupportedEncodingException e) { // Can't test without the converter } catch (IOException e) { fail("UTF-16BE unexpected 3: " + e); } try { in = new LimitedByteArrayInputStream(2); reader = new InputStreamReader(in, "ISO2022JP"); int ch = reader.read(); assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == '\u4e5d'); ch = reader.read(); assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == '\u7b2c'); } catch (UnsupportedEncodingException e) { // Can't test without the converter System.out.println(e); } catch (IOException e) { fail("ISO2022JP unexpected: " + e); } } static class LimitedByteArrayInputStream extends ByteArrayInputStream { // A ByteArrayInputStream that only returns a single byte per read byte[] bytes; int count; public LimitedByteArrayInputStream(int type) { super(new byte[0]); switch (type) { case 0: bytes = new byte[] { 0x61, 0x72 }; break; case 1: bytes = new byte[] { (byte) 0xff, (byte) 0xfe, 0x61, 0x72 }; break; case 2: bytes = new byte[] { '\u001b', '$', 'B', '6', 'e', 'B', 'h', '\u001b', '(', 'B' }; break; } count = bytes.length; } public int read() { if (count == 0) return -1; count--; return bytes[bytes.length - count]; } public int read(byte[] buffer, int offset, int length) { if (count == 0) return -1; if (length == 0) return 0; buffer[offset] = bytes[bytes.length - count]; count--; return 1; } public int available() { return count; } } } -- This message is automatically generated by JI
relying on 'available()' (was: RE: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character)
Vladimir, >Method available() is still used to determine endOfInput parameter. According to >specification the >method should be overridden by subclasses. 'should' does not mean 'must' , moreover it might be a user class who does not care what the spec says. There could be cases when you do not know how many bytes are available and you have to return 0. So I think the patch should be modified to avoid 'available()' Thanks, Mikhail >-Original Message- >From: Vladimir Strigun (JIRA) [mailto:[EMAIL PROTECTED] >Sent: Monday, April 24, 2006 5:14 PM >To: [EMAIL PROTECTED] >Subject: [jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one >character > > [ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] > >Vladimir Strigun updated HARMONY-166: >- > >Attachment: InputStreamReader.java.patch > >Mikhail, > >please review my patch. Now method read() is not modified. I just add decoding operations to >fillBuf() method. >Method available() is still used to determine endOfInput parameter. According to specification the >method should be overridden by subclasses. > >> method read() in InputStreamReader failed to read one character >> --- >> >> Key: HARMONY-166 >> URL: http://issues.apache.org/jira/browse/HARMONY-166 >> Project: Harmony >> Type: Bug > >> Components: Classlib >> Reporter: Vladimir Strigun >> Attachments: InputStreamReader.java.patch, InputStreamReader.patch.txt, >InputStreamReaderTest.java >> >> I've started to play with harmony-57 contribution and found bug in InputStreamReader class. >Method read() should read a single character from input stream but it works incorrectly for 2 >bytes-per-char charsets. Example below shows that it failed to read one character in UTF-16 >charset. Sorry for so ugly test, it's just a part of InputStreamReaderTest from Harmony-57 >contribution. >> import java.io.*; >> import junit.framework.TestCase; >> public class InputStreamReaderTest extends TestCase { >> public static void main(String[] args) { >> junit.textui.TestRunner.run(InputStreamReaderTest.class); >> } >> public void test_ISR_read() throws Exception { >> InputStream in; >> InputStreamReader reader; >> try { >> in = new LimitedByteArrayInputStream(0); >> reader = new InputStreamReader(in, "UTF-16BE"); >> int xx = reader.read(); >> assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); >> } catch (UnsupportedEncodingException e) { >> // Can't test without the converter >> System.out.println(e); >> } catch (IOException e) { >> e.printStackTrace(); >> fail("UTF-16BE unexpected 1: " + e); >> } >> try { >> in = new LimitedByteArrayInputStream(0); >> reader = new InputStreamReader(in, "UTF-16LE"); >> int xx = reader.read(); >> assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); >> } catch (UnsupportedEncodingException e) { >> // Can't test without the converter >> } catch (IOException e) { >> fail("UTF-16BE unexpected 2: " + e); >> } >> try { >> in = new LimitedByteArrayInputStream(1); >> reader = new InputStreamReader(in, "UTF-16"); >> assertTrue("Incorrect byte UTF-16BE", reader.read() == '\u7261'); >> } catch (UnsupportedEncodingException e) { >> // Can't test without the converter >> } catch (IOException e) { >> fail("UTF-16BE unexpected 3: " + e); >> } >> try { >> in = new LimitedByteArrayInputStream(2); >> reader = new InputStreamReader(in, "ISO2022JP"); >> int ch = reader.read(); >> assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == '\u4e5d'); >> ch = reader.read(); >> assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == '\u7b2c'); >> } catch (UnsupportedEncodingException e) { >> // Can't test without the converter >> System.out.println(e); >> } catch (IOException e) { >> fail("ISO2022JP unexpected: " + e); >> } >> } >> static class LimitedByteArrayInputStream extends ByteArrayInputStream { >> // A ByteArrayInputStream that only returns a single byte per read >> byte[] bytes; >> int count; >> public LimitedByteArrayInputStream(int type) { >> super(new byte[0]); >> switch (type) { >> case 0: >> bytes = new byte[] { 0x61, 0x72 }; >>
[jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character
[ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] Vladimir Strigun updated HARMONY-166: - Attachment: InputStreamReaderTest.java test for this issue. it's the same as in description, but it's more comfortable to read. > method read() in InputStreamReader failed to read one character > --- > > Key: HARMONY-166 > URL: http://issues.apache.org/jira/browse/HARMONY-166 > Project: Harmony > Type: Bug > Components: Classlib > Reporter: Vladimir Strigun > Attachments: InputStreamReader.patch.txt, InputStreamReaderTest.java > > I've started to play with harmony-57 contribution and found bug in > InputStreamReader class. Method read() should read a single character from > input stream but it works incorrectly for 2 bytes-per-char charsets. Example > below shows that it failed to read one character in UTF-16 charset. Sorry for > so ugly test, it's just a part of InputStreamReaderTest from Harmony-57 > contribution. > import java.io.*; > import junit.framework.TestCase; > public class InputStreamReaderTest extends TestCase { > public static void main(String[] args) { > junit.textui.TestRunner.run(InputStreamReaderTest.class); > } > public void test_ISR_read() throws Exception { > InputStream in; > InputStreamReader reader; > try { > in = new LimitedByteArrayInputStream(0); > reader = new InputStreamReader(in, "UTF-16BE"); > int xx = reader.read(); > assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > System.out.println(e); > } catch (IOException e) { > e.printStackTrace(); > fail("UTF-16BE unexpected 1: " + e); > } > try { > in = new LimitedByteArrayInputStream(0); > reader = new InputStreamReader(in, "UTF-16LE"); > int xx = reader.read(); > assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > } catch (IOException e) { > fail("UTF-16BE unexpected 2: " + e); > } > try { > in = new LimitedByteArrayInputStream(1); > reader = new InputStreamReader(in, "UTF-16"); > assertTrue("Incorrect byte UTF-16BE", reader.read() == > '\u7261'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > } catch (IOException e) { > fail("UTF-16BE unexpected 3: " + e); > } > try { > in = new LimitedByteArrayInputStream(2); > reader = new InputStreamReader(in, "ISO2022JP"); > int ch = reader.read(); > assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == > '\u4e5d'); > ch = reader.read(); > assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == > '\u7b2c'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > System.out.println(e); > } catch (IOException e) { > fail("ISO2022JP unexpected: " + e); > } > } > static class LimitedByteArrayInputStream extends ByteArrayInputStream { > // A ByteArrayInputStream that only returns a single byte per > read > byte[] bytes; > int count; > public LimitedByteArrayInputStream(int type) { > super(new byte[0]); > switch (type) { > case 0: > bytes = new byte[] { 0x61, 0x72 }; > break; > case 1: > bytes = new byte[] { (byte) 0xff, (byte) 0xfe, > 0x61, 0x72 }; > break; > case 2: > bytes = new byte[] { '\u001b', '$', 'B', '6', > 'e', 'B', 'h', > '\u001b', '(', 'B' }; > break; > } > count = bytes.length; > } > public int read() { > if (count == 0) > return -1; > count--; >
[jira] Updated: (HARMONY-166) method read() in InputStreamReader failed to read one character
[ http://issues.apache.org/jira/browse/HARMONY-166?page=all ] Vladimir Strigun updated HARMONY-166: - Attachment: InputStreamReader.patch.txt Please try my patch. > method read() in InputStreamReader failed to read one character > --- > > Key: HARMONY-166 > URL: http://issues.apache.org/jira/browse/HARMONY-166 > Project: Harmony > Type: Bug > Components: Classlib > Reporter: Vladimir Strigun > Attachments: InputStreamReader.patch.txt > > I've started to play with harmony-57 contribution and found bug in > InputStreamReader class. Method read() should read a single character from > input stream but it works incorrectly for 2 bytes-per-char charsets. Example > below shows that it failed to read one character in UTF-16 charset. Sorry for > so ugly test, it's just a part of InputStreamReaderTest from Harmony-57 > contribution. > import java.io.*; > import junit.framework.TestCase; > public class InputStreamReaderTest extends TestCase { > public static void main(String[] args) { > junit.textui.TestRunner.run(InputStreamReaderTest.class); > } > public void test_ISR_read() throws Exception { > InputStream in; > InputStreamReader reader; > try { > in = new LimitedByteArrayInputStream(0); > reader = new InputStreamReader(in, "UTF-16BE"); > int xx = reader.read(); > assertTrue("Incorrect byte UTF-16BE", xx == '\u6172'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > System.out.println(e); > } catch (IOException e) { > e.printStackTrace(); > fail("UTF-16BE unexpected 1: " + e); > } > try { > in = new LimitedByteArrayInputStream(0); > reader = new InputStreamReader(in, "UTF-16LE"); > int xx = reader.read(); > assertTrue("Incorrect byte UTF-16BE", xx == '\u7261'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > } catch (IOException e) { > fail("UTF-16BE unexpected 2: " + e); > } > try { > in = new LimitedByteArrayInputStream(1); > reader = new InputStreamReader(in, "UTF-16"); > assertTrue("Incorrect byte UTF-16BE", reader.read() == > '\u7261'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > } catch (IOException e) { > fail("UTF-16BE unexpected 3: " + e); > } > try { > in = new LimitedByteArrayInputStream(2); > reader = new InputStreamReader(in, "ISO2022JP"); > int ch = reader.read(); > assertTrue("Incorrect byte ISO2022JP 1: " + ch, ch == > '\u4e5d'); > ch = reader.read(); > assertTrue("Incorrect byte ISO2022JP 2: " + ch, ch == > '\u7b2c'); > } catch (UnsupportedEncodingException e) { > // Can't test without the converter > System.out.println(e); > } catch (IOException e) { > fail("ISO2022JP unexpected: " + e); > } > } > static class LimitedByteArrayInputStream extends ByteArrayInputStream { > // A ByteArrayInputStream that only returns a single byte per > read > byte[] bytes; > int count; > public LimitedByteArrayInputStream(int type) { > super(new byte[0]); > switch (type) { > case 0: > bytes = new byte[] { 0x61, 0x72 }; > break; > case 1: > bytes = new byte[] { (byte) 0xff, (byte) 0xfe, > 0x61, 0x72 }; > break; > case 2: > bytes = new byte[] { '\u001b', '$', 'B', '6', > 'e', 'B', 'h', > '\u001b', '(', 'B' }; > break; > } > count = bytes.length; > } > public int read() { > if (count == 0) > return -1; > count--; > return bytes[bytes.length - count]; > } > public int read(byte[] b