Path: blob/master/test/jdk/sun/nio/cs/OLD/DoubleByteDecoder.java
41155 views
/*1* Copyright (c) 2002, 2012, 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*/2425/*26*/272829import java.nio.ByteBuffer;30import java.nio.CharBuffer;31import java.nio.charset.Charset;32import java.nio.charset.CharsetDecoder;33import java.nio.charset.CoderResult;3435abstract class DoubleByteDecoder36extends CharsetDecoder37{3839private short index1[];4041/*42* 2nd level index, provided by subclass43* every string has 0x10*(end-start+1) characters.44*/45private String index2[];4647protected int start;48protected int end;4950protected static final char REPLACE_CHAR = '\uFFFD';51protected char highSurrogate;52protected char lowSurrogate;5354protected DoubleByteDecoder(Charset cs, short[] index1, String[] index2,55int start, int end ) {56super(cs, 0.5f, 1.0f);57this.index1 = index1;58this.index2 = index2;59this.start = start;60this.end = end;61}6263private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {64byte[] sa = src.array();65int sp = src.arrayOffset() + src.position();66int sl = src.arrayOffset() + src.limit();67assert (sp <= sl);68sp = (sp <= sl ? sp : sl);69char[] da = dst.array();70int dp = dst.arrayOffset() + dst.position();71int dl = dst.arrayOffset() + dst.limit();72assert (dp <= dl);73dp = (dp <= dl ? dp : dl);7475try {76while (sp < sl) {77int b1, b2;78b1 = sa[sp];79int inputSize = 1;80int outputSize = 1;81highSurrogate = lowSurrogate = 0;82char c = decodeSingle(b1);83if (c == REPLACE_CHAR) {84b1 &= 0xff;85if (sl - sp < 2)86return CoderResult.UNDERFLOW;87b2 = sa[sp + 1] & 0xff;88c = decodeDouble(b1, b2);89inputSize = 2;90if (c == REPLACE_CHAR)91return CoderResult.unmappableForLength(inputSize);92outputSize = (highSurrogate > 0) ? 2: 1;93}9495if (dl - dp < outputSize)96return CoderResult.OVERFLOW;97if (outputSize == 2) {98da[dp++] = highSurrogate;99da[dp++] = lowSurrogate;100} else {101da[dp++] = c;102}103sp += inputSize;104}105return CoderResult.UNDERFLOW;106} finally {107src.position(sp - src.arrayOffset());108dst.position(dp - dst.arrayOffset());109}110}111112private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {113int mark = src.position();114int inputSize = 0;115int outputSize = 0;116try {117while (src.hasRemaining()) {118int b1 = src.get();119inputSize = 1;120outputSize = 1;121highSurrogate = lowSurrogate = 0;122123char c = decodeSingle(b1);124125if (c == REPLACE_CHAR) {126if (src.remaining() < 1)127return CoderResult.UNDERFLOW;128b1 &= 0xff;129int b2 = src.get() & 0xff;130inputSize = 2;131132c = decodeDouble(b1, b2);133134if (c == REPLACE_CHAR)135return CoderResult.unmappableForLength(2);136137outputSize = (highSurrogate > 0) ? 2: 1;138}139if (dst.remaining() < outputSize)140return CoderResult.OVERFLOW;141mark += inputSize;142143if (outputSize == 2) {144dst.put(highSurrogate);145dst.put(lowSurrogate);146} else {147dst.put(c);148}149}150return CoderResult.UNDERFLOW;151} finally {152src.position(mark);153}154}155156protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {157if (src.hasArray() && dst.hasArray())158return decodeArrayLoop(src, dst);159else160return decodeBufferLoop(src, dst);161}162163/*164* Can be changed by subclass165*/166protected char decodeSingle(int b) {167if (b >= 0)168return (char) b;169return REPLACE_CHAR;170}171172protected char decodeDouble(int byte1, int byte2) {173if (((byte1 < 0) || (byte1 > index1.length))174|| ((byte2 < start) || (byte2 > end)))175return REPLACE_CHAR;176177int n = (index1[byte1] & 0xf) * (end - start + 1) + (byte2 - start);178return index2[index1[byte1] >> 4].charAt(n);179}180}181182183