src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
author bpb
Wed, 17 Apr 2019 08:12:19 -0700
changeset 54569 7689e1cc56fe
parent 47216 71c04702a3d5
permissions -rw-r--r--
8220477: Channels.newWriter() does not close if underlying channel throws an IOException Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54569
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
54569
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    28
import java.io.FileOutputStream;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    29
import java.io.IOException;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    30
import java.io.OutputStream;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    31
import java.io.UnsupportedEncodingException;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    32
import java.io.Writer;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    33
import java.nio.ByteBuffer;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    34
import java.nio.CharBuffer;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    35
import java.nio.channels.WritableByteChannel;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    36
import java.nio.charset.Charset;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    37
import java.nio.charset.CharsetEncoder;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    38
import java.nio.charset.CoderResult;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    39
import java.nio.charset.CodingErrorAction;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
    40
import java.nio.charset.IllegalCharsetNameException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class StreamEncoder extends Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
    47
    private volatile boolean closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private void ensureOpen() throws IOException {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
    50
        if (closed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Factories for java.io.OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                                                      String charsetName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        String csn = charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (csn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (Charset.isSupported(csn))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                return new StreamEncoder(out, lock, Charset.forName(csn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } catch (IllegalCharsetNameException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        throw new UnsupportedEncodingException (csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                                      Charset cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return new StreamEncoder(out, lock, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                                      CharsetEncoder enc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return new StreamEncoder(out, lock, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // Factory for java.nio.channels.Channels.newWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static StreamEncoder forEncoder(WritableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                           CharsetEncoder enc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                           int minBufferCap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return new StreamEncoder(ch, enc, minBufferCap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // -- Public methods corresponding to those in OutputStreamWriter --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // All synchronization and state/argument checking is done in these public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // methods; the concrete stream-encoder subclasses defined below need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    // do any such checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public String getEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return encodingName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                implFlushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        char cbuf[] = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        cbuf[0] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        write(cbuf, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public void write(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            implWrite(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void write(String str, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        /* Check the len before creating a char buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        char cbuf[] = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        str.getChars(off, off + len, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        write(cbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   144
    public void write(CharBuffer cb) throws IOException {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   145
        int position = cb.position();
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   146
        try {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   147
            synchronized (lock) {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   148
                ensureOpen();
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   149
                implWrite(cb);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   150
            }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   151
        } finally {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   152
            cb.position(position);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   153
        }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   154
    }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            implFlush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        synchronized (lock) {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
   165
            if (closed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return;
54569
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   167
            try {
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   168
                implClose();
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   169
            } finally {
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   170
                closed = true;
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   171
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private boolean isOpen() {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
   176
        return !closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    // -- Charset-based stream encoder impl --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private ByteBuffer bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // Exactly one of these is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private final OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private WritableByteChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    // Leftover first char in a surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private boolean haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private char leftoverChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private CharBuffer lcb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private StreamEncoder(OutputStream out, Object lock, Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this(out, lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         cs.newEncoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         .onUnmappableCharacter(CodingErrorAction.REPLACE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private StreamEncoder(OutputStream out, Object lock, CharsetEncoder enc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        super(lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        this.ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // This path disabled until direct buffers are faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (false && out instanceof FileOutputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                ch = ((FileOutputStream)out).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    bb = ByteBuffer.allocateDirect(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (ch == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        bb = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    private StreamEncoder(WritableByteChannel ch, CharsetEncoder enc, int mbc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        this.out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this.ch = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        this.bb = ByteBuffer.allocate(mbc < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                                  ? DEFAULT_BYTE_BUFFER_SIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                                  : mbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private void writeBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (rem > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (ch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (ch.write(bb) != rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                assert false : rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            out.write(bb.array(), bb.arrayOffset() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private void flushLeftoverChar(CharBuffer cb, boolean endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (!haveLeftoverChar && !endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (lcb == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            lcb = CharBuffer.allocate(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            lcb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (haveLeftoverChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lcb.put(leftoverChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        if ((cb != null) && cb.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            lcb.put(cb.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        lcb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        while (lcb.hasRemaining() || endOfInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            CoderResult cr = encoder.encode(lcb, bb, endOfInput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (cr.isUnderflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if (lcb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    leftoverChar = lcb.get();
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   267
                    if (cb != null && cb.hasRemaining()) {
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   268
                        lcb.clear();
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   269
                        lcb.put(leftoverChar).put(cb.get()).flip();
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   270
                        continue;
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   271
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    void implWrite(char cbuf[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        CharBuffer cb = CharBuffer.wrap(cbuf, off, len);
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   290
        implWrite(cb);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   291
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   293
    void implWrite(CharBuffer cb)
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   294
        throws IOException
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   295
    {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   296
        if (haveLeftoverChar) {
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   297
            flushLeftoverChar(cb, false);
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   298
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        while (cb.hasRemaining()) {
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   301
            CoderResult cr = encoder.encode(cb, bb, false);
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   302
            if (cr.isUnderflow()) {
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   303
                assert (cb.remaining() <= 1) : cb.remaining();
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   304
                if (cb.remaining() == 1) {
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   305
                    haveLeftoverChar = true;
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   306
                    leftoverChar = cb.get();
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   307
                }
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   308
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
28850
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   310
            if (cr.isOverflow()) {
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   311
                assert bb.position() > 0;
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   312
                writeBytes();
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   313
                continue;
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   314
            }
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   315
            cr.throwException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    void implFlushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    void implFlush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        implFlushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        if (out != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    void implClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        flushLeftoverChar(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                CoderResult cr = encoder.flush(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                    assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                ch.close();
54569
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   349
            else {
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   350
                try {
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   351
                    out.flush();
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   352
                } finally {
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   353
                    out.close();
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   354
                }
7689e1cc56fe 8220477: Channels.newWriter() does not close if underlying channel throws an IOException
bpb
parents: 47216
diff changeset
   355
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            encoder.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    String encodingName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return ((cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            ? ((HistoricallyNamedCharset)cs).historicalName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            : cs.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}