diff options
author | David Daney <ddaney@avtrex.com> | 2006-01-03 22:58:31 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2006-01-03 22:58:31 +0000 |
commit | 8ceb88d4cd6ba729b38dee3bda925ab34a75efc9 (patch) | |
tree | 82dbb00c69057da2d374d746d77eb40854b23164 /libjava/gnu/gcj | |
parent | 368872c3154af66d44a984737b0119d88431c64f (diff) | |
download | gcc-8ceb88d4cd6ba729b38dee3bda925ab34a75efc9.zip gcc-8ceb88d4cd6ba729b38dee3bda925ab34a75efc9.tar.gz gcc-8ceb88d4cd6ba729b38dee3bda925ab34a75efc9.tar.bz2 |
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 <tromey@redhat.com>
From-SVN: r109294
Diffstat (limited to 'libjava/gnu/gcj')
-rw-r--r-- | libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java | 90 | ||||
-rw-r--r-- | libjava/gnu/gcj/convert/BytesToUnicode.java | 15 | ||||
-rw-r--r-- | libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java | 150 | ||||
-rw-r--r-- | libjava/gnu/gcj/convert/UnicodeToBytes.java | 20 |
4 files changed, 270 insertions, 5 deletions
diff --git a/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java b/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java new file mode 100644 index 0000000..78ba848 --- /dev/null +++ b/libjava/gnu/gcj/convert/BytesToCharsetAdaptor.java @@ -0,0 +1,90 @@ +/* Copyright (C) 2005 Free Software Foundation + +This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package gnu.gcj.convert; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.CoderResult; +import gnu.java.nio.charset.EncodingHelper; + +/** + * Adaptor class that allow any {@link Charset} to be used + * as a BytesToUnicode converter. + */ +public class BytesToCharsetAdaptor extends BytesToUnicode +{ + /** + * The CharsetDecoder that does all the work. + */ + private final CharsetDecoder decoder; + + /** + * ByteBuffer wrapper for this.buf. + */ + private ByteBuffer inBuf; + + /** + * Create a new BytesToCharsetAdaptor for the given Charset. + * + * @param cs the Charset. + */ + public BytesToCharsetAdaptor(Charset cs) + { + this(cs.newDecoder()); + } + + /** + * Create a new BytesToCharsetAdaptor for the given CharsetDecoder. + * + * @param dec the CharsetDecoder. + */ + public BytesToCharsetAdaptor(CharsetDecoder dec) + { + decoder = dec; + // Use default replacments on bad input so that we don't have to + // deal with errors. + decoder.onMalformedInput(CodingErrorAction.REPLACE); + decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } + + /** + * Return the decoder's name. The backing Charset's name is + * returned. + * + * @return The name. + */ + public String getName() + { + return EncodingHelper.getOldCanonical(decoder.charset().name()); + } + + public int read(char[] outbuffer, int outpos, int count) + { + if (inBuf == null || ! inBuf.hasArray() || inBuf.array() != inbuffer) + inBuf = ByteBuffer.wrap(inbuffer); + inBuf.limit(inpos + inlength); + inBuf.position(inpos); + + CharBuffer outBuf = CharBuffer.wrap(outbuffer, outpos, count); + decoder.decode(inBuf, outBuf, false); + + // Update this.inpos to reflect the bytes consumed. + inpos = inBuf.position(); + // Return the number of characters that were written to outbuffer. + return outBuf.position() - outpos; + } + + // These aren't cached. + public void done() + { + } +} diff --git a/libjava/gnu/gcj/convert/BytesToUnicode.java b/libjava/gnu/gcj/convert/BytesToUnicode.java index f33720a..e3afe3e 100644 --- a/libjava/gnu/gcj/convert/BytesToUnicode.java +++ b/libjava/gnu/gcj/convert/BytesToUnicode.java @@ -1,4 +1,4 @@ -/* Copyright (C) 1999, 2000, 2001 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2005 Free Software Foundation This file is part of libgcj. @@ -8,6 +8,8 @@ details. */ package gnu.gcj.convert; +import java.nio.charset.Charset; + public abstract class BytesToUnicode extends IOConverter { /** Buffer to read bytes from. @@ -104,11 +106,20 @@ public abstract class BytesToUnicode extends IOConverter try { // We pass the original name to iconv and let it handle - // its own aliasing. + // its own aliasing. Note that we intentionally prefer + // iconv over nio. return new Input_iconv (encoding); } catch (Throwable _) { + // Ignore, and try the next method. + } + try + { + return new BytesToCharsetAdaptor(Charset.forName(encoding)); + } + catch (Throwable _) + { throw new java.io.UnsupportedEncodingException(encoding + " (" + ex + ')'); } diff --git a/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java b/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java new file mode 100644 index 0000000..4e9bcd5 --- /dev/null +++ b/libjava/gnu/gcj/convert/CharsetToBytesAdaptor.java @@ -0,0 +1,150 @@ +/* Copyright (C) 2005, 2006 Free Software Foundation + +This file is part of libgcj. + +This software is copyrighted work licensed under the terms of the +Libgcj License. Please consult the file "LIBGCJ_LICENSE" for +details. */ + +package gnu.gcj.convert; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.CoderResult; +import gnu.java.nio.charset.EncodingHelper; + +/** + * Adaptor class that allow any {@link Charset} to be used + * as a UnicodeToBytes converter. + */ +public class CharsetToBytesAdaptor extends UnicodeToBytes +{ + /** + * The CharsetEncoder that does all the work. + */ + private final CharsetEncoder encoder; + + /** + * ByteBuffer wrapper for this.buf. + */ + private ByteBuffer outBuf; + + /** + * True if we've told the CharsetEncoder that there are no more + * characters available. + */ + private boolean closedEncoder; + + /** + * True if we're finished. + */ + private boolean finished; + + /** + * Create a new CharsetToBytesAdaptor for the given Charset. + * + * @param cs The Charset. + */ + public CharsetToBytesAdaptor(Charset cs) + { + this(cs.newEncoder()); + } + + /** + * Create a new CharsetToBytesAdaptor for the given CharsetEncoder. + * + * @param enc The CharsetEncoder. + */ + public CharsetToBytesAdaptor(CharsetEncoder enc) + { + encoder = enc; + // Use default replacments on bad input so that we don't have to + // deal with errors. + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } + + /** + * Return the encoder's name. The backing Charset's name is + * returned. + * + * @return The name. + */ + public String getName() + { + return EncodingHelper.getOldCanonical(encoder.charset().name()); + } + + public int write (char[] inbuffer, int inpos, int inlength) + { + // Wrap the char array so it can be used by the encoder. + CharBuffer b = CharBuffer.wrap(inbuffer, inpos, inlength); + write(b); + return b.position() - inpos; // Number of chars consumed. + } + + public int write (String str, int inpos, int inlength, char work) + { + // Wrap the String so it can be used by the encoder. + CharBuffer b = CharBuffer.wrap(str, inpos, inlength); + write(b); + return b.position() - inpos; // Number of chars consumed. + } + + /** + * Encode as much of inBuf as will fit in buf. The number of + * chars consumed is reflected by the new position of inBuf. The + * output is put in buf and count is incremented by the number of + * bytes written. + * + * @param inBuf The input. + */ + private void write(CharBuffer inBuf) + { + // Reuse existing outBuf if it is still wrapping the same array + // it was created with. + if (outBuf == null || !outBuf.hasArray() || outBuf.array() != buf) + outBuf = ByteBuffer.wrap(buf); + + // Set the current position. + outBuf.position(count); + + // If we've already said that there is no more input available, + // then we simply try to flush again. + if (closedEncoder) + { + CoderResult result = encoder.flush(outBuf); + if (result == CoderResult.UNDERFLOW) + finished = true; + } + else + { + // Do the conversion. If there are no characters to write, + // then we are finished. + closedEncoder = ! inBuf.hasRemaining(); + encoder.encode(inBuf, outBuf, closedEncoder); + } + + // Mark the new end of buf. + count = outBuf.position(); + } + + /** + * Check for cached output in the converter. + * + * @return true if there is cached output that has not been + * written to buf. + */ + public boolean havePendingBytes() + { + return ! finished; + } + + // These aren't cached. + public void done() + { + } +} diff --git a/libjava/gnu/gcj/convert/UnicodeToBytes.java b/libjava/gnu/gcj/convert/UnicodeToBytes.java index b143258..8522bec 100644 --- a/libjava/gnu/gcj/convert/UnicodeToBytes.java +++ b/libjava/gnu/gcj/convert/UnicodeToBytes.java @@ -1,4 +1,4 @@ -/* Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation +/* Copyright (C) 1999, 2000, 2001, 2003, 2005 Free Software Foundation This file is part of libgcj. @@ -8,6 +8,8 @@ details. */ package gnu.gcj.convert; +import java.nio.charset.Charset; + public abstract class UnicodeToBytes extends IOConverter { /** Buffer to emit bytes to. @@ -99,10 +101,22 @@ public abstract class UnicodeToBytes extends IOConverter catch (Throwable ex) { try - { + { // We pass the original name to iconv and let it handle - // its own aliasing. + // its own aliasing. Note that we intentionally prefer + // iconv over nio. return new Output_iconv (encoding); + } + catch (Throwable _) + { + // Ignore, and try the next method. + } + try + { + // Try using finding java.nio.charset.Charset and using + // the adaptor. Use the original name as Charsets have + // their own canonical names. + return new CharsetToBytesAdaptor(Charset.forName(encoding)); } catch (Throwable _) { |