0N/A/*
3261N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.nio.cs;
0N/A
0N/Aimport java.nio.*;
0N/Aimport java.nio.charset.*;
0N/A
0N/A/**
0N/A * Base class for different flavors of UTF-16 encoders
0N/A */
0N/Apublic abstract class UnicodeEncoder extends CharsetEncoder {
0N/A
0N/A protected static final char BYTE_ORDER_MARK = '\uFEFF';
0N/A protected static final char REVERSED_MARK = '\uFFFE';
0N/A
0N/A protected static final int BIG = 0;
0N/A protected static final int LITTLE = 1;
0N/A
0N/A private int byteOrder; /* Byte order in use */
0N/A private boolean usesMark; /* Write an initial BOM */
0N/A private boolean needsMark;
0N/A
0N/A protected UnicodeEncoder(Charset cs, int bo, boolean m) {
0N/A super(cs, 2.0f,
0N/A // Four bytes max if you need a BOM
0N/A m ? 4.0f : 2.0f,
0N/A // Replacement depends upon byte order
0N/A ((bo == BIG)
0N/A ? new byte[] { (byte)0xff, (byte)0xfd }
0N/A : new byte[] { (byte)0xfd, (byte)0xff }));
0N/A usesMark = needsMark = m;
0N/A byteOrder = bo;
0N/A }
0N/A
0N/A private void put(char c, ByteBuffer dst) {
0N/A if (byteOrder == BIG) {
0N/A dst.put((byte)(c >> 8));
0N/A dst.put((byte)(c & 0xff));
0N/A } else {
0N/A dst.put((byte)(c & 0xff));
0N/A dst.put((byte)(c >> 8));
0N/A }
0N/A }
0N/A
0N/A private final Surrogate.Parser sgp = new Surrogate.Parser();
0N/A
0N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
0N/A int mark = src.position();
0N/A
3227N/A if (needsMark && src.hasRemaining()) {
0N/A if (dst.remaining() < 2)
0N/A return CoderResult.OVERFLOW;
0N/A put(BYTE_ORDER_MARK, dst);
0N/A needsMark = false;
0N/A }
0N/A try {
0N/A while (src.hasRemaining()) {
0N/A char c = src.get();
1602N/A if (!Character.isSurrogate(c)) {
0N/A if (dst.remaining() < 2)
0N/A return CoderResult.OVERFLOW;
0N/A mark++;
0N/A put(c, dst);
0N/A continue;
0N/A }
0N/A int d = sgp.parse(c, src);
0N/A if (d < 0)
0N/A return sgp.error();
0N/A if (dst.remaining() < 4)
0N/A return CoderResult.OVERFLOW;
0N/A mark += 2;
2567N/A put(Character.highSurrogate(d), dst);
2567N/A put(Character.lowSurrogate(d), dst);
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(mark);
0N/A }
0N/A }
0N/A
0N/A protected void implReset() {
0N/A needsMark = usesMark;
0N/A }
0N/A
0N/A public boolean canEncode(char c) {
1602N/A return ! Character.isSurrogate(c);
0N/A }
0N/A}