diff --git a/classpath/avian/Iso88591.java b/classpath/avian/Iso88591.java new file mode 100644 index 0000000000..a94f8e2f2f --- /dev/null +++ b/classpath/avian/Iso88591.java @@ -0,0 +1,26 @@ + +/* Copyright (c) 2011, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package avian; + +import java.io.ByteArrayOutputStream; + +public class Iso88591 { + + public static byte[] encode(char[] s16, int offset, int length) { + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + for (int i = offset; i < offset+length; ++i) { + // ISO-88591-1/Latin-1 is the same as UTF-16 under 0x100 + buf.write(s16[i]); + } + return buf.toByteArray(); + } +} diff --git a/classpath/java/lang/String.java b/classpath/java/lang/String.java index 7f6ff1edfc..83ce12dfa3 100644 --- a/classpath/java/lang/String.java +++ b/classpath/java/lang/String.java @@ -16,10 +16,16 @@ import java.util.Comparator; import java.util.Locale; import java.io.Serializable; import avian.Utf8; +import avian.Iso88591; public final class String implements Comparable, CharSequence, Serializable { + private static final String UTF_8_ENCODING = "UTF-8"; + private static final String ISO_8859_1_ENCODING = "ISO-8859-1"; + private static final String LATIN_1_ENCODING = "LATIN-1"; + private static final String DEFAULT_ENCODING = UTF_8_ENCODING; + public static Comparator CASE_INSENSITIVE_ORDER = new Comparator() { public int compare(String a, String b) { @@ -52,8 +58,8 @@ public final class String throws UnsupportedEncodingException { this(bytes, offset, length); - if (! (charsetName.equalsIgnoreCase("UTF-8") - || charsetName.equalsIgnoreCase("ISO-8859-1"))) + if (! (charsetName.equalsIgnoreCase(UTF_8_ENCODING) + || charsetName.equalsIgnoreCase(ISO_8859_1_ENCODING))) { throw new UnsupportedEncodingException(charsetName); } @@ -421,18 +427,31 @@ public final class String } public byte[] getBytes() { - if(data instanceof byte[]) { - byte[] b = new byte[length]; - getBytes(0, length, b, 0); - return b; + try { + return getBytes(DEFAULT_ENCODING); + } catch (java.io.UnsupportedEncodingException ex) { + throw new RuntimeException( + "Default '" + DEFAULT_ENCODING + "' encoding not handled", ex); } - return Utf8.encode((char[])data, offset, length); } public byte[] getBytes(String format) throws java.io.UnsupportedEncodingException { - return getBytes(); + if(data instanceof byte[]) { + byte[] b = new byte[length]; + getBytes(0, length, b, 0); + return b; + } + String fmt = format.trim().toUpperCase(); + if (DEFAULT_ENCODING.equals(fmt)) { + return Utf8.encode((char[])data, offset, length); + } else if (ISO_8859_1_ENCODING.equals(fmt) || LATIN_1_ENCODING.equals(fmt)) { + return Iso88591.encode((char[])data, offset, length); + } else { + throw new java.io.UnsupportedEncodingException( + "Encoding " + format + " not supported"); + } } public void getChars(int srcOffset, int srcEnd, diff --git a/classpath/java/util/AbstractMap.java b/classpath/java/util/AbstractMap.java new file mode 100644 index 0000000000..6bcdf2e5fa --- /dev/null +++ b/classpath/java/util/AbstractMap.java @@ -0,0 +1,13 @@ +/* Copyright (c) 2008-2011, Avian Contributors + + Permission to use, copy, modify, and/or distribute this software + for any purpose with or without fee is hereby granted, provided + that the above copyright notice and this permission notice appear + in all copies. + + There is NO WARRANTY for this software. See license.txt for + details. */ + +package java.util; + +public abstract class AbstractMap extends Object implements Map { }