1245N/A/*
3261N/A * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
1245N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1245N/A *
1245N/A * This code is free software; you can redistribute it and/or modify it
1245N/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
1245N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1245N/A *
1245N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1245N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1245N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1245N/A * version 2 for more details (a copy is included in the LICENSE file that
1245N/A * accompanied this code).
1245N/A *
1245N/A * You should have received a copy of the GNU General Public License version
1245N/A * 2 along with this work; if not, write to the Free Software Foundation,
1245N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1245N/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.
1245N/A */
1245N/A
1245N/Apackage sun.nio.cs.ext;
1245N/A
1245N/Aimport java.nio.ByteBuffer;
1245N/Aimport java.nio.CharBuffer;
1245N/Aimport java.nio.charset.Charset;
1245N/Aimport java.nio.charset.CharsetDecoder;
1245N/Aimport java.nio.charset.CharsetEncoder;
1245N/Aimport java.nio.charset.CoderResult;
1245N/Aimport java.util.Arrays;
1245N/Aimport sun.nio.cs.Surrogate;
1245N/Aimport static sun.nio.cs.CharsetMapping.*;
1245N/A
1245N/A/*
1245N/A * Four types of "DoubleByte" charsets are implemented in this class
1245N/A * (1)DoubleByte
1245N/A * The "mostly widely used" multibyte charset, a combination of
1245N/A * a singlebyte character set (usually the ASCII charset) and a
1245N/A * doublebyte character set. The codepoint values of singlebyte
1245N/A * and doublebyte don't overlap. Microsoft's multibyte charsets
1245N/A * and IBM's "DBCS_ASCII" charsets, such as IBM1381, 942, 943,
1245N/A * 948, 949 and 950 are such charsets.
1245N/A *
1245N/A * (2)DoubleByte_EBCDIC
1245N/A * IBM EBCDIC Mix multibyte charset. Use SO and SI to shift (switch)
1245N/A * in and out between the singlebyte character set and doublebyte
1245N/A * character set.
1245N/A *
1245N/A * (3)DoubleByte_SIMPLE_EUC
1245N/A * It's a "simple" form of EUC encoding scheme, only have the
1245N/A * singlebyte character set G0 and one doublebyte character set
1245N/A * G1 are defined, G2 (with SS2) and G3 (with SS3) are not used.
1245N/A * So it is actually the same as the "typical" type (1) mentioned
1245N/A * above, except it return "malformed" for the SS2 and SS3 when
1245N/A * decoding.
1245N/A *
1245N/A * (4)DoubleByte ONLY
1245N/A * A "pure" doublebyte only character set. From implementation
1245N/A * point of view, this is the type (1) with "decodeSingle" always
1245N/A * returns unmappable.
1245N/A *
1245N/A * For simplicity, all implementations share the same decoding and
1245N/A * encoding data structure.
1245N/A *
1245N/A * Decoding:
1245N/A *
1245N/A * char[][] b2c;
1245N/A * char[] b2cSB;
1245N/A * int b2Min, b2Max
1245N/A *
1245N/A * public char decodeSingle(int b) {
1245N/A * return b2cSB.[b];
1245N/A * }
1245N/A *
1245N/A * public char decodeDouble(int b1, int b2) {
1245N/A * if (b2 < b2Min || b2 > b2Max)
1245N/A * return UNMAPPABLE_DECODING;
1245N/A * return b2c[b1][b2 - b2Min];
1245N/A * }
1245N/A *
1245N/A * (1)b2Min, b2Max are the corresponding min and max value of the
1245N/A * low-half of the double-byte.
1245N/A * (2)The high 8-bit/b1 of the double-byte are used to indexed into
1245N/A * b2c array.
1245N/A *
1245N/A * Encoding:
1245N/A *
1245N/A * char[] c2b;
1245N/A * char[] c2bIndex;
1245N/A *
1245N/A * public int encodeChar(char ch) {
1245N/A * return c2b[c2bIndex[ch >> 8] + (ch & 0xff)];
1245N/A * }
1245N/A *
1245N/A */
1245N/A
1245N/Apublic class DoubleByte {
1245N/A
1245N/A public final static char[] B2C_UNMAPPABLE;
1245N/A static {
1245N/A B2C_UNMAPPABLE = new char[0x100];
2472N/A Arrays.fill(B2C_UNMAPPABLE, UNMAPPABLE_DECODING);
1245N/A }
1245N/A
1306N/A public static class Decoder extends CharsetDecoder
1306N/A implements DelegatableDecoder
1306N/A {
1306N/A
1245N/A final char[][] b2c;
1245N/A final char[] b2cSB;
1245N/A final int b2Min;
1245N/A final int b2Max;
1245N/A
1245N/A // for SimpleEUC override
1245N/A protected CoderResult crMalformedOrUnderFlow(int b) {
1245N/A return CoderResult.UNDERFLOW;
1245N/A }
1245N/A
1245N/A protected CoderResult crMalformedOrUnmappable(int b) {
1245N/A return CoderResult.unmappableForLength(2);
1245N/A }
1245N/A
1245N/A Decoder(Charset cs, float avgcpb, float maxcpb,
1245N/A char[][] b2c, char[] b2cSB,
1245N/A int b2Min, int b2Max) {
1245N/A super(cs, avgcpb, maxcpb);
1245N/A this.b2c = b2c;
1245N/A this.b2cSB = b2cSB;
1245N/A this.b2Min = b2Min;
1245N/A this.b2Max = b2Max;
1245N/A }
1245N/A
1245N/A Decoder(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
1245N/A this(cs, 0.5f, 1.0f, b2c, b2cSB, b2Min, b2Max);
1245N/A }
1245N/A
1245N/A protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
1245N/A byte[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A
1245N/A char[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A
1245N/A try {
1245N/A while (sp < sl && dp < dl) {
1245N/A // inline the decodeSingle/Double() for better performance
1245N/A int inSize = 1;
1245N/A int b1 = sa[sp] & 0xff;
1245N/A char c = b2cSB[b1];
1245N/A if (c == UNMAPPABLE_DECODING) {
1245N/A if (sl - sp < 2)
1245N/A return crMalformedOrUnderFlow(b1);
1245N/A int b2 = sa[sp + 1] & 0xff;
1245N/A if (b2 < b2Min || b2 > b2Max ||
1245N/A (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
1245N/A return crMalformedOrUnmappable(b1);
1245N/A }
1245N/A inSize++;
1245N/A }
1245N/A da[dp++] = c;
1245N/A sp += inSize;
1245N/A }
1245N/A return (sp >= sl) ? CoderResult.UNDERFLOW
1245N/A : CoderResult.OVERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
1245N/A int mark = src.position();
1245N/A try {
1306N/A
1245N/A while (src.hasRemaining() && dst.hasRemaining()) {
1245N/A int b1 = src.get() & 0xff;
1245N/A char c = b2cSB[b1];
1245N/A int inSize = 1;
1245N/A if (c == UNMAPPABLE_DECODING) {
1245N/A if (src.remaining() < 1)
1245N/A return crMalformedOrUnderFlow(b1);
1245N/A int b2 = src.get() & 0xff;
1245N/A if (b2 < b2Min || b2 > b2Max ||
1245N/A (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING)
1245N/A return crMalformedOrUnmappable(b1);
1245N/A inSize++;
1245N/A }
1245N/A dst.put(c);
1245N/A mark += inSize;
1245N/A }
1245N/A return src.hasRemaining()? CoderResult.OVERFLOW
1245N/A : CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A
1306N/A // Make some protected methods public for use by JISAutoDetect
1306N/A public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
1245N/A if (src.hasArray() && dst.hasArray())
1245N/A return decodeArrayLoop(src, dst);
1245N/A else
1245N/A return decodeBufferLoop(src, dst);
1245N/A }
1245N/A
1306N/A public void implReset() {
1306N/A super.implReset();
1306N/A }
1306N/A
1306N/A public CoderResult implFlush(CharBuffer out) {
1306N/A return super.implFlush(out);
1306N/A }
1306N/A
1245N/A // decode loops are not using decodeSingle/Double() for performance
1245N/A // reason.
1245N/A public char decodeSingle(int b) {
1245N/A return b2cSB[b];
1245N/A }
1245N/A
1245N/A public char decodeDouble(int b1, int b2) {
1245N/A if (b2 < b2Min || b2 > b2Max)
1245N/A return UNMAPPABLE_DECODING;
1245N/A return b2c[b1][b2 - b2Min];
1245N/A }
1245N/A }
1245N/A
1245N/A // IBM_EBCDIC_DBCS
1245N/A public static class Decoder_EBCDIC extends Decoder {
1245N/A private static final int SBCS = 0;
1245N/A private static final int DBCS = 1;
1245N/A private static final int SO = 0x0e;
1245N/A private static final int SI = 0x0f;
1245N/A private int currentState;
1245N/A
1245N/A Decoder_EBCDIC(Charset cs,
1245N/A char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
1245N/A super(cs, b2c, b2cSB, b2Min, b2Max);
1245N/A }
1245N/A
1306N/A public void implReset() {
1245N/A currentState = SBCS;
1245N/A }
1245N/A
1245N/A // Check validity of dbcs ebcdic byte pair values
1245N/A //
1245N/A // First byte : 0x41 -- 0xFE
1245N/A // Second byte: 0x41 -- 0xFE
1245N/A // Doublebyte blank: 0x4040
1245N/A //
1245N/A // The validation implementation in "old" DBCS_IBM_EBCDIC and sun.io
1245N/A // as
1245N/A // if ((b1 != 0x40 || b2 != 0x40) &&
1245N/A // (b2 < 0x41 || b2 > 0xfe)) {...}
1245N/A // is not correct/complete (range check for b1)
1245N/A //
1245N/A private static boolean isDoubleByte(int b1, int b2) {
1245N/A return (0x41 <= b1 && b1 <= 0xfe && 0x41 <= b2 && b2 <= 0xfe)
1245N/A || (b1 == 0x40 && b2 == 0x40); // DBCS-HOST SPACE
1245N/A }
1245N/A
1245N/A protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
1245N/A byte[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A char[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A
1245N/A try {
1245N/A // don't check dp/dl together here, it's possible to
1245N/A // decdoe a SO/SI without space in output buffer.
1245N/A while (sp < sl) {
1245N/A int b1 = sa[sp] & 0xff;
1245N/A int inSize = 1;
1245N/A if (b1 == SO) { // Shift out
1245N/A if (currentState != SBCS)
1245N/A return CoderResult.malformedForLength(1);
1245N/A else
1245N/A currentState = DBCS;
1245N/A } else if (b1 == SI) {
1245N/A if (currentState != DBCS)
1245N/A return CoderResult.malformedForLength(1);
1245N/A else
1245N/A currentState = SBCS;
1245N/A } else {
1245N/A char c = UNMAPPABLE_DECODING;
1245N/A if (currentState == SBCS) {
1245N/A c = b2cSB[b1];
1245N/A if (c == UNMAPPABLE_DECODING)
1245N/A return CoderResult.unmappableForLength(1);
1245N/A } else {
1245N/A if (sl - sp < 2)
1245N/A return CoderResult.UNDERFLOW;
1245N/A int b2 = sa[sp + 1] & 0xff;
1245N/A if (b2 < b2Min || b2 > b2Max ||
1245N/A (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
1245N/A if (!isDoubleByte(b1, b2))
1245N/A return CoderResult.malformedForLength(2);
1245N/A return CoderResult.unmappableForLength(2);
1245N/A }
1245N/A inSize++;
1245N/A }
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A
1245N/A da[dp++] = c;
1245N/A }
1245N/A sp += inSize;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
1245N/A int mark = src.position();
1245N/A try {
1245N/A while (src.hasRemaining()) {
1245N/A int b1 = src.get() & 0xff;
1245N/A int inSize = 1;
1245N/A if (b1 == SO) { // Shift out
1245N/A if (currentState != SBCS)
1245N/A return CoderResult.malformedForLength(1);
1245N/A else
1245N/A currentState = DBCS;
1245N/A } else if (b1 == SI) {
1245N/A if (currentState != DBCS)
1245N/A return CoderResult.malformedForLength(1);
1245N/A else
1245N/A currentState = SBCS;
1245N/A } else {
1245N/A char c = UNMAPPABLE_DECODING;
1245N/A if (currentState == SBCS) {
1245N/A c = b2cSB[b1];
1245N/A if (c == UNMAPPABLE_DECODING)
1245N/A return CoderResult.unmappableForLength(1);
1245N/A } else {
1245N/A if (src.remaining() < 1)
1245N/A return CoderResult.UNDERFLOW;
1245N/A int b2 = src.get()&0xff;
1245N/A if (b2 < b2Min || b2 > b2Max ||
1245N/A (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
1245N/A if (!isDoubleByte(b1, b2))
1245N/A return CoderResult.malformedForLength(2);
1245N/A return CoderResult.unmappableForLength(2);
1245N/A }
1245N/A inSize++;
1245N/A }
1245N/A
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A
1245N/A dst.put(c);
1245N/A }
1245N/A mark += inSize;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A }
1245N/A
1245N/A // EBCDIC_DBCS_ONLY
1245N/A public static class Decoder_EBCDIC_DBCSONLY extends Decoder {
1245N/A static final char[] b2cSB;
1245N/A static {
1245N/A b2cSB = new char[0x100];
2472N/A Arrays.fill(b2cSB, UNMAPPABLE_DECODING);
1245N/A }
1245N/A Decoder_EBCDIC_DBCSONLY(Charset cs, char[][] b2c, int b2Min, int b2Max) {
1245N/A super(cs, 0.5f, 1.0f, b2c, b2cSB, b2Min, b2Max);
1245N/A }
1245N/A }
1245N/A
1245N/A // EUC_SIMPLE
1245N/A // The only thing we need to "override" is to check SS2/SS3 and
1245N/A // return "malformed" if found
1245N/A public static class Decoder_EUC_SIM extends Decoder {
1245N/A private final int SS2 = 0x8E;
1245N/A private final int SS3 = 0x8F;
1245N/A
1245N/A Decoder_EUC_SIM(Charset cs,
1245N/A char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
1245N/A super(cs, b2c, b2cSB, b2Min, b2Max);
1245N/A }
1245N/A
1245N/A // No support provided for G2/G3 for SimpleEUC
1245N/A protected CoderResult crMalformedOrUnderFlow(int b) {
1245N/A if (b == SS2 || b == SS3 )
1245N/A return CoderResult.malformedForLength(1);
1245N/A return CoderResult.UNDERFLOW;
1245N/A }
1245N/A
1245N/A protected CoderResult crMalformedOrUnmappable(int b) {
1245N/A if (b == SS2 || b == SS3 )
1245N/A return CoderResult.malformedForLength(1);
1245N/A return CoderResult.unmappableForLength(2);
1245N/A }
1245N/A }
1245N/A
1245N/A public static class Encoder extends CharsetEncoder {
1245N/A final int MAX_SINGLEBYTE = 0xff;
1245N/A private final char[] c2b;
1245N/A private final char[] c2bIndex;
1245N/A Surrogate.Parser sgp;
1245N/A
1306N/A protected Encoder(Charset cs, char[] c2b, char[] c2bIndex) {
1245N/A super(cs, 2.0f, 2.0f);
1245N/A this.c2b = c2b;
1245N/A this.c2bIndex = c2bIndex;
1245N/A }
1245N/A
1245N/A Encoder(Charset cs, float avg, float max, byte[] repl, char[] c2b, char[] c2bIndex) {
1245N/A super(cs, avg, max, repl);
1245N/A this.c2b = c2b;
1245N/A this.c2bIndex = c2bIndex;
1245N/A }
1245N/A
1245N/A public boolean canEncode(char c) {
1245N/A return encodeChar(c) != UNMAPPABLE_ENCODING;
1245N/A }
1245N/A
1245N/A Surrogate.Parser sgp() {
1245N/A if (sgp == null)
1245N/A sgp = new Surrogate.Parser();
1245N/A return sgp;
1245N/A }
1245N/A
1245N/A protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
1245N/A char[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A
1245N/A byte[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A
1245N/A try {
1245N/A while (sp < sl) {
1245N/A char c = sa[sp];
1245N/A int bb = encodeChar(c);
1245N/A if (bb == UNMAPPABLE_ENCODING) {
1602N/A if (Character.isSurrogate(c)) {
1245N/A if (sgp().parse(c, sa, sp, sl) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A
1245N/A if (bb > MAX_SINGLEBYTE) { // DoubleByte
1245N/A if (dl - dp < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte)(bb >> 8);
1245N/A da[dp++] = (byte)bb;
1245N/A } else { // SingleByte
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte)bb;
1245N/A }
1245N/A
1245N/A sp++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
1245N/A int mark = src.position();
1245N/A try {
1245N/A while (src.hasRemaining()) {
1245N/A char c = src.get();
1245N/A int bb = encodeChar(c);
1245N/A if (bb == UNMAPPABLE_ENCODING) {
1602N/A if (Character.isSurrogate(c)) {
1245N/A if (sgp().parse(c, src) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A if (bb > MAX_SINGLEBYTE) { // DoubleByte
1245N/A if (dst.remaining() < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte)(bb >> 8));
1245N/A dst.put((byte)(bb));
1245N/A } else {
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte)bb);
1245N/A }
1245N/A mark++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
1245N/A if (src.hasArray() && dst.hasArray())
1245N/A return encodeArrayLoop(src, dst);
1245N/A else
1245N/A return encodeBufferLoop(src, dst);
1245N/A }
1245N/A
1245N/A public int encodeChar(char ch) {
1245N/A return c2b[c2bIndex[ch >> 8] + (ch & 0xff)];
1245N/A }
1245N/A
1245N/A // init the c2b and c2bIndex tables from b2c.
1245N/A static void initC2B(String[] b2c, String b2cSB, String b2cNR, String c2bNR,
1245N/A int b2Min, int b2Max,
1245N/A char[] c2b, char[] c2bIndex)
1245N/A {
1245N/A Arrays.fill(c2b, (char)UNMAPPABLE_ENCODING);
1245N/A int off = 0x100;
1245N/A
1245N/A char[][] b2c_ca = new char[b2c.length][];
1245N/A char[] b2cSB_ca = null;
1245N/A if (b2cSB != null)
1245N/A b2cSB_ca = b2cSB.toCharArray();
1245N/A
1245N/A for (int i = 0; i < b2c.length; i++) {
1245N/A if (b2c[i] == null)
1245N/A continue;
1245N/A b2c_ca[i] = b2c[i].toCharArray();
1245N/A }
1245N/A
1245N/A if (b2cNR != null) {
1245N/A int j = 0;
1245N/A while (j < b2cNR.length()) {
1245N/A char b = b2cNR.charAt(j++);
1245N/A char c = b2cNR.charAt(j++);
1245N/A if (b < 0x100 && b2cSB_ca != null) {
1245N/A if (b2cSB_ca[b] == c)
1245N/A b2cSB_ca[b] = UNMAPPABLE_DECODING;
1245N/A } else {
1245N/A if (b2c_ca[b >> 8][(b & 0xff) - b2Min] == c)
1245N/A b2c_ca[b >> 8][(b & 0xff) - b2Min] = UNMAPPABLE_DECODING;
1245N/A }
1245N/A }
1245N/A }
1245N/A
1245N/A if (b2cSB_ca != null) { // SingleByte
1245N/A for (int b = 0; b < b2cSB_ca.length; b++) {
1245N/A char c = b2cSB_ca[b];
1245N/A if (c == UNMAPPABLE_DECODING)
1245N/A continue;
1245N/A int index = c2bIndex[c >> 8];
1245N/A if (index == 0) {
1245N/A index = off;
1245N/A off += 0x100;
1245N/A c2bIndex[c >> 8] = (char)index;
1245N/A }
1245N/A c2b[index + (c & 0xff)] = (char)b;
1245N/A }
1245N/A }
1245N/A
1245N/A for (int b1 = 0; b1 < b2c.length; b1++) { // DoubleByte
1245N/A char[] db = b2c_ca[b1];
1245N/A if (db == null)
1245N/A continue;
1245N/A for (int b2 = b2Min; b2 <= b2Max; b2++) {
1245N/A char c = db[b2 - b2Min];
1245N/A if (c == UNMAPPABLE_DECODING)
1245N/A continue;
1245N/A int index = c2bIndex[c >> 8];
1245N/A if (index == 0) {
1245N/A index = off;
1245N/A off += 0x100;
1245N/A c2bIndex[c >> 8] = (char)index;
1245N/A }
1245N/A c2b[index + (c & 0xff)] = (char)((b1 << 8) | b2);
1245N/A }
1245N/A }
1245N/A
1245N/A if (c2bNR != null) {
1245N/A // add c->b only nr entries
1245N/A for (int i = 0; i < c2bNR.length(); i += 2) {
1245N/A char b = c2bNR.charAt(i);
1245N/A char c = c2bNR.charAt(i + 1);
1245N/A int index = (c >> 8);
1245N/A if (c2bIndex[index] == 0) {
1245N/A c2bIndex[index] = (char)off;
1245N/A off += 0x100;
1245N/A }
1245N/A index = c2bIndex[index] + (c & 0xff);
1245N/A c2b[index] = b;
1245N/A }
1245N/A }
1245N/A }
1245N/A }
1245N/A
1245N/A // EBCDIC_DBCS_ONLY
1245N/A public static class Encoder_EBCDIC_DBCSONLY extends Encoder {
1245N/A Encoder_EBCDIC_DBCSONLY(Charset cs, byte[] repl,
1245N/A char[] c2b, char[] c2bIndex) {
1245N/A super(cs, 2.0f, 2.0f, repl, c2b, c2bIndex);
1245N/A }
1245N/A
1245N/A public int encodeChar(char ch) {
1245N/A int bb = super.encodeChar(ch);
1245N/A if (bb <= MAX_SINGLEBYTE)
1245N/A return UNMAPPABLE_ENCODING;
1245N/A return bb;
1245N/A }
1245N/A }
1245N/A
1245N/A // for IBM_EBCDIC_DBCS
1245N/A public static class Encoder_EBCDIC extends Encoder {
1245N/A static final int SBCS = 0;
1245N/A static final int DBCS = 1;
1245N/A static final byte SO = 0x0e;
1245N/A static final byte SI = 0x0f;
1245N/A
1245N/A protected int currentState = SBCS;
1245N/A
1245N/A Encoder_EBCDIC(Charset cs, char[] c2b, char[] c2bIndex) {
1245N/A super(cs, 4.0f, 5.0f, new byte[] {(byte)0x6f}, c2b, c2bIndex);
1245N/A }
1245N/A
1245N/A protected void implReset() {
1245N/A currentState = SBCS;
1245N/A }
1245N/A
1245N/A protected CoderResult implFlush(ByteBuffer out) {
1245N/A if (currentState == DBCS) {
1245N/A if (out.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A out.put(SI);
1245N/A }
1245N/A implReset();
1245N/A return CoderResult.UNDERFLOW;
1245N/A }
1245N/A
1245N/A protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
1245N/A char[] sa = src.array();
1245N/A int sp = src.arrayOffset() + src.position();
1245N/A int sl = src.arrayOffset() + src.limit();
1245N/A byte[] da = dst.array();
1245N/A int dp = dst.arrayOffset() + dst.position();
1245N/A int dl = dst.arrayOffset() + dst.limit();
1245N/A
1245N/A try {
1245N/A while (sp < sl) {
1245N/A char c = sa[sp];
1245N/A int bb = encodeChar(c);
1245N/A if (bb == UNMAPPABLE_ENCODING) {
1602N/A if (Character.isSurrogate(c)) {
1245N/A if (sgp().parse(c, sa, sp, sl) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A if (bb > MAX_SINGLEBYTE) { // DoubleByte
1245N/A if (currentState == SBCS) {
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A currentState = DBCS;
1245N/A da[dp++] = SO;
1245N/A }
1245N/A if (dl - dp < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte)(bb >> 8);
1245N/A da[dp++] = (byte)bb;
1245N/A } else { // SingleByte
1245N/A if (currentState == DBCS) {
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A currentState = SBCS;
1245N/A da[dp++] = SI;
1245N/A }
1245N/A if (dl - dp < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A da[dp++] = (byte)bb;
1245N/A
1245N/A }
1245N/A sp++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(sp - src.arrayOffset());
1245N/A dst.position(dp - dst.arrayOffset());
1245N/A }
1245N/A }
1245N/A
1245N/A protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
1245N/A int mark = src.position();
1245N/A try {
1245N/A while (src.hasRemaining()) {
1245N/A char c = src.get();
1245N/A int bb = encodeChar(c);
1245N/A if (bb == UNMAPPABLE_ENCODING) {
1602N/A if (Character.isSurrogate(c)) {
1245N/A if (sgp().parse(c, src) < 0)
1245N/A return sgp.error();
1245N/A return sgp.unmappableResult();
1245N/A }
1245N/A return CoderResult.unmappableForLength(1);
1245N/A }
1245N/A if (bb > MAX_SINGLEBYTE) { // DoubleByte
1245N/A if (currentState == SBCS) {
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A currentState = DBCS;
1245N/A dst.put(SO);
1245N/A }
1245N/A if (dst.remaining() < 2)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte)(bb >> 8));
1245N/A dst.put((byte)(bb));
1245N/A } else { // Single-byte
1245N/A if (currentState == DBCS) {
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A currentState = SBCS;
1245N/A dst.put(SI);
1245N/A }
1245N/A if (dst.remaining() < 1)
1245N/A return CoderResult.OVERFLOW;
1245N/A dst.put((byte)bb);
1245N/A }
1245N/A mark++;
1245N/A }
1245N/A return CoderResult.UNDERFLOW;
1245N/A } finally {
1245N/A src.position(mark);
1245N/A }
1245N/A }
1245N/A }
1245N/A
1245N/A // EUC_SIMPLE
1245N/A public static class Encoder_EUC_SIM extends Encoder {
1245N/A Encoder_EUC_SIM(Charset cs, char[] c2b, char[] c2bIndex) {
1245N/A super(cs, c2b, c2bIndex);
1245N/A }
1245N/A }
1245N/A}