src/java.base/share/classes/sun/nio/cs/UnicodeEncoder.java
author martin
Wed, 28 Mar 2018 21:14:06 -0700
changeset 49443 e5679a6661d6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets Reviewed-by: sherman, ulfzibis
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7542
diff changeset
     2
 * Copyright (c) 2000, 2010, 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: 3714
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: 3714
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: 3714
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3714
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Base class for different flavors of UTF-16 encoders
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public abstract class UnicodeEncoder extends CharsetEncoder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    protected static final char BYTE_ORDER_MARK = '\uFEFF';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    protected static final char REVERSED_MARK = '\uFFFE';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    protected static final int BIG = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected static final int LITTLE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int byteOrder;      /* Byte order in use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private boolean usesMark;   /* Write an initial BOM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private boolean needsMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected UnicodeEncoder(Charset cs, int bo, boolean m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(cs, 2.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
              // Four bytes max if you need a BOM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
              m ? 4.0f : 2.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
              // Replacement depends upon byte order
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
              ((bo == BIG)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
               ? new byte[] { (byte)0xff, (byte)0xfd }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
               : new byte[] { (byte)0xfd, (byte)0xff }));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        usesMark = needsMark = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        byteOrder = bo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private void put(char c, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (byteOrder == BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            dst.put((byte)(c >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            dst.put((byte)(c & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            dst.put((byte)(c & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            dst.put((byte)(c >> 8));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final Surrogate.Parser sgp = new Surrogate.Parser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int mark = src.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
7542
514d58003c20 6415373: (cs) UnicodeEncoder emits BOM when there are no bytes to encode
sherman
parents: 5991
diff changeset
    73
        if (needsMark && src.hasRemaining()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            put(BYTE_ORDER_MARK, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            needsMark = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                char c = src.get();
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
    82
                if (!Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    put(c, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                int d = sgp.parse(c, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (d < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (dst.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                mark += 2;
5991
288afdbbca28 6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents: 5506
diff changeset
    95
                put(Character.highSurrogate(d), dst);
288afdbbca28 6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents: 5506
diff changeset
    96
                put(Character.lowSurrogate(d), dst);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        needsMark = usesMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public boolean canEncode(char c) {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   109
        return ! Character.isSurrogate(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}