Path: blob/master/test/jdk/sun/nio/cs/OLD/EUC_JP_OLD.java
41154 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*/2728import java.nio.ByteBuffer;29import java.nio.CharBuffer;30import java.nio.charset.Charset;31import java.nio.charset.CharsetDecoder;32import java.nio.charset.CharsetEncoder;33import java.nio.charset.CoderResult;34import sun.nio.cs.HistoricallyNamedCharset;35import sun.nio.cs.Surrogate;3637public class EUC_JP_OLD38extends Charset39implements HistoricallyNamedCharset40{41public EUC_JP_OLD() {42super("EUC-JP_OLD", null);43}4445public String historicalName() {46return "EUC_JP";47}4849public boolean contains(Charset cs) {50return ((cs.name().equals("US-ASCII"))51|| (cs instanceof JIS_X_0201_OLD)52|| (cs instanceof JIS_X_0208_OLD)53|| (cs instanceof JIS_X_0212_OLD)54|| (cs instanceof EUC_JP_OLD));55}5657public CharsetDecoder newDecoder() {58return new Decoder(this);59}6061public CharsetEncoder newEncoder() {6263// Need to force the replacement byte to 0x3f64// because JIS_X_0208_Encoder defines its own65// alternative 2 byte substitution to permit it66// to exist as a self-standing Encoder6768byte[] replacementBytes = { (byte)0x3f };69return new Encoder(this).replaceWith(replacementBytes);70}717273static class Decoder extends JIS_X_0208_Decoder {7475JIS_X_0201_OLD.Decoder decoderJ0201;76JIS_X_0212_Decoder decoderJ0212;7778private static final short[] j0208Index1 =79JIS_X_0208_Decoder.getIndex1();80private static final String[] j0208Index2 =81JIS_X_0208_Decoder.getIndex2();8283protected Decoder(Charset cs) {84super(cs);85decoderJ0201 = new JIS_X_0201_OLD.Decoder(cs);86decoderJ0212 = new JIS_X_0212_Decoder(cs);87start = 0xa1;88end = 0xfe;89}90protected char decode0212(int byte1, int byte2) {91return decoderJ0212.decodeDouble(byte1, byte2);92}9394protected char decodeDouble(int byte1, int byte2) {95if (byte1 == 0x8e) {96return decoderJ0201.decode(byte2 - 256);97}98// Fix for bug 4121358 - similar fix for bug 4117820 put99// into ByteToCharDoubleByte.getUnicode()100if (((byte1 < 0) || (byte1 > getIndex1().length))101|| ((byte2 < start) || (byte2 > end)))102return REPLACE_CHAR;103104int n = (j0208Index1[byte1 - 0x80] & 0xf) * (end - start + 1)105+ (byte2 - start);106return j0208Index2[j0208Index1[byte1 - 0x80] >> 4].charAt(n);107}108109private CoderResult decodeArrayLoop(ByteBuffer src,110CharBuffer dst)111{112byte[] sa = src.array();113int sp = src.arrayOffset() + src.position();114int sl = src.arrayOffset() + src.limit();115assert (sp <= sl);116sp = (sp <= sl ? sp : sl);117118char[] da = dst.array();119int dp = dst.arrayOffset() + dst.position();120int dl = dst.arrayOffset() + dst.limit();121assert (dp <= dl);122dp = (dp <= dl ? dp : dl);123124int b1 = 0, b2 = 0;125int inputSize = 0;126char outputChar = REPLACE_CHAR; // U+FFFD;127128try {129while (sp < sl) {130b1 = sa[sp] & 0xff;131inputSize = 1;132133if ((b1 & 0x80) == 0) {134outputChar = (char)b1;135}136else { // Multibyte char137if ((b1 & 0xff) == 0x8f) { // JIS0212138if (sp + 3 > sl)139return CoderResult.UNDERFLOW;140b1 = sa[sp + 1] & 0xff;141b2 = sa[sp + 2] & 0xff;142inputSize += 2;143outputChar = decode0212(b1-0x80, b2-0x80);144} else {145// JIS0208146if (sp + 2 > sl)147return CoderResult.UNDERFLOW;148b2 = sa[sp + 1] & 0xff;149inputSize++;150outputChar = decodeDouble(b1, b2);151}152}153if (outputChar == REPLACE_CHAR) { // can't be decoded154return CoderResult.unmappableForLength(inputSize);155}156if (dp + 1 > dl)157return CoderResult.OVERFLOW;158da[dp++] = outputChar;159sp += inputSize;160}161return CoderResult.UNDERFLOW;162} finally {163src.position(sp - src.arrayOffset());164dst.position(dp - dst.arrayOffset());165}166}167168private CoderResult decodeBufferLoop(ByteBuffer src,169CharBuffer dst)170{171int mark = src.position();172int b1 = 0, b2 = 0;173int inputSize = 0;174175char outputChar = REPLACE_CHAR; // U+FFFD;176177try {178while (src.hasRemaining()) {179b1 = src.get() & 0xff;180inputSize = 1;181182if ((b1 & 0x80) == 0) {183outputChar = (char)b1;184} else { // Multibyte char185if ((b1 & 0xff) == 0x8f) { // JIS0212186if (src.remaining() < 2)187return CoderResult.UNDERFLOW;188b1 = src.get() & 0xff;189b2 = src.get() & 0xff;190inputSize += 2;191outputChar = decode0212(b1-0x80, b2-0x80);192} else {193// JIS0208194if (src.remaining() < 1)195return CoderResult.UNDERFLOW;196b2 = src.get() & 0xff;197inputSize++;198outputChar = decodeDouble(b1, b2);199}200}201202if (outputChar == REPLACE_CHAR) {203return CoderResult.unmappableForLength(inputSize);204}205if (dst.remaining() < 1)206return CoderResult.OVERFLOW;207dst.put(outputChar);208mark += inputSize;209}210return CoderResult.UNDERFLOW;211} finally {212src.position(mark);213}214}215216// Make some protected methods public for use by JISAutoDetect217public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {218if (src.hasArray() && dst.hasArray())219return decodeArrayLoop(src, dst);220else221return decodeBufferLoop(src, dst);222}223public void implReset() {224super.implReset();225}226public CoderResult implFlush(CharBuffer out) {227return super.implFlush(out);228}229}230231232static class Encoder extends JIS_X_0208_Encoder {233234JIS_X_0201_OLD.Encoder encoderJ0201;235JIS_X_0212_Encoder encoderJ0212;236237private static final short[] j0208Index1 =238JIS_X_0208_Encoder.getIndex1();239private static final String[] j0208Index2 =240JIS_X_0208_Encoder.getIndex2();241242private final Surrogate.Parser sgp = new Surrogate.Parser();243244protected Encoder(Charset cs) {245super(cs, 3.0f, 3.0f);246encoderJ0201 = new JIS_X_0201_OLD.Encoder(cs);247encoderJ0212 = new JIS_X_0212_Encoder(cs);248}249250public boolean canEncode(char c) {251byte[] encodedBytes = new byte[3];252253if (encodeSingle(c, encodedBytes) == 0) { //doublebyte254if (encodeDouble(c) == 0)255return false;256}257return true;258}259260protected int encodeSingle(char inputChar, byte[] outputByte) {261byte b;262263if (inputChar == 0) {264outputByte[0] = (byte)0;265return 1;266}267268if ((b = encoderJ0201.encode(inputChar)) == 0)269return 0;270271if (b > 0 && b < 128) {272outputByte[0] = b;273return 1;274}275276outputByte[0] = (byte)0x8e;277outputByte[1] = b;278return 2;279}280281protected int encodeDouble(char ch) {282int offset = j0208Index1[((ch & 0xff00) >> 8 )] << 8;283int r = j0208Index2[offset >> 12].charAt((offset & 0xfff) +284(ch & 0xff));285if (r != 0)286return r + 0x8080;287r = encoderJ0212.encodeDouble(ch);288if (r == 0)289return r;290return r + 0x8F8080;291}292293private CoderResult encodeArrayLoop(CharBuffer src,294ByteBuffer dst)295{296char[] sa = src.array();297int sp = src.arrayOffset() + src.position();298int sl = src.arrayOffset() + src.limit();299assert (sp <= sl);300sp = (sp <= sl ? sp : sl);301byte[] da = dst.array();302int dp = dst.arrayOffset() + dst.position();303int dl = dst.arrayOffset() + dst.limit();304assert (dp <= dl);305dp = (dp <= dl ? dp : dl);306307int outputSize = 0;308byte[] outputByte;309int inputSize = 0; // Size of input310byte[] tmpBuf = new byte[3];311312try {313while (sp < sl) {314outputByte = tmpBuf;315char c = sa[sp];316317if (Character.isSurrogate(c)) {318if (sgp.parse(c, sa, sp, sl) < 0)319return sgp.error();320return sgp.unmappableResult();321}322323outputSize = encodeSingle(c, outputByte);324325if (outputSize == 0) { // DoubleByte326int ncode = encodeDouble(c);327if (ncode != 0 ) {328if ((ncode & 0xFF0000) == 0) {329outputByte[0] = (byte) ((ncode & 0xff00) >> 8);330outputByte[1] = (byte) (ncode & 0xff);331outputSize = 2;332} else {333outputByte[0] = (byte) 0x8f;334outputByte[1] = (byte) ((ncode & 0xff00) >> 8);335outputByte[2] = (byte) (ncode & 0xff);336outputSize = 3;337}338} else {339return CoderResult.unmappableForLength(1);340}341}342if (dl - dp < outputSize)343return CoderResult.OVERFLOW;344// Put the byte in the output buffer345for (int i = 0; i < outputSize; i++) {346da[dp++] = outputByte[i];347}348sp++;349}350return CoderResult.UNDERFLOW;351} finally {352src.position(sp - src.arrayOffset());353dst.position(dp - dst.arrayOffset());354}355}356357private CoderResult encodeBufferLoop(CharBuffer src,358ByteBuffer dst)359{360int outputSize = 0;361byte[] outputByte;362int inputSize = 0; // Size of input363byte[] tmpBuf = new byte[3];364365int mark = src.position();366367try {368while (src.hasRemaining()) {369outputByte = tmpBuf;370char c = src.get();371if (Character.isSurrogate(c)) {372if (sgp.parse(c, src) < 0)373return sgp.error();374return sgp.unmappableResult();375}376377outputSize = encodeSingle(c, outputByte);378if (outputSize == 0) { // DoubleByte379int ncode = encodeDouble(c);380if (ncode != 0 ) {381if ((ncode & 0xFF0000) == 0) {382outputByte[0] = (byte) ((ncode & 0xff00) >> 8);383outputByte[1] = (byte) (ncode & 0xff);384outputSize = 2;385} else {386outputByte[0] = (byte) 0x8f;387outputByte[1] = (byte) ((ncode & 0xff00) >> 8);388outputByte[2] = (byte) (ncode & 0xff);389outputSize = 3;390}391} else {392return CoderResult.unmappableForLength(1);393}394}395396if (dst.remaining() < outputSize)397return CoderResult.OVERFLOW;398// Put the byte in the output buffer399for (int i = 0; i < outputSize; i++) {400dst.put(outputByte[i]);401}402mark++;403}404return CoderResult.UNDERFLOW;405} finally {406src.position(mark);407}408}409410protected CoderResult encodeLoop(CharBuffer src,411ByteBuffer dst)412{413if (src.hasArray() && dst.hasArray())414return encodeArrayLoop(src, dst);415else416return encodeBufferLoop(src, dst);417}418}419}420421422