jdk/src/share/classes/sun/nio/cs/UnicodeEncoder.java
author martin
Mon, 31 Aug 2009 15:00:04 -0700
changeset 3714 6a4eb8f53f91
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6860431: Character.isSurrogate(char ch) Summary: Add new method Character.isSurrogate(char ch) Reviewed-by: sherman, darcy, okutsu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (needsMark) {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            while (src.hasRemaining()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                char c = src.get();
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
    83
                if (!Character.isSurrogate(c)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    if (dst.remaining() < 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    mark++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    put(c, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                int d = sgp.parse(c, src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                if (d < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    return sgp.error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (dst.remaining() < 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    return CoderResult.OVERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                mark += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                put(Surrogate.high(d), dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                put(Surrogate.low(d), dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return CoderResult.UNDERFLOW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            src.position(mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    protected void implReset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        needsMark = usesMark;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public boolean canEncode(char c) {
3714
6a4eb8f53f91 6860431: Character.isSurrogate(char ch)
martin
parents: 2
diff changeset
   110
        return ! Character.isSurrogate(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}