From 8f523f3a1047919d3563daf1ef47ba87336ebe89 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Tue, 15 Nov 2005 23:20:01 +0000 Subject: Imported GNU Classpath 0.19 + gcj-import-20051115. * sources.am: Regenerated. * Makefile.in: Likewise. * scripts/makemake.tcl: Use glob -nocomplain. From-SVN: r107049 --- libjava/classpath/java/io/OutputStreamWriter.java | 160 +++++++++++++--------- 1 file changed, 99 insertions(+), 61 deletions(-) (limited to 'libjava/classpath/java/io/OutputStreamWriter.java') diff --git a/libjava/classpath/java/io/OutputStreamWriter.java b/libjava/classpath/java/io/OutputStreamWriter.java index ee229796cce..29fb631faf4 100644 --- a/libjava/classpath/java/io/OutputStreamWriter.java +++ b/libjava/classpath/java/io/OutputStreamWriter.java @@ -39,16 +39,14 @@ exception statement from your version. */ package java.io; import gnu.java.nio.charset.EncodingHelper; + import java.nio.ByteBuffer; import java.nio.CharBuffer; -import java.nio.charset.MalformedInputException; -import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.CharacterCodingException; -import java.nio.charset.IllegalCharsetNameException; -import java.nio.charset.CoderResult; -import java.nio.charset.CodingErrorAction; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.MalformedInputException; /** * This class writes characters to an output stream that is byte oriented @@ -124,52 +122,58 @@ public class OutputStreamWriter extends Writer { this.out = out; try - { - // Don't use NIO if avoidable - if(EncodingHelper.isISOLatin1(encoding_scheme)) { - encodingName = "ISO8859_1"; - encoder = null; - return; - } - - /* - * Workraround for encodings with a byte-order-mark. - * We only want to write it once per stream. - */ - try { - if(encoding_scheme.equalsIgnoreCase("UnicodeBig") || - encoding_scheme.equalsIgnoreCase("UTF-16") || - encoding_scheme.equalsIgnoreCase("UTF16")) - { - encoding_scheme = "UTF-16BE"; - out.write((byte)0xFE); - out.write((byte)0xFF); - } else if(encoding_scheme.equalsIgnoreCase("UnicodeLittle")){ - encoding_scheme = "UTF-16LE"; - out.write((byte)0xFF); - out.write((byte)0xFE); - } - } catch(IOException ioe){ - } + // Don't use NIO if avoidable + if(EncodingHelper.isISOLatin1(encoding_scheme)) + { + encodingName = "ISO8859_1"; + encoder = null; + return; + } - outputBuffer = CharBuffer.allocate(BUFFER_SIZE); + /* + * Workraround for encodings with a byte-order-mark. + * We only want to write it once per stream. + */ + try + { + if(encoding_scheme.equalsIgnoreCase("UnicodeBig") || + encoding_scheme.equalsIgnoreCase("UTF-16") || + encoding_scheme.equalsIgnoreCase("UTF16")) + { + encoding_scheme = "UTF-16BE"; + out.write((byte)0xFE); + out.write((byte)0xFF); + } + else if(encoding_scheme.equalsIgnoreCase("UnicodeLittle")){ + encoding_scheme = "UTF-16LE"; + out.write((byte)0xFF); + out.write((byte)0xFE); + } + } + catch(IOException ioe) + { + } + + outputBuffer = CharBuffer.allocate(BUFFER_SIZE); - Charset cs = EncodingHelper.getCharset(encoding_scheme); - if(cs == null) - throw new UnsupportedEncodingException("Encoding "+encoding_scheme+ - " unknown"); - encoder = cs.newEncoder(); - encodingName = EncodingHelper.getOldCanonical(cs.name()); + Charset cs = EncodingHelper.getCharset(encoding_scheme); + if(cs == null) + throw new UnsupportedEncodingException("Encoding "+encoding_scheme+ + " unknown"); + encoder = cs.newEncoder(); + encodingName = EncodingHelper.getOldCanonical(cs.name()); - encoder.onMalformedInput(CodingErrorAction.REPLACE); - encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); - } catch(RuntimeException e) { - // Default to ISO Latin-1, will happen if this is called, for instance, - // before the NIO provider is loadable. - encoder = null; - encodingName = "ISO8859_1"; - } + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + } + catch(RuntimeException e) + { + // Default to ISO Latin-1, will happen if this is called, for instance, + // before the NIO provider is loadable. + encoder = null; + encodingName = "ISO8859_1"; + } } /** @@ -183,21 +187,55 @@ public class OutputStreamWriter extends Writer this.out = out; outputBuffer = null; try - { - String encoding = System.getProperty("file.encoding"); - Charset cs = Charset.forName(encoding); - encoder = cs.newEncoder(); - encodingName = EncodingHelper.getOldCanonical(cs.name()); - } catch(RuntimeException e) { - encoder = null; - encodingName = "ISO8859_1"; - } + { + String encoding = System.getProperty("file.encoding"); + Charset cs = Charset.forName(encoding); + encoder = cs.newEncoder(); + encodingName = EncodingHelper.getOldCanonical(cs.name()); + } + catch(RuntimeException e) + { + encoder = null; + encodingName = "ISO8859_1"; + } + if(encoder != null) - { - encoder.onMalformedInput(CodingErrorAction.REPLACE); - encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); - outputBuffer = CharBuffer.allocate(BUFFER_SIZE); - } + { + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + outputBuffer = CharBuffer.allocate(BUFFER_SIZE); + } + } + + /** + * 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 = out; + encoder = cs.newEncoder(); + encoder.onMalformedInput(CodingErrorAction.REPLACE); + encoder.onUnmappableCharacter(CodingErrorAction.REPLACE); + outputBuffer = CharBuffer.allocate(BUFFER_SIZE); + } + + /** + * 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 = out; + encoder = enc; + outputBuffer = CharBuffer.allocate(BUFFER_SIZE); } /** -- cgit v1.2.1