jdk/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
author redestad
Mon, 21 Dec 2015 20:54:00 +0100
changeset 34774 03b4e6dc367b
parent 34390 feb9879cd993
permissions -rw-r--r--
8145680: Remove unnecessary explicit initialization of volatile variables in java.base Reviewed-by: alanb, chegar, jfranck, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 2005, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
package sun.nio.cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class StreamEncoder extends Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private static final int DEFAULT_BYTE_BUFFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
    41
    private volatile boolean closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private void ensureOpen() throws IOException {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
    44
        if (closed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Factories for java.io.OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                                      String charsetName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throws UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        String csn = charsetName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (csn == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            csn = Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (Charset.isSupported(csn))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                return new StreamEncoder(out, lock, Charset.forName(csn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } catch (IllegalCharsetNameException x) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        throw new UnsupportedEncodingException (csn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                                      Charset cs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return new StreamEncoder(out, lock, cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static StreamEncoder forOutputStreamWriter(OutputStream out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                                      Object lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                                      CharsetEncoder enc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return new StreamEncoder(out, lock, enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // Factory for java.nio.channels.Channels.newWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static StreamEncoder forEncoder(WritableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                           CharsetEncoder enc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                           int minBufferCap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return new StreamEncoder(ch, enc, minBufferCap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // -- Public methods corresponding to those in OutputStreamWriter --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    // All synchronization and state/argument checking is done in these public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // methods; the concrete stream-encoder subclasses defined below need not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // do any such checking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public String getEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return encodingName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                implFlushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        char cbuf[] = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        cbuf[0] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        write(cbuf, 0, 1);
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(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            implWrite(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public void write(String str, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /* Check the len before creating a char buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (len < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        char cbuf[] = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        str.getChars(off, off + len, cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        write(cbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   138
    public void write(CharBuffer cb) throws IOException {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   139
        int position = cb.position();
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   140
        try {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   141
            synchronized (lock) {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   142
                ensureOpen();
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   143
                implWrite(cb);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   144
            }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   145
        } finally {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   146
            cb.position(position);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   147
        }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   148
    }
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            implFlush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        synchronized (lock) {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
   159
            if (closed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            implClose();
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
   162
            closed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private boolean isOpen() {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 34390
diff changeset
   167
        return !closed;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // -- Charset-based stream encoder impl --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private ByteBuffer bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    // Exactly one of these is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private final OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private WritableByteChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // Leftover first char in a surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private boolean haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private char leftoverChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private CharBuffer lcb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private StreamEncoder(OutputStream out, Object lock, Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        this(out, lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
         cs.newEncoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
         .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
         .onUnmappableCharacter(CodingErrorAction.REPLACE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private StreamEncoder(OutputStream out, Object lock, CharsetEncoder enc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        super(lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // This path disabled until direct buffers are faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (false && out instanceof FileOutputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                ch = ((FileOutputStream)out).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    bb = ByteBuffer.allocateDirect(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (ch == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        bb = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    private StreamEncoder(WritableByteChannel ch, CharsetEncoder enc, int mbc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        this.out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        this.ch = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.bb = ByteBuffer.allocate(mbc < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                  ? DEFAULT_BYTE_BUFFER_SIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                  : mbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private void writeBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (rem > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (ch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (ch.write(bb) != rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                assert false : rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            out.write(bb.array(), bb.arrayOffset() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private void flushLeftoverChar(CharBuffer cb, boolean endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (!haveLeftoverChar && !endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (lcb == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            lcb = CharBuffer.allocate(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            lcb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (haveLeftoverChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            lcb.put(leftoverChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if ((cb != null) && cb.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            lcb.put(cb.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        lcb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        while (lcb.hasRemaining() || endOfInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            CoderResult cr = encoder.encode(lcb, bb, endOfInput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (cr.isUnderflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                if (lcb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    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
   258
                    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
   259
                        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
   260
                        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
   261
                        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
   262
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    void implWrite(char cbuf[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        CharBuffer cb = CharBuffer.wrap(cbuf, off, len);
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   281
        implWrite(cb);
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   282
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   284
    void implWrite(CharBuffer cb)
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   285
        throws IOException
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   286
    {
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   287
        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
   288
            flushLeftoverChar(cb, false);
34390
feb9879cd993 6856817: Poor performance of Writer#append with CharBuffer
vtewari
parents: 28850
diff changeset
   289
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        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
   292
            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
   293
            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
   294
                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
   295
                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
   296
                    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
   297
                    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
   298
                }
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   299
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
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
            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
   302
                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
   303
                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
   304
                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
   305
            }
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
            cr.throwException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    void implFlushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    void implFlush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        implFlushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if (out != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    void implClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        flushLeftoverChar(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                CoderResult cr = encoder.flush(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            encoder.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    String encodingName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return ((cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            ? ((HistoricallyNamedCharset)cs).historicalName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            : cs.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
}