Path: blob/master/test/jdk/sun/nio/cs/OLD/SimpleEUCDecoder.java
41155 views
/*1* Copyright (c) 2003, 2006, 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*/2728/**29* Simple EUC-like decoder used by IBM01383 and IBM97030* supports G1 - no support for G2 or G331*/323334import java.nio.ByteBuffer;35import java.nio.CharBuffer;36import java.nio.charset.Charset;37import java.nio.charset.CharsetDecoder;38import java.nio.charset.CoderResult;3940abstract class SimpleEUCDecoder41extends CharsetDecoder42{43private final int SS2 = 0x8E;44private final int SS3 = 0x8F;4546protected static String mappingTableG1;47protected static String byteToCharTable;4849protected SimpleEUCDecoder(Charset cs) {50super(cs, 0.5f, 1.0f);51}5253private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {54byte[] sa = src.array();55int sp = src.arrayOffset() + src.position();56int sl = src.arrayOffset() + src.limit();57assert (sp <= sl);58sp = (sp <= sl ? sp : sl);59char[] da = dst.array();60int dp = dst.arrayOffset() + dst.position();61int dl = dst.arrayOffset() + dst.limit();62assert (dp <= dl);63dp = (dp <= dl ? dp : dl);6465try {66while (sp < sl) {67int byte1, byte2;68int inputSize = 1;69char outputChar = '\uFFFD';7071byte1 = sa[sp] & 0xff;7273if ( byte1 <= 0x9f ) { // < 0x9f has its own table (G0)74if (byte1 == SS2 || byte1 == SS3 ) {75// No support provided for G2/G3 at this time.76return CoderResult.malformedForLength(1);77}78outputChar = byteToCharTable.charAt(byte1);79} else if (byte1 < 0xa1 || byte1 > 0xfe) { // invalid range?80return CoderResult.malformedForLength(1);81} else { // (G1)82if (sl - sp < 2) {83return CoderResult.UNDERFLOW;84}85byte2 = sa[sp + 1] & 0xff;86inputSize++;87if ( byte2 < 0xa1 || byte2 > 0xfe) {88return CoderResult.malformedForLength(2);89}90outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);91}92if (outputChar == '\uFFFD') {93return CoderResult.unmappableForLength(inputSize);94}95if (dl - dp < 1)96return CoderResult.OVERFLOW;97da[dp++] = outputChar;98sp += inputSize;99}100return CoderResult.UNDERFLOW;101} finally {102src.position(sp - src.arrayOffset());103dst.position(dp - dst.arrayOffset());104}105}106107private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {108int mark = src.position();109110try {111while (src.hasRemaining()) {112char outputChar = '\uFFFD';113int inputSize = 1;114int byte1, byte2;115116byte1 = src.get() & 0xff;117if ( byte1 <= 0x9f ) {118if (byte1 == SS2 || byte1 == SS3 ) {119return CoderResult.malformedForLength(1);120}121outputChar = byteToCharTable.charAt(byte1);122} else if (byte1 < 0xa1 || byte1 > 0xfe) {123return CoderResult.malformedForLength(1);124} else {125if (!src.hasRemaining()) {126return CoderResult.UNDERFLOW;127}128byte2 = src.get() & 0xff;129inputSize++;130if ( byte2 < 0xa1 || byte2 > 0xfe) {131return CoderResult.malformedForLength(2);132}133outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);134}135if (outputChar == '\uFFFD') {136return CoderResult.unmappableForLength(inputSize);137}138if (!dst.hasRemaining())139return CoderResult.OVERFLOW;140dst.put(outputChar);141mark += inputSize;142}143return CoderResult.UNDERFLOW;144} finally {145src.position(mark);146}147}148149protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {150if (src.hasArray() && dst.hasArray())151return decodeArrayLoop(src, dst);152else153return decodeBufferLoop(src, dst);154}155}156157158