Path: blob/master/src/java.base/share/classes/sun/nio/cs/UnicodeEncoder.java
41159 views
/*1* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.nio.cs;2627import java.nio.*;28import java.nio.charset.*;2930/**31* Base class for different flavors of UTF-16 encoders32*/33public abstract class UnicodeEncoder extends CharsetEncoder {3435protected static final char BYTE_ORDER_MARK = '\uFEFF';36protected static final char REVERSED_MARK = '\uFFFE';3738protected static final int BIG = 0;39protected static final int LITTLE = 1;4041private int byteOrder; /* Byte order in use */42private boolean usesMark; /* Write an initial BOM */43private boolean needsMark;4445protected UnicodeEncoder(Charset cs, int bo, boolean m) {46super(cs, 2.0f,47// Four bytes max if you need a BOM48m ? 4.0f : 2.0f,49// Replacement depends upon byte order50((bo == BIG)51? new byte[] { (byte)0xff, (byte)0xfd }52: new byte[] { (byte)0xfd, (byte)0xff }));53usesMark = needsMark = m;54byteOrder = bo;55}5657private void put(char c, ByteBuffer dst) {58if (byteOrder == BIG) {59dst.put((byte)(c >> 8));60dst.put((byte)(c & 0xff));61} else {62dst.put((byte)(c & 0xff));63dst.put((byte)(c >> 8));64}65}6667private final Surrogate.Parser sgp = new Surrogate.Parser();6869protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {70int mark = src.position();7172if (needsMark && src.hasRemaining()) {73if (dst.remaining() < 2)74return CoderResult.OVERFLOW;75put(BYTE_ORDER_MARK, dst);76needsMark = false;77}78try {79while (src.hasRemaining()) {80char c = src.get();81if (!Character.isSurrogate(c)) {82if (dst.remaining() < 2)83return CoderResult.OVERFLOW;84mark++;85put(c, dst);86continue;87}88int d = sgp.parse(c, src);89if (d < 0)90return sgp.error();91if (dst.remaining() < 4)92return CoderResult.OVERFLOW;93mark += 2;94put(Character.highSurrogate(d), dst);95put(Character.lowSurrogate(d), dst);96}97return CoderResult.UNDERFLOW;98} finally {99src.position(mark);100}101}102103protected void implReset() {104needsMark = usesMark;105}106107public boolean canEncode(char c) {108return ! Character.isSurrogate(c);109}110}111112113