1306N/A/*
2362N/A * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
1306N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1306N/A *
1306N/A * This code is free software; you can redistribute it and/or modify it
1306N/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
1306N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1306N/A *
1306N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1306N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1306N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1306N/A * version 2 for more details (a copy is included in the LICENSE file that
1306N/A * accompanied this code).
1306N/A *
1306N/A * You should have received a copy of the GNU General Public License version
1306N/A * 2 along with this work; if not, write to the Free Software Foundation,
1306N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1306N/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.
1306N/A */
1306N/A
1306N/A/*
1306N/A */
1306N/A
1306N/A//package sun.nio.cs.ext;
1306N/A
1306N/Aimport java.nio.ByteBuffer;
1306N/Aimport java.nio.CharBuffer;
1306N/Aimport java.nio.charset.Charset;
1306N/Aimport java.nio.charset.CharsetDecoder;
1306N/Aimport java.nio.charset.CoderResult;
1306N/A
1306N/Aabstract class DoubleByteDecoder
1306N/A extends CharsetDecoder
1306N/A{
1306N/A
1306N/A private short index1[];
1306N/A
1306N/A /*
1306N/A * 2nd level index, provided by subclass
1306N/A * every string has 0x10*(end-start+1) characters.
1306N/A */
1306N/A private String index2[];
1306N/A
1306N/A protected int start;
1306N/A protected int end;
1306N/A
1306N/A protected static final char REPLACE_CHAR = '\uFFFD';
1306N/A protected char highSurrogate;
1306N/A protected char lowSurrogate;
1306N/A
1306N/A protected DoubleByteDecoder(Charset cs, short[] index1, String[] index2,
1306N/A int start, int end ) {
1306N/A super(cs, 0.5f, 1.0f);
1306N/A this.index1 = index1;
1306N/A this.index2 = index2;
1306N/A this.start = start;
1306N/A this.end = end;
1306N/A }
1306N/A
1306N/A private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
1306N/A byte[] sa = src.array();
1306N/A int sp = src.arrayOffset() + src.position();
1306N/A int sl = src.arrayOffset() + src.limit();
1306N/A assert (sp <= sl);
1306N/A sp = (sp <= sl ? sp : sl);
1306N/A char[] da = dst.array();
1306N/A int dp = dst.arrayOffset() + dst.position();
1306N/A int dl = dst.arrayOffset() + dst.limit();
1306N/A assert (dp <= dl);
1306N/A dp = (dp <= dl ? dp : dl);
1306N/A
1306N/A try {
1306N/A while (sp < sl) {
1306N/A int b1, b2;
1306N/A b1 = sa[sp];
1306N/A int inputSize = 1;
1306N/A int outputSize = 1;
1306N/A highSurrogate = lowSurrogate = 0;
1306N/A char c = decodeSingle(b1);
1306N/A if (c == REPLACE_CHAR) {
1306N/A b1 &= 0xff;
1306N/A if (sl - sp < 2)
1306N/A return CoderResult.UNDERFLOW;
1306N/A b2 = sa[sp + 1] & 0xff;
1306N/A c = decodeDouble(b1, b2);
1306N/A inputSize = 2;
1306N/A if (c == REPLACE_CHAR)
1306N/A return CoderResult.unmappableForLength(inputSize);
1306N/A outputSize = (highSurrogate > 0) ? 2: 1;
1306N/A }
1306N/A
1306N/A if (dl - dp < outputSize)
1306N/A return CoderResult.OVERFLOW;
1306N/A if (outputSize == 2) {
1306N/A da[dp++] = highSurrogate;
1306N/A da[dp++] = lowSurrogate;
1306N/A } else {
1306N/A da[dp++] = c;
1306N/A }
1306N/A sp += inputSize;
1306N/A }
1306N/A return CoderResult.UNDERFLOW;
1306N/A } finally {
1306N/A src.position(sp - src.arrayOffset());
1306N/A dst.position(dp - dst.arrayOffset());
1306N/A }
1306N/A }
1306N/A
1306N/A private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
1306N/A int mark = src.position();
1306N/A int inputSize = 0;
1306N/A int outputSize = 0;
1306N/A try {
1306N/A while (src.hasRemaining()) {
1306N/A int b1 = src.get();
1306N/A inputSize = 1;
1306N/A outputSize = 1;
1306N/A highSurrogate = lowSurrogate = 0;
1306N/A
1306N/A char c = decodeSingle(b1);
1306N/A
1306N/A if (c == REPLACE_CHAR) {
1306N/A if (src.remaining() < 1)
1306N/A return CoderResult.UNDERFLOW;
1306N/A b1 &= 0xff;
1306N/A int b2 = src.get() & 0xff;
1306N/A inputSize = 2;
1306N/A
1306N/A c = decodeDouble(b1, b2);
1306N/A
1306N/A if (c == REPLACE_CHAR)
1306N/A return CoderResult.unmappableForLength(2);
1306N/A
1306N/A outputSize = (highSurrogate > 0) ? 2: 1;
1306N/A }
1306N/A if (dst.remaining() < outputSize)
1306N/A return CoderResult.OVERFLOW;
1306N/A mark += inputSize;
1306N/A
1306N/A if (outputSize == 2) {
1306N/A dst.put(highSurrogate);
1306N/A dst.put(lowSurrogate);
1306N/A } else {
1306N/A dst.put(c);
1306N/A }
1306N/A }
1306N/A return CoderResult.UNDERFLOW;
1306N/A } finally {
1306N/A src.position(mark);
1306N/A }
1306N/A }
1306N/A
1306N/A protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
1306N/A if (src.hasArray() && dst.hasArray())
1306N/A return decodeArrayLoop(src, dst);
1306N/A else
1306N/A return decodeBufferLoop(src, dst);
1306N/A }
1306N/A
1306N/A /*
1306N/A * Can be changed by subclass
1306N/A */
1306N/A protected char decodeSingle(int b) {
1306N/A if (b >= 0)
1306N/A return (char) b;
1306N/A return REPLACE_CHAR;
1306N/A }
1306N/A
1306N/A protected char decodeDouble(int byte1, int byte2) {
1306N/A if (((byte1 < 0) || (byte1 > index1.length))
1306N/A || ((byte2 < start) || (byte2 > end)))
1306N/A return REPLACE_CHAR;
1306N/A
1306N/A int n = (index1[byte1] & 0xf) * (end - start + 1) + (byte2 - start);
1306N/A return index2[index1[byte1] >> 4].charAt(n);
1306N/A }
1306N/A}