jdk/src/java.base/share/classes/sun/nio/cs/StreamEncoder.java
author sherman
Mon, 09 Feb 2015 11:37:56 -0800
changeset 28850 4996a75e8bfb
parent 25859 3317bb8137f4
child 34390 feb9879cd993
permissions -rw-r--r--
8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError Summary: fix the surrogate corner case in SingleByte charset encoder Reviewed-by: psandoz, alanb
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private volatile boolean isOpen = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (!isOpen)
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            implFlush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (!isOpen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            isOpen = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return isOpen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // -- Charset-based stream encoder impl --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private Charset cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private CharsetEncoder encoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private ByteBuffer bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // Exactly one of these is non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private final OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private WritableByteChannel ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // Leftover first char in a surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private boolean haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private char leftoverChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private CharBuffer lcb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private StreamEncoder(OutputStream out, Object lock, Charset cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        this(out, lock,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         cs.newEncoder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         .onMalformedInput(CodingErrorAction.REPLACE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         .onUnmappableCharacter(CodingErrorAction.REPLACE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private StreamEncoder(OutputStream out, Object lock, CharsetEncoder enc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        super(lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        this.ch = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // This path disabled until direct buffers are faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (false && out instanceof FileOutputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ch = ((FileOutputStream)out).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    bb = ByteBuffer.allocateDirect(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (ch == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        bb = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private StreamEncoder(WritableByteChannel ch, CharsetEncoder enc, int mbc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        this.out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        this.ch = ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        this.cs = enc.charset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        this.encoder = enc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        this.bb = ByteBuffer.allocate(mbc < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                  ? DEFAULT_BYTE_BUFFER_SIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                  : mbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    private void writeBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int lim = bb.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int pos = bb.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        assert (pos <= lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int rem = (pos <= lim ? lim - pos : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (rem > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (ch != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            if (ch.write(bb) != rem)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                assert false : rem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            out.write(bb.array(), bb.arrayOffset() + pos, rem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private void flushLeftoverChar(CharBuffer cb, boolean endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (!haveLeftoverChar && !endOfInput)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (lcb == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            lcb = CharBuffer.allocate(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            lcb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (haveLeftoverChar)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            lcb.put(leftoverChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if ((cb != null) && cb.hasRemaining())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            lcb.put(cb.get());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        lcb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        while (lcb.hasRemaining() || endOfInput) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            CoderResult cr = encoder.encode(lcb, bb, endOfInput);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (cr.isUnderflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (lcb.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    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
   246
                    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
   247
                        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
   248
                        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
   249
                        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
   250
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        haveLeftoverChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    void implWrite(char cbuf[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        CharBuffer cb = CharBuffer.wrap(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        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
   271
            flushLeftoverChar(cb, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        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
   274
            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
   275
            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
   276
                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
   277
                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
   278
                    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
   279
                    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
   280
                }
4996a75e8bfb 8030179: java/nio/Buffer/Chars.java, testcases seems all pass but jtreg/testng failed with java.lang.AssertionError
sherman
parents: 25859
diff changeset
   281
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
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
   283
            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
   284
                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
   285
                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
   286
                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
   287
            }
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
            cr.throwException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    void implFlushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    void implFlush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        implFlushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (out != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    void implClose() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        flushLeftoverChar(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                CoderResult cr = encoder.flush(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (cr.isUnderflow())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                if (cr.isOverflow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    assert bb.position() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                cr.throwException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (bb.position() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                writeBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (ch != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            encoder.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    String encodingName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return ((cs instanceof HistoricallyNamedCharset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            ? ((HistoricallyNamedCharset)cs).historicalName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            : cs.name());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
}