< prev index next >

src/java.base/share/classes/sun/nio/cs/StreamEncoder.java

Print this page


   1 /*
   2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 



  26 package sun.nio.cs;
  27 
  28 import java.io.FileOutputStream;
  29 import java.io.IOException;
  30 import java.io.OutputStream;
  31 import java.io.UnsupportedEncodingException;
  32 import java.io.Writer;
  33 import java.nio.ByteBuffer;
  34 import java.nio.CharBuffer;
  35 import java.nio.channels.WritableByteChannel;
  36 import java.nio.charset.Charset;
  37 import java.nio.charset.CharsetEncoder;
  38 import java.nio.charset.CoderResult;
  39 import java.nio.charset.CodingErrorAction;
  40 import java.nio.charset.IllegalCharsetNameException;
  41 
  42 public class StreamEncoder extends Writer
  43 {
  44 
  45     private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
  46 
  47     private volatile boolean closed;
  48 
  49     private void ensureOpen() throws IOException {
  50         if (closed)
  51             throw new IOException("Stream closed");
  52     }
  53 
  54     // Factories for java.io.OutputStreamWriter
  55     public static StreamEncoder forOutputStreamWriter(OutputStream out,
  56                                                       Object lock,
  57                                                       String charsetName)
  58         throws UnsupportedEncodingException
  59     {
  60         String csn = charsetName;


 147             synchronized (lock) {
 148                 ensureOpen();
 149                 implWrite(cb);
 150             }
 151         } finally {
 152             cb.position(position);
 153         }
 154     }
 155 
 156     public void flush() throws IOException {
 157         synchronized (lock) {
 158             ensureOpen();
 159             implFlush();
 160         }
 161     }
 162 
 163     public void close() throws IOException {
 164         synchronized (lock) {
 165             if (closed)
 166                 return;
 167             try {
 168                 implClose();
 169             } finally {
 170                 closed = true;
 171             }
 172         }
 173     }
 174 
 175     private boolean isOpen() {
 176         return !closed;
 177     }
 178 
 179 
 180     // -- Charset-based stream encoder impl --
 181 
 182     private Charset cs;
 183     private CharsetEncoder encoder;
 184     private ByteBuffer bb;
 185 
 186     // Exactly one of these is non-null
 187     private final OutputStream out;
 188     private WritableByteChannel ch;
 189 
 190     // Leftover first char in a surrogate pair
 191     private boolean haveLeftoverChar = false;


 329 
 330     void implClose() throws IOException {
 331         flushLeftoverChar(null, true);
 332         try {
 333             for (;;) {
 334                 CoderResult cr = encoder.flush(bb);
 335                 if (cr.isUnderflow())
 336                     break;
 337                 if (cr.isOverflow()) {
 338                     assert bb.position() > 0;
 339                     writeBytes();
 340                     continue;
 341                 }
 342                 cr.throwException();
 343             }
 344 
 345             if (bb.position() > 0)
 346                 writeBytes();
 347             if (ch != null)
 348                 ch.close();
 349             else {
 350                 try {
 351                     out.flush();
 352                 } finally {
 353                     out.close();
 354                 }
 355             }
 356         } catch (IOException x) {
 357             encoder.reset();
 358             throw x;
 359         }
 360     }
 361 
 362     String encodingName() {
 363         return ((cs instanceof HistoricallyNamedCharset)
 364             ? ((HistoricallyNamedCharset)cs).historicalName()
 365             : cs.name());
 366     }
 367 }
   1 /*
   2  * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  */
  28 
  29 package sun.nio.cs;
  30 
  31 import java.io.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.nio.charset.*;









  35 
  36 public class StreamEncoder extends Writer
  37 {
  38 
  39     private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
  40 
  41     private volatile boolean closed;
  42 
  43     private void ensureOpen() throws IOException {
  44         if (closed)
  45             throw new IOException("Stream closed");
  46     }
  47 
  48     // Factories for java.io.OutputStreamWriter
  49     public static StreamEncoder forOutputStreamWriter(OutputStream out,
  50                                                       Object lock,
  51                                                       String charsetName)
  52         throws UnsupportedEncodingException
  53     {
  54         String csn = charsetName;


 141             synchronized (lock) {
 142                 ensureOpen();
 143                 implWrite(cb);
 144             }
 145         } finally {
 146             cb.position(position);
 147         }
 148     }
 149 
 150     public void flush() throws IOException {
 151         synchronized (lock) {
 152             ensureOpen();
 153             implFlush();
 154         }
 155     }
 156 
 157     public void close() throws IOException {
 158         synchronized (lock) {
 159             if (closed)
 160                 return;
 161             implClose();
 162             closed = true;



 163         }
 164     }
 165 
 166     private boolean isOpen() {
 167         return !closed;
 168     }
 169 
 170 
 171     // -- Charset-based stream encoder impl --
 172 
 173     private Charset cs;
 174     private CharsetEncoder encoder;
 175     private ByteBuffer bb;
 176 
 177     // Exactly one of these is non-null
 178     private final OutputStream out;
 179     private WritableByteChannel ch;
 180 
 181     // Leftover first char in a surrogate pair
 182     private boolean haveLeftoverChar = false;


 320 
 321     void implClose() throws IOException {
 322         flushLeftoverChar(null, true);
 323         try {
 324             for (;;) {
 325                 CoderResult cr = encoder.flush(bb);
 326                 if (cr.isUnderflow())
 327                     break;
 328                 if (cr.isOverflow()) {
 329                     assert bb.position() > 0;
 330                     writeBytes();
 331                     continue;
 332                 }
 333                 cr.throwException();
 334             }
 335 
 336             if (bb.position() > 0)
 337                 writeBytes();
 338             if (ch != null)
 339                 ch.close();
 340             else
 341                 out.close();





 342         } catch (IOException x) {
 343             encoder.reset();
 344             throw x;
 345         }
 346     }
 347 
 348     String encodingName() {
 349         return ((cs instanceof HistoricallyNamedCharset)
 350             ? ((HistoricallyNamedCharset)cs).historicalName()
 351             : cs.name());
 352     }
 353 }
< prev index next >