Path: blob/master/src/java.base/share/classes/sun/nio/cs/UTF_32Coder.java
41159 views
/*1* Copyright (c) 2005, 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.ByteBuffer;28import java.nio.CharBuffer;29import java.nio.charset.Charset;30import java.nio.charset.CoderResult;31import java.nio.charset.CharsetDecoder;32import java.nio.charset.CharsetEncoder;3334class UTF_32Coder {35protected static final int BOM_BIG = 0xFEFF;36protected static final int BOM_LITTLE = 0xFFFE0000;37protected static final int NONE = 0;38protected static final int BIG = 1;39protected static final int LITTLE = 2;4041protected static class Decoder extends CharsetDecoder {42private int currentBO;43private int expectedBO;4445protected Decoder(Charset cs, int bo) {46super(cs, 0.25f, 1.0f);47this.expectedBO = bo;48this.currentBO = NONE;49}5051private int getCP(ByteBuffer src) {52return (currentBO==BIG)53?(((src.get() & 0xff) << 24) |54((src.get() & 0xff) << 16) |55((src.get() & 0xff) << 8) |56(src.get() & 0xff))57:((src.get() & 0xff) |58((src.get() & 0xff) << 8) |59((src.get() & 0xff) << 16) |60((src.get() & 0xff) << 24));61}6263protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {64if (src.remaining() < 4)65return CoderResult.UNDERFLOW;66int mark = src.position();67int cp;68try {69if (currentBO == NONE) {70cp = ((src.get() & 0xff) << 24) |71((src.get() & 0xff) << 16) |72((src.get() & 0xff) << 8) |73(src.get() & 0xff);74if (cp == BOM_BIG && expectedBO != LITTLE) {75currentBO = BIG;76mark += 4;77} else if (cp == BOM_LITTLE && expectedBO != BIG) {78currentBO = LITTLE;79mark += 4;80} else {81if (expectedBO == NONE)82currentBO = BIG;83else84currentBO = expectedBO;85src.position(mark);86}87}88while (src.remaining() >= 4) {89cp = getCP(src);90if (Character.isBmpCodePoint(cp)) {91if (!dst.hasRemaining())92return CoderResult.OVERFLOW;93mark += 4;94dst.put((char) cp);95} else if (Character.isValidCodePoint(cp)) {96if (dst.remaining() < 2)97return CoderResult.OVERFLOW;98mark += 4;99dst.put(Character.highSurrogate(cp));100dst.put(Character.lowSurrogate(cp));101} else {102return CoderResult.malformedForLength(4);103}104}105return CoderResult.UNDERFLOW;106} finally {107src.position(mark);108}109}110protected void implReset() {111currentBO = NONE;112}113}114115protected static class Encoder extends CharsetEncoder {116private boolean doBOM = false;117private boolean doneBOM = true;118private int byteOrder;119120protected void put(int cp, ByteBuffer dst) {121if (byteOrder==BIG) {122dst.put((byte)(cp >> 24));123dst.put((byte)(cp >> 16));124dst.put((byte)(cp >> 8));125dst.put((byte)cp);126} else {127dst.put((byte)cp);128dst.put((byte)(cp >> 8));129dst.put((byte)(cp >> 16));130dst.put((byte)(cp >> 24));131}132}133134protected Encoder(Charset cs, int byteOrder, boolean doBOM) {135super(cs, 4.0f,136doBOM?8.0f:4.0f,137(byteOrder==BIG)?new byte[]{(byte)0, (byte)0, (byte)0xff, (byte)0xfd}138:new byte[]{(byte)0xfd, (byte)0xff, (byte)0, (byte)0});139this.byteOrder = byteOrder;140this.doBOM = doBOM;141this.doneBOM = !doBOM;142}143144protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {145int mark = src.position();146if (!doneBOM && src.hasRemaining()) {147if (dst.remaining() < 4)148return CoderResult.OVERFLOW;149put(BOM_BIG, dst);150doneBOM = true;151}152try {153while (src.hasRemaining()) {154char c = src.get();155if (!Character.isSurrogate(c)) {156if (dst.remaining() < 4)157return CoderResult.OVERFLOW;158mark++;159put(c, dst);160} else if (Character.isHighSurrogate(c)) {161if (!src.hasRemaining())162return CoderResult.UNDERFLOW;163char low = src.get();164if (Character.isLowSurrogate(low)) {165if (dst.remaining() < 4)166return CoderResult.OVERFLOW;167mark += 2;168put(Character.toCodePoint(c, low), dst);169} else {170return CoderResult.malformedForLength(1);171}172} else {173// assert Character.isLowSurrogate(c);174return CoderResult.malformedForLength(1);175}176}177return CoderResult.UNDERFLOW;178} finally {179src.position(mark);180}181}182183protected void implReset() {184doneBOM = !doBOM;185}186187}188}189190191