I'm checking this in to classpath and gcj svn head.
I think I'll also pull it to the RH 4.1 branch.
This fixes PR 29178 by having the ASCII encoder reject characters
0x80..0xff. According to the PR Sun does this as well.
I also added a few methods to improve performance of canEncode. This
should also be done for utf-16 and utf-8 sometime.
Tom
2006-09-25 Tom Tromey <[EMAIL PROTECTED]>
PR libgcj/29178:
* gnu/java/nio/charset/US_ASCII.java (Encoder.canEncode): New method.
(Encoder.canEncode): Likewise.
(Encoder.encodeLoop): Return unmappable for all non-ASCII characters.
* gnu/java/nio/charset/ByteCharset.java (Encoder.canEncode): New
method.
(Encoder.canEncode): Likewise.
* gnu/java/nio/charset/ISO_8859_1.java (Encoder.canEncode): New
method.
(Encoder.canEncode): Likewise.
Index: gnu/java/nio/charset/ByteCharset.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/charset/ByteCharset.java,v
retrieving revision 1.2
diff -u -r1.2 ByteCharset.java
--- gnu/java/nio/charset/ByteCharset.java 2 Jul 2005 20:32:13 -0000
1.2
+++ gnu/java/nio/charset/ByteCharset.java 25 Sep 2006 18:57:29 -0000
@@ -156,6 +156,22 @@
}
}
+ public boolean canEncode(char c)
+ {
+ byte b = (c < lookup.length) ? lookup[c] : 0;
+ return b != 0 || c == 0;
+ }
+
+ public boolean canEncode(CharSequence cs)
+ {
+ for (int i = 0; i < cs.length(); ++i)
+ {
+ if (! canEncode(cs.charAt(i)))
+ return false;
+ }
+ return true;
+ }
+
protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
{
// TODO: Optimize this in the case in.hasArray() / out.hasArray()
Index: gnu/java/nio/charset/ISO_8859_1.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/charset/ISO_8859_1.java,v
retrieving revision 1.5
diff -u -r1.5 ISO_8859_1.java
--- gnu/java/nio/charset/ISO_8859_1.java 2 Jul 2005 20:32:13 -0000
1.5
+++ gnu/java/nio/charset/ISO_8859_1.java 25 Sep 2006 18:57:29 -0000
@@ -128,6 +128,19 @@
super (cs, 1.0f, 1.0f);
}
+ public boolean canEncode(char c)
+ {
+ return c <= 0xff;
+ }
+
+ public boolean canEncode(CharSequence cs)
+ {
+ for (int i = 0; i < cs.length(); ++i)
+ if (! canEncode(cs.charAt(i)))
+ return false;
+ return true;
+ }
+
protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
{
// TODO: Optimize this in the case in.hasArray() / out.hasArray()
Index: gnu/java/nio/charset/US_ASCII.java
===================================================================
RCS file: /cvsroot/classpath/classpath/gnu/java/nio/charset/US_ASCII.java,v
retrieving revision 1.5
diff -u -r1.5 US_ASCII.java
--- gnu/java/nio/charset/US_ASCII.java 2 Jul 2005 20:32:13 -0000 1.5
+++ gnu/java/nio/charset/US_ASCII.java 25 Sep 2006 18:57:29 -0000
@@ -134,6 +134,19 @@
super (cs, 1.0f, 1.0f);
}
+ public boolean canEncode(char c)
+ {
+ return c <= 0x7f;
+ }
+
+ public boolean canEncode(CharSequence cs)
+ {
+ for (int i = 0; i < cs.length(); ++i)
+ if (! canEncode(cs.charAt(i)))
+ return false;
+ return true;
+ }
+
protected CoderResult encodeLoop (CharBuffer in, ByteBuffer out)
{
// TODO: Optimize this in the case in.hasArray() / out.hasArray()
@@ -141,7 +154,7 @@
{
char c = in.get ();
- if (c > Byte.MAX_VALUE)
+ if (c > 0x7f)
{
in.position (in.position () - 1);
return CoderResult.unmappableForLength (1);