0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/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
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/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.
0N/A */
0N/A
0N/A/*
0N/A */
0N/A
0N/Apackage sun.nio.cs.ext;
0N/A
0N/Aimport java.nio.ByteBuffer;
0N/Aimport java.nio.CharBuffer;
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.CharsetDecoder;
0N/Aimport java.nio.charset.CharsetEncoder;
0N/Aimport java.nio.charset.CoderResult;
0N/Aimport sun.nio.cs.Surrogate;
0N/A
0N/Aabstract class ISO2022
0N/A extends Charset
0N/A{
0N/A
0N/A private static final byte ISO_ESC = 0x1b;
0N/A private static final byte ISO_SI = 0x0f;
0N/A private static final byte ISO_SO = 0x0e;
0N/A private static final byte ISO_SS2_7 = 0x4e;
0N/A private static final byte ISO_SS3_7 = 0x4f;
0N/A private static final byte MSB = (byte)0x80;
0N/A private static final char REPLACE_CHAR = '\uFFFD';
0N/A private static final byte minDesignatorLength = 3;
0N/A
0N/A public ISO2022(String csname, String[] aliases) {
0N/A super(csname, aliases);
0N/A }
0N/A
0N/A public CharsetDecoder newDecoder() {
0N/A return new Decoder(this);
0N/A }
0N/A
0N/A public CharsetEncoder newEncoder() {
0N/A return new Encoder(this);
0N/A }
0N/A
0N/A protected static class Decoder extends CharsetDecoder {
0N/A
0N/A // Value to be filled by subclass
0N/A protected byte SODesig[][];
0N/A protected byte SS2Desig[][] = null;
0N/A protected byte SS3Desig[][] = null;
0N/A
0N/A protected CharsetDecoder SODecoder[];
0N/A protected CharsetDecoder SS2Decoder[] = null;
0N/A protected CharsetDecoder SS3Decoder[] = null;
0N/A
0N/A private static final byte SOFlag = 0;
0N/A private static final byte SS2Flag = 1;
0N/A private static final byte SS3Flag = 2;
0N/A
0N/A private int curSODes, curSS2Des, curSS3Des;
0N/A private boolean shiftout;
0N/A private CharsetDecoder tmpDecoder[];
0N/A
0N/A protected Decoder(Charset cs) {
0N/A super(cs, 1.0f, 1.0f);
0N/A }
0N/A
0N/A protected void implReset() {
0N/A curSODes = 0;
0N/A curSS2Des = 0;
0N/A curSS3Des = 0;
0N/A shiftout = false;
0N/A }
0N/A
0N/A private char decode(byte byte1, byte byte2, byte shiftFlag)
0N/A {
0N/A byte1 |= MSB;
0N/A byte2 |= MSB;
0N/A
0N/A byte[] tmpByte = { byte1,byte2 };
0N/A char[] tmpChar = new char[1];
0N/A int i = 0,
0N/A tmpIndex = 0;
0N/A
0N/A switch(shiftFlag) {
0N/A case SOFlag:
0N/A tmpIndex = curSODes;
2472N/A tmpDecoder = SODecoder;
0N/A break;
0N/A case SS2Flag:
0N/A tmpIndex = curSS2Des;
2472N/A tmpDecoder = SS2Decoder;
0N/A break;
0N/A case SS3Flag:
0N/A tmpIndex = curSS3Des;
2472N/A tmpDecoder = SS3Decoder;
0N/A break;
0N/A }
0N/A
0N/A if (tmpDecoder != null) {
0N/A for(i = 0; i < tmpDecoder.length; i++) {
0N/A if(tmpIndex == i) {
0N/A try {
0N/A ByteBuffer bb = ByteBuffer.wrap(tmpByte,0,2);
0N/A CharBuffer cc = CharBuffer.wrap(tmpChar,0,1);
0N/A tmpDecoder[i].decode(bb, cc, true);
0N/A cc.flip();
0N/A return cc.get();
0N/A } catch (Exception e) {}
0N/A }
0N/A }
0N/A }
0N/A return REPLACE_CHAR;
0N/A }
0N/A
0N/A private int findDesig(byte[] in, int sp, int sl, byte[][] desigs) {
0N/A if (desigs == null) return -1;
0N/A int i = 0;
0N/A while (i < desigs.length) {
0N/A if (desigs[i] != null && sl - sp >= desigs[i].length) {
0N/A int j = 0;
0N/A while (j < desigs[i].length && in[sp+j] == desigs[i][j]) { j++; }
0N/A if (j == desigs[i].length)
0N/A return i;
0N/A }
0N/A i++;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A private int findDesigBuf(ByteBuffer in, byte[][] desigs) {
0N/A if (desigs == null) return -1;
0N/A int i = 0;
0N/A while (i < desigs.length) {
0N/A if (desigs[i] != null && in.remaining() >= desigs[i].length) {
0N/A int j = 0;
0N/A in.mark();
0N/A while (j < desigs[i].length && in.get() == desigs[i][j]) { j++; }
0N/A if (j == desigs[i].length)
0N/A return i;
0N/A in.reset();
0N/A }
0N/A i++;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A private CoderResult decodeArrayLoop(ByteBuffer src,
0N/A CharBuffer dst)
0N/A {
0N/A byte[] sa = src.array();
0N/A int sp = src.arrayOffset() + src.position();
0N/A int sl = src.arrayOffset() + src.limit();
0N/A assert (sp <= sl);
0N/A sp = (sp <= sl ? sp : sl);
0N/A
0N/A char[] da = dst.array();
0N/A int dp = dst.arrayOffset() + dst.position();
0N/A int dl = dst.arrayOffset() + dst.limit();
0N/A assert (dp <= dl);
0N/A dp = (dp <= dl ? dp : dl);
0N/A
0N/A int b1 = 0, b2 = 0, b3 = 0;
0N/A
0N/A try {
0N/A while (sp < sl) {
0N/A b1 = sa[sp] & 0xff;
0N/A int inputSize = 1;
0N/A switch (b1) {
0N/A case ISO_SO:
0N/A shiftout = true;
0N/A inputSize = 1;
0N/A break;
0N/A case ISO_SI:
0N/A shiftout = false;
0N/A inputSize = 1;
0N/A break;
0N/A case ISO_ESC:
0N/A if (sl - sp - 1 < minDesignatorLength)
0N/A return CoderResult.UNDERFLOW;
0N/A
0N/A int desig = findDesig(sa, sp + 1, sl, SODesig);
0N/A if (desig != -1) {
0N/A curSODes = desig;
0N/A inputSize = SODesig[desig].length + 1;
0N/A break;
0N/A }
0N/A desig = findDesig(sa, sp + 1, sl, SS2Desig);
0N/A if (desig != -1) {
0N/A curSS2Des = desig;
0N/A inputSize = SS2Desig[desig].length + 1;
0N/A break;
0N/A }
0N/A desig = findDesig(sa, sp + 1, sl, SS3Desig);
0N/A if (desig != -1) {
0N/A curSS3Des = desig;
0N/A inputSize = SS3Desig[desig].length + 1;
0N/A break;
0N/A }
0N/A if (sl - sp < 2)
0N/A return CoderResult.UNDERFLOW;
0N/A b1 = sa[sp + 1];
0N/A switch(b1) {
0N/A case ISO_SS2_7:
0N/A if (sl - sp < 4)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = sa[sp +2];
0N/A b3 = sa[sp +3];
0N/A if (dl - dp <1)
0N/A return CoderResult.OVERFLOW;
0N/A da[dp] = decode((byte)b2,
0N/A (byte)b3,
0N/A SS2Flag);
0N/A dp++;
0N/A inputSize = 4;
0N/A break;
0N/A case ISO_SS3_7:
0N/A if (sl - sp < 4)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = sa[sp + 2];
0N/A b3 = sa[sp + 3];
0N/A if (dl - dp <1)
0N/A return CoderResult.OVERFLOW;
0N/A da[dp] = decode((byte)b2,
0N/A (byte)b3,
0N/A SS3Flag);
0N/A dp++;
0N/A inputSize = 4;
0N/A break;
0N/A default:
0N/A return CoderResult.malformedForLength(2);
0N/A }
0N/A break;
0N/A default:
0N/A if (dl - dp < 1)
0N/A return CoderResult.OVERFLOW;
0N/A if (!shiftout) {
0N/A da[dp++]=(char)(sa[sp] & 0xff);
0N/A } else {
0N/A if (dl - dp < 1)
0N/A return CoderResult.OVERFLOW;
0N/A if (sl - sp < 2)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = sa[sp+1] & 0xff;
0N/A da[dp++] = decode((byte)b1,
0N/A (byte)b2,
0N/A SOFlag);
0N/A inputSize = 2;
0N/A }
0N/A break;
0N/A }
0N/A sp += inputSize;
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(sp - src.arrayOffset());
0N/A dst.position(dp - dst.arrayOffset());
0N/A }
0N/A }
0N/A
0N/A private CoderResult decodeBufferLoop(ByteBuffer src,
0N/A CharBuffer dst)
0N/A {
0N/A int mark = src.position();
0N/A int b1 = 0, b2 = 0, b3 = 0;
0N/A
0N/A try {
0N/A while (src.hasRemaining()) {
0N/A b1 = src.get();
0N/A int inputSize = 1;
0N/A switch (b1) {
0N/A case ISO_SO:
0N/A shiftout = true;
0N/A break;
0N/A case ISO_SI:
0N/A shiftout = false;
0N/A break;
0N/A case ISO_ESC:
0N/A if (src.remaining() < minDesignatorLength)
0N/A return CoderResult.UNDERFLOW;
0N/A
0N/A int desig = findDesigBuf(src, SODesig);
0N/A if (desig != -1) {
0N/A curSODes = desig;
0N/A inputSize = SODesig[desig].length + 1;
0N/A break;
0N/A }
0N/A desig = findDesigBuf(src, SS2Desig);
0N/A if (desig != -1) {
0N/A curSS2Des = desig;
0N/A inputSize = SS2Desig[desig].length + 1;
0N/A break;
0N/A }
0N/A desig = findDesigBuf(src, SS3Desig);
0N/A if (desig != -1) {
0N/A curSS3Des = desig;
0N/A inputSize = SS3Desig[desig].length + 1;
0N/A break;
0N/A }
0N/A
0N/A if (src.remaining() < 1)
0N/A return CoderResult.UNDERFLOW;
0N/A b1 = src.get();
0N/A switch(b1) {
0N/A case ISO_SS2_7:
0N/A if (src.remaining() < 2)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = src.get();
0N/A b3 = src.get();
0N/A if (dst.remaining() < 1)
0N/A return CoderResult.OVERFLOW;
0N/A dst.put(decode((byte)b2,
0N/A (byte)b3,
0N/A SS2Flag));
0N/A inputSize = 4;
0N/A break;
0N/A case ISO_SS3_7:
0N/A if (src.remaining() < 2)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = src.get();
0N/A b3 = src.get();
0N/A if (dst.remaining() < 1)
0N/A return CoderResult.OVERFLOW;
0N/A dst.put(decode((byte)b2,
0N/A (byte)b3,
0N/A SS3Flag));
0N/A inputSize = 4;
0N/A break;
0N/A default:
0N/A return CoderResult.malformedForLength(2);
0N/A }
0N/A break;
0N/A default:
0N/A if (dst.remaining() < 1)
0N/A return CoderResult.OVERFLOW;
0N/A if (!shiftout) {
0N/A dst.put((char)(b1 & 0xff));
0N/A } else {
0N/A if (dst.remaining() < 1)
0N/A return CoderResult.OVERFLOW;
0N/A if (src.remaining() < 1)
0N/A return CoderResult.UNDERFLOW;
0N/A b2 = src.get() & 0xff;
0N/A dst.put(decode((byte)b1,
0N/A (byte)b2,
0N/A SOFlag));
0N/A inputSize = 2;
0N/A }
0N/A break;
0N/A }
0N/A mark += inputSize;
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } catch (Exception e) { e.printStackTrace(); return CoderResult.OVERFLOW; }
0N/A finally {
0N/A src.position(mark);
0N/A }
0N/A }
0N/A
0N/A protected CoderResult decodeLoop(ByteBuffer src,
0N/A CharBuffer dst)
0N/A {
0N/A if (src.hasArray() && dst.hasArray())
0N/A return decodeArrayLoop(src, dst);
0N/A else
0N/A return decodeBufferLoop(src, dst);
0N/A }
0N/A }
0N/A
0N/A protected static class Encoder extends CharsetEncoder {
0N/A private final Surrogate.Parser sgp = new Surrogate.Parser();
1571N/A public static final byte SS2 = (byte)0x8e;
1571N/A public static final byte PLANE2 = (byte)0xA2;
1571N/A public static final byte PLANE3 = (byte)0xA3;
0N/A private final byte MSB = (byte)0x80;
0N/A
0N/A protected final byte maximumDesignatorLength = 4;
0N/A
0N/A protected String SODesig,
0N/A SS2Desig = null,
0N/A SS3Desig = null;
0N/A
0N/A protected CharsetEncoder ISOEncoder;
0N/A
0N/A private boolean shiftout = false;
0N/A private boolean SODesDefined = false;
0N/A private boolean SS2DesDefined = false;
0N/A private boolean SS3DesDefined = false;
0N/A
0N/A private boolean newshiftout = false;
0N/A private boolean newSODesDefined = false;
0N/A private boolean newSS2DesDefined = false;
0N/A private boolean newSS3DesDefined = false;
0N/A
0N/A protected Encoder(Charset cs) {
0N/A super(cs, 4.0f, 8.0f);
0N/A }
0N/A
0N/A public boolean canEncode(char c) {
0N/A return (ISOEncoder.canEncode(c));
0N/A }
0N/A
0N/A protected void implReset() {
0N/A shiftout = false;
0N/A SODesDefined = false;
0N/A SS2DesDefined = false;
0N/A SS3DesDefined = false;
0N/A }
0N/A
0N/A private int unicodeToNative(char unicode, byte ebyte[])
0N/A {
0N/A int index = 0;
0N/A byte tmpByte[];
0N/A char convChar[] = {unicode};
0N/A byte convByte[] = new byte[4];
0N/A int converted;
0N/A
0N/A try{
0N/A CharBuffer cc = CharBuffer.wrap(convChar);
0N/A ByteBuffer bb = ByteBuffer.allocate(4);
0N/A ISOEncoder.encode(cc, bb, true);
0N/A bb.flip();
0N/A converted = bb.remaining();
0N/A bb.get(convByte,0,converted);
0N/A } catch(Exception e) {
0N/A return -1;
0N/A }
0N/A
0N/A if (converted == 2) {
0N/A if (!SODesDefined) {
0N/A newSODesDefined = true;
0N/A ebyte[0] = ISO_ESC;
0N/A tmpByte = SODesig.getBytes();
0N/A System.arraycopy(tmpByte,0,ebyte,1,tmpByte.length);
0N/A index = tmpByte.length+1;
0N/A }
0N/A if (!shiftout) {
0N/A newshiftout = true;
0N/A ebyte[index++] = ISO_SO;
0N/A }
0N/A ebyte[index++] = (byte)(convByte[0] & 0x7f);
0N/A ebyte[index++] = (byte)(convByte[1] & 0x7f);
0N/A } else {
1237N/A if(convByte[0] == SS2) {
1237N/A if (convByte[1] == PLANE2) {
1237N/A if (!SS2DesDefined) {
1237N/A newSS2DesDefined = true;
1237N/A ebyte[0] = ISO_ESC;
1237N/A tmpByte = SS2Desig.getBytes();
1237N/A System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
1237N/A index = tmpByte.length+1;
1237N/A }
1237N/A ebyte[index++] = ISO_ESC;
1237N/A ebyte[index++] = ISO_SS2_7;
1237N/A ebyte[index++] = (byte)(convByte[2] & 0x7f);
1237N/A ebyte[index++] = (byte)(convByte[3] & 0x7f);
1237N/A } else if (convByte[1] == PLANE3) {
1237N/A if(!SS3DesDefined){
1237N/A newSS3DesDefined = true;
1237N/A ebyte[0] = ISO_ESC;
1237N/A tmpByte = SS3Desig.getBytes();
1237N/A System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
1237N/A index = tmpByte.length+1;
1237N/A }
1237N/A ebyte[index++] = ISO_ESC;
1237N/A ebyte[index++] = ISO_SS3_7;
1237N/A ebyte[index++] = (byte)(convByte[2] & 0x7f);
1237N/A ebyte[index++] = (byte)(convByte[3] & 0x7f);
0N/A }
0N/A }
0N/A }
0N/A return index;
0N/A }
0N/A
0N/A private CoderResult encodeArrayLoop(CharBuffer src,
0N/A ByteBuffer dst)
0N/A {
0N/A char[] sa = src.array();
0N/A int sp = src.arrayOffset() + src.position();
0N/A int sl = src.arrayOffset() + src.limit();
0N/A assert (sp <= sl);
0N/A sp = (sp <= sl ? sp : sl);
0N/A byte[] da = dst.array();
0N/A int dp = dst.arrayOffset() + dst.position();
0N/A int dl = dst.arrayOffset() + dst.limit();
0N/A assert (dp <= dl);
0N/A dp = (dp <= dl ? dp : dl);
0N/A
0N/A int outputSize = 0;
0N/A byte[] outputByte = new byte[8];
0N/A newshiftout = shiftout;
0N/A newSODesDefined = SODesDefined;
0N/A newSS2DesDefined = SS2DesDefined;
0N/A newSS3DesDefined = SS3DesDefined;
0N/A
0N/A try {
0N/A while (sp < sl) {
0N/A char c = sa[sp];
1602N/A if (Character.isSurrogate(c)) {
0N/A if (sgp.parse(c, sa, sp, sl) < 0)
0N/A return sgp.error();
0N/A return sgp.unmappableResult();
0N/A }
0N/A
0N/A if (c < 0x80) { // ASCII
0N/A if (shiftout){
0N/A newshiftout = false;
0N/A outputSize = 2;
0N/A outputByte[0] = ISO_SI;
0N/A outputByte[1] = (byte)(c & 0x7f);
0N/A } else {
0N/A outputSize = 1;
0N/A outputByte[0] = (byte)(c & 0x7f);
0N/A }
0N/A if(sa[sp] == '\n'){
0N/A newSODesDefined = false;
0N/A newSS2DesDefined = false;
0N/A newSS3DesDefined = false;
0N/A }
0N/A } else {
0N/A outputSize = unicodeToNative(c, outputByte);
0N/A if (outputSize == 0) {
0N/A return CoderResult.unmappableForLength(1);
0N/A }
0N/A }
0N/A if (dl - dp < outputSize)
0N/A return CoderResult.OVERFLOW;
0N/A
0N/A for (int i = 0; i < outputSize; i++)
0N/A da[dp++] = outputByte[i];
0N/A sp++;
0N/A shiftout = newshiftout;
0N/A SODesDefined = newSODesDefined;
0N/A SS2DesDefined = newSS2DesDefined;
0N/A SS3DesDefined = newSS3DesDefined;
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(sp - src.arrayOffset());
0N/A dst.position(dp - dst.arrayOffset());
0N/A }
0N/A }
0N/A
0N/A
0N/A private CoderResult encodeBufferLoop(CharBuffer src,
0N/A ByteBuffer dst)
0N/A {
0N/A int outputSize = 0;
0N/A byte[] outputByte = new byte[8];
0N/A int inputSize = 0; // Size of input
0N/A newshiftout = shiftout;
0N/A newSODesDefined = SODesDefined;
0N/A newSS2DesDefined = SS2DesDefined;
0N/A newSS3DesDefined = SS3DesDefined;
0N/A int mark = src.position();
0N/A
0N/A try {
0N/A while (src.hasRemaining()) {
0N/A char inputChar = src.get();
1602N/A if (Character.isSurrogate(inputChar)) {
0N/A if (sgp.parse(inputChar, src) < 0)
0N/A return sgp.error();
0N/A return sgp.unmappableResult();
0N/A }
0N/A if (inputChar < 0x80) { // ASCII
0N/A if (shiftout){
0N/A newshiftout = false;
0N/A outputSize = 2;
0N/A outputByte[0] = ISO_SI;
0N/A outputByte[1] = (byte)(inputChar & 0x7f);
0N/A } else {
0N/A outputSize = 1;
0N/A outputByte[0] = (byte)(inputChar & 0x7f);
0N/A }
0N/A if(inputChar == '\n'){
0N/A newSODesDefined = false;
0N/A newSS2DesDefined = false;
0N/A newSS3DesDefined = false;
0N/A }
0N/A } else {
0N/A outputSize = unicodeToNative(inputChar, outputByte);
0N/A if (outputSize == 0) {
0N/A return CoderResult.unmappableForLength(1);
0N/A }
0N/A }
0N/A
0N/A if (dst.remaining() < outputSize)
0N/A return CoderResult.OVERFLOW;
0N/A for (int i = 0; i < outputSize; i++)
0N/A dst.put(outputByte[i]);
0N/A mark++;
0N/A shiftout = newshiftout;
0N/A SODesDefined = newSODesDefined;
0N/A SS2DesDefined = newSS2DesDefined;
0N/A SS3DesDefined = newSS3DesDefined;
0N/A }
0N/A return CoderResult.UNDERFLOW;
0N/A } finally {
0N/A src.position(mark);
0N/A }
0N/A }
0N/A
0N/A protected CoderResult encodeLoop(CharBuffer src,
0N/A ByteBuffer dst)
0N/A {
0N/A if (src.hasArray() && dst.hasArray())
0N/A return encodeArrayLoop(src, dst);
0N/A else
0N/A return encodeBufferLoop(src, dst);
0N/A }
0N/A }
0N/A}