From 8ceb88d4cd6ba729b38dee3bda925ab34a75efc9 Mon Sep 17 00:00:00 2001 From: David Daney Date: Tue, 3 Jan 2006 22:58:31 +0000 Subject: PR libgcj/9715, PR libgcj/19132: * java/nio/charset/Charset.java (charsetForName): Try default provider first. (availableCharsets): Re-merged. (providers2): Likewise. (defaultCharset): Likewise. * sources.am, Makefile.in: Rebuilt. * gnu/java/nio/charset/Provider.java: Removed. * java/io/OutputStreamWriter.java (OutputStreamWriter(OutputStream,Charset)): New constructor. (OutputStreamWriter(OutputStream,CharsetEncoder)): Likewise. * java/io/InputStreamReader.java (InputStreamReader(InputStream,CharsetDecoder)): New constructor. (InputStreamReader(InputStream,Charset)): Likewise. * gnu/gcj/convert/BytesToUnicode.java (getDecoder): Try a BytesToCharsetAdaptor. * gnu/gcj/convert/UnicodeToBytes.java (getEncoder): Try a CharsetToBytesAdaptor. * gnu/gcj/convert/CharsetToBytesAdaptor.java: New file. * gnu/gcj/convert/BytesToCharsetAdaptor.java: New file. * mauve-libgcj: Remove getEncoding exclusion. Co-Authored-By: Tom Tromey From-SVN: r109294 --- libjava/java/io/InputStreamReader.java | 21 ++++++++ libjava/java/io/OutputStreamWriter.java | 28 ++++++++++ libjava/java/nio/charset/Charset.java | 94 ++++++++++++++++++++++++--------- 3 files changed, 119 insertions(+), 24 deletions(-) (limited to 'libjava/java') diff --git a/libjava/java/io/InputStreamReader.java b/libjava/java/io/InputStreamReader.java index e55f135..91568c5 100644 --- a/libjava/java/io/InputStreamReader.java +++ b/libjava/java/io/InputStreamReader.java @@ -39,6 +39,8 @@ exception statement from your version. */ package java.io; import gnu.gcj.convert.*; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; /** * This class reads characters from a byte input stream. The characters @@ -131,6 +133,25 @@ public class InputStreamReader extends Reader this(in, BytesToUnicode.getDecoder(encoding_name)); } + /** + * Creates an InputStreamReader that uses a decoder of the given + * charset to decode the bytes in the InputStream into + * characters. + */ + public InputStreamReader(InputStream in, Charset charset) + { + this(in, new BytesToCharsetAdaptor(charset)); + } + + /** + * Creates an InputStreamReader that uses the given charset decoder + * to decode the bytes in the InputStream into characters. + */ + public InputStreamReader(InputStream in, CharsetDecoder decoder) + { + this(in, new BytesToCharsetAdaptor(decoder)); + } + private InputStreamReader(InputStream in, BytesToUnicode decoder) { // FIXME: someone could pass in a BufferedInputStream whose buffer diff --git a/libjava/java/io/OutputStreamWriter.java b/libjava/java/io/OutputStreamWriter.java index 8b7beeb..90ecd9f 100644 --- a/libjava/java/io/OutputStreamWriter.java +++ b/libjava/java/io/OutputStreamWriter.java @@ -39,6 +39,9 @@ exception statement from your version. */ package java.io; import gnu.gcj.convert.UnicodeToBytes; +import gnu.gcj.convert.CharsetToBytesAdaptor; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; /** * This class writes characters to an output stream that is byte oriented @@ -129,6 +132,31 @@ public class OutputStreamWriter extends Writer } /** + * This method initializes a new instance of OutputStreamWriter + * to write to the specified stream using a given Charset. + * + * @param out The OutputStream to write to + * @param cs The Charset of the encoding to use + */ + public OutputStreamWriter(OutputStream out, Charset cs) + { + this(out, new CharsetToBytesAdaptor(cs)); + } + + /** + * This method initializes a new instance of OutputStreamWriter + * to write to the specified stream using a given + * CharsetEncoder. + * + * @param out The OutputStream to write to + * @param enc The CharsetEncoder to encode the output with + */ + public OutputStreamWriter(OutputStream out, CharsetEncoder enc) + { + this(out, new CharsetToBytesAdaptor(enc)); + } + + /** * This method closes this stream, and the underlying * OutputStream * diff --git a/libjava/java/nio/charset/Charset.java b/libjava/java/nio/charset/Charset.java index 14b6db8..48093bc9d 100644 --- a/libjava/java/nio/charset/Charset.java +++ b/libjava/java/nio/charset/Charset.java @@ -38,6 +38,8 @@ exception statement from your version. */ package java.nio.charset; +import gnu.classpath.ServiceFactory; +import gnu.classpath.SystemProperties; import gnu.java.nio.charset.Provider; import java.io.BufferedReader; @@ -116,6 +118,53 @@ public abstract class Charset implements Comparable } } + /** + * Returns the system default charset. + * + * This may be set by the user or VM with the file.encoding + * property. + * + * @since 1.5 + */ + public static Charset defaultCharset() + { + String encoding; + + try + { + encoding = SystemProperties.getProperty("file.encoding"); + } + catch(SecurityException e) + { + // Use fallback. + encoding = "ISO-8859-1"; + } + catch(IllegalArgumentException e) + { + // Use fallback. + encoding = "ISO-8859-1"; + } + + try + { + return forName(encoding); + } + catch(UnsupportedCharsetException e) + { + // Ignore. + } + catch(IllegalCharsetNameException e) + { + // Ignore. + } + catch(IllegalArgumentException e) + { + // Ignore. + } + + throw new IllegalStateException("Can't get default charset!"); + } + public static boolean isSupported (String charsetName) { return charsetForName (charsetName) != null; @@ -155,13 +204,19 @@ public abstract class Charset implements Comparable private static Charset charsetForName(String charsetName) { checkName (charsetName); - Charset cs = null; - CharsetProvider[] providers = providers2(); - for (int i = 0; i < providers.length; i++) + // Try the default provider first + // (so we don't need to load external providers unless really necessary) + // if it is an exotic charset try loading the external providers. + Charset cs = provider().charsetForName(charsetName); + if (cs == null) { - cs = providers[i].charsetForName(charsetName); - if (cs != null) - break; + CharsetProvider[] providers = providers2(); + for (int i = 0; i < providers.length; i++) + { + cs = providers[i].charsetForName(charsetName); + if (cs != null) + break; + } } return cs; } @@ -169,6 +224,11 @@ public abstract class Charset implements Comparable public static SortedMap availableCharsets() { TreeMap charsets = new TreeMap(String.CASE_INSENSITIVE_ORDER); + for (Iterator i = provider().charsets(); i.hasNext(); ) + { + Charset cs = (Charset) i.next(); + charsets.put(cs.name(), cs); + } CharsetProvider[] providers = providers2(); for (int j = 0; j < providers.length; j++) @@ -206,7 +266,7 @@ public abstract class Charset implements Comparable /** * We need to support multiple providers, reading them from * java.nio.charset.spi.CharsetProvider in the resource directory - * META-INF/services. + * META-INF/services. This returns the "extra" charset providers. */ private static CharsetProvider[] providers2() { @@ -214,24 +274,10 @@ public abstract class Charset implements Comparable { try { - Enumeration en = ClassLoader.getSystemResources - ("META-INF/services/java.nio.charset.spi.CharsetProvider"); + Iterator i = ServiceFactory.lookupProviders(CharsetProvider.class); LinkedHashSet set = new LinkedHashSet(); - set.add(provider()); - while (en.hasMoreElements()) - { - BufferedReader rdr = new BufferedReader(new InputStreamReader - (((URL) (en.nextElement())).openStream())); - while (true) - { - String s = rdr.readLine(); - if (s == null) - break; - CharsetProvider p = - (CharsetProvider) ((Class.forName(s)).newInstance()); - set.add(p); - } - } + while (i.hasNext()) + set.add(i.next()); providers = new CharsetProvider[set.size()]; set.toArray(providers); -- cgit v1.1