Java Code Examples for java.nio.charset.CoderResult#OVERFLOW

The following examples show how to use java.nio.charset.CoderResult#OVERFLOW . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SingleByte.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = decode(src.get());
            if (c == UNMAPPABLE_DECODING)
                return CoderResult.unmappableForLength(1);
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            dst.put(c);
            mark++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 2
Source File: US_ASCII.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src,
                                     ByteBuffer dst)
{
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (c < 0x80) {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                dst.put((byte)c);
                mark++;
                continue;
            }
            if (sgp.parse(c, src) < 0)
                return sgp.error();
            return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 3
Source File: ISO_8859_1.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src,
                                    CharBuffer dst)
{
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            byte b = sa[sp];
            if (dp >= dl)
                return CoderResult.OVERFLOW;
            da[dp++] = (char)(b & 0xff);
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 4
Source File: SingleByteDecoder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            int b = sa[sp];

            char c = decode(b);
            if (c == '\uFFFD')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = c;
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 5
Source File: US_ASCII.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src,
                                    ByteBuffer dst)
{
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (c < 0x80) {
                if (dp >= dl)
                    return CoderResult.OVERFLOW;
                da[dp] = (byte)c;
                sp++; dp++;
                continue;
            }
            if (sgp.parse(c, sa, sp, sl) < 0)
                return sgp.error();
            return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 6
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.remaining() > 1) {
            int b1 = src.get() & 0xff;
            int b2 = src.get() & 0xff;

            if (!isValidDoubleByte(b1, b2)) {
                return CoderResult.malformedForLength(2);
            }
            // Lookup in the two level index
            int v = b1 * 256 + b2;
            char outputChar = index2.charAt(index1[((v & mask1) >> shift)]
                                            + (v & mask2));
            if (outputChar == REPLACE_CHAR)
                return CoderResult.unmappableForLength(2);

            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            dst.put(outputChar);
            mark += 2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 7
Source File: SJIS_0213.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char[] cc = null;
            int b1 = src.get() & 0xff;
            char c = decodeSingle(b1);
            int inSize = 1, outSize = 1;
            if (c == UNMAPPABLE) {
                if (src.remaining() < 1)
                    return CoderResult.UNDERFLOW;
                int b2 = src.get() & 0xff;
                inSize++;
                c = decodeDouble(b1, b2);
                if (c == UNMAPPABLE) {
                    cc = decodeDoubleEx(b1, b2);
                    if (cc == null) {
                        if (decodeSingle(b2) == UNMAPPABLE)
                            return CoderResult.unmappableForLength(2);
                        else
                            return CoderResult.unmappableForLength(1);
                    }
                    outSize++;
                }
            }
            if (dst.remaining() < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                dst.put(cc[0]);
                dst.put(cc[1]);
            } else {
                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 8
Source File: UTF_32Coder.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    if (!doneBOM && src.hasRemaining()) {
        if (dst.remaining() < 4)
            return CoderResult.OVERFLOW;
        put(BOM_BIG, dst);
        doneBOM = true;
    }
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (!Character.isSurrogate(c)) {
                if (dst.remaining() < 4)
                    return CoderResult.OVERFLOW;
                mark++;
                put(c, dst);
            } else if (Character.isHighSurrogate(c)) {
                if (!src.hasRemaining())
                    return CoderResult.UNDERFLOW;
                char low = src.get();
                if (Character.isLowSurrogate(low)) {
                    if (dst.remaining() < 4)
                        return CoderResult.OVERFLOW;
                    mark += 2;
                    put(Character.toCodePoint(c, low), dst);
                } else {
                    return CoderResult.malformedForLength(1);
                }
            } else {
                // assert Character.isLowSurrogate(c);
                return CoderResult.malformedForLength(1);
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 9
Source File: DBCS_IBM_ASCII_Encoder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    int outputSize = 0;             // size of output

    try {
        while (src.hasRemaining()) {
            int index;
            int theBytes;
            char c = src.get();
            if (Surrogate.is(c)) {
                if (sgp.parse(c, src) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            index = index1[((c & mask1) >> shift)]
                            + (c & mask2);
            if (index < 15000)
                theBytes = (int)(index2.charAt(index));
            else
                theBytes = (int)(index2a.charAt(index-15000));
            b1 = (byte)((theBytes & 0x0000ff00)>>8);
            b2 = (byte)(theBytes & 0x000000ff);

            if (b1 == 0x00 && b2 == 0x00
                && c != '\u0000') {
                    return CoderResult.unmappableForLength(1);
            }

            if (b1 == 0) {
                if (dst.remaining() < 1)
                    return CoderResult.OVERFLOW;
                dst.put((byte) b2);
            } else {
                if (dst.remaining() < 2)
                    return CoderResult.OVERFLOW;
                dst.put((byte) b1);
                dst.put((byte) b2);
            }
            mark++;
         }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 10
Source File: SimpleEUCDecoder.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            int byte1, byte2;
            int inputSize = 1;
            char outputChar = '\uFFFD';

            byte1 = sa[sp] & 0xff;

            if ( byte1 <= 0x9f ) {  // < 0x9f has its own table (G0)
                if (byte1 == SS2 || byte1 == SS3 ) {
                    // No support provided for G2/G3 at this time.
                    return CoderResult.malformedForLength(1);
                }
                outputChar = byteToCharTable.charAt(byte1);
            } else if (byte1 < 0xa1 || byte1 > 0xfe) {  // invalid range?
                return CoderResult.malformedForLength(1);
            } else {                                        // (G1)
                if (sl - sp < 2) {
                    return CoderResult.UNDERFLOW;
                }
                byte2 = sa[sp + 1] & 0xff;
                inputSize++;
                if ( byte2 < 0xa1 || byte2 > 0xfe) {
                    return CoderResult.malformedForLength(2);
                }
                outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
            }
            if  (outputChar == '\uFFFD') {
                return CoderResult.unmappableForLength(inputSize);
            }
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 11
Source File: HKSCS.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();

    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl) {
            int b1 = sa[sp] & 0xff;
            char c = decodeSingle(b1);
            int inSize = 1, outSize = 1;
            char[] cc = null;
            if (c == UNMAPPABLE_DECODING) {
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                int b2 = sa[sp + 1] & 0xff;
                inSize++;
                if (b2 < b2Min || b2 > b2Max)
                    return CoderResult.unmappableForLength(2);
                c = decodeDouble(b1, b2);           //bmp
                if (c == UNMAPPABLE_DECODING) {
                    c = decodeDoubleEx(b1, b2);     //supp
                    if (c == UNMAPPABLE_DECODING) {
                        c = decodeBig5(b1, b2);     //big5
                        if (c == UNMAPPABLE_DECODING)
                            return CoderResult.unmappableForLength(2);
                    } else {
                        // supplementary character in u+2xxxx area
                        outSize = 2;
                    }
                }
            }
            if (dl - dp < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                // supplementary characters
                da[dp++] = Surrogate.high(0x20000 + c);
                da[dp++] = Surrogate.low(0x20000 + c);
            } else {
                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 12
Source File: EUC_JP_LINUX_OLD.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src,
                                     CharBuffer dst)
{
    int mark = src.position();
    char outputChar = REPLACE_CHAR; // U+FFFD;

    try {
        while (src.hasRemaining()) {
            int b1 = src.get() & 0xff;
            int inputSize = 1;

            if ((b1 & 0x80) == 0) {
                outputChar = (char)b1;
            } else {    // Multibyte char

                if ((b1 & 0xff) == 0x8f) { // JIS0212 not supported
                    if (src.remaining() < 2)
                        return CoderResult.UNDERFLOW;
                    return CoderResult.unmappableForLength(3);
                } else {
                    // JIS0208
                    if (src.remaining() < 1)
                        return CoderResult.UNDERFLOW;
                    int b2 = src.get() & 0xff;
                    inputSize++;
                    outputChar = decodeDouble(b1, b2);
                }
            }

            if (outputChar == REPLACE_CHAR)
                return CoderResult.unmappableForLength(inputSize);
            if (dst.remaining() < 1)
                return CoderResult.OVERFLOW;
            dst.put(outputChar);
            mark += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 13
Source File: SingleByteEncoder.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;

            char e = index2.charAt(index1[(c & mask1) >> shift]
                                   + (c & mask2));

            // If output byte is zero because input char is zero
            // then character is mappable, o.w. fail
            if (e == '\u0000' && c != '\u0000')
                return CoderResult.unmappableForLength(1);

            sp++;
            da[dp++] = (byte)e;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 14
Source File: CESU_8.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static CoderResult overflow(CharBuffer src, int sp,
                                    ByteBuffer dst, int dp) {
    updatePositions(src, sp, dst, dp);
    return CoderResult.OVERFLOW;
}
 
Example 15
Source File: DoubleByte.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            int b1 = src.get() & 0xff;
            int inSize = 1;
            if (b1 == SO) {  // Shift out
                if (currentState != SBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = DBCS;
            } else if (b1 == SI) {
                if (currentState != DBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = SBCS;
            } else {
                char c = UNMAPPABLE_DECODING;
                if (currentState == SBCS) {
                    c = b2cSB[b1];
                    if (c == UNMAPPABLE_DECODING)
                        return CoderResult.unmappableForLength(1);
                } else {
                    if (src.remaining() < 1)
                        return CoderResult.UNDERFLOW;
                    int b2 = src.get()&0xff;
                    if (b2 < b2Min || b2 > b2Max ||
                        (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
                        if (!isDoubleByte(b1, b2))
                            return CoderResult.malformedForLength(2);
                        return CoderResult.unmappableForLength(2);
                    }
                    inSize++;
                }

                if (dst.remaining() < 1)
                    return CoderResult.OVERFLOW;

                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 16
Source File: DoubleByteEncoder.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                char c2 = sa[sp + 1];

                byte[] outputBytes = new byte[2];
                outputBytes = encodeSurrogate(c, c2);

                if (outputBytes == null) {
                    return sgp.unmappableResult();
                }
                else {
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = outputBytes[0];
                    da[dp++] = outputBytes[1];
                    sp += 2;
                    continue;
                }
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            int b = encodeSingle(c);
            if (b != -1) { // Single Byte
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)b;
                sp++;
                continue;
            }

            int ncode  = encodeDouble(c);
            if (ncode != 0 && c != '\u0000' ) {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) ((ncode & 0xff00) >> 8);
                da[dp++] = (byte) (ncode & 0xff);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
            }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 17
Source File: EUC_JP.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src,
                                     CharBuffer dst)
{
    int mark = src.position();
    int b1 = 0, b2 = 0;
    int inputSize = 0;
    char outputChar = UNMAPPABLE_DECODING;

    try {
        while (src.hasRemaining()) {
            b1 = src.get() & 0xff;
            inputSize = 1;
            if ((b1 & 0x80) == 0) {
                outputChar = (char)b1;
            } else {                         // Multibyte char
                if (b1 == 0x8f) {   // JIS0212
                    if (src.remaining() < 2)
                       return CoderResult.UNDERFLOW;
                    b1 = src.get() & 0xff;
                    b2 = src.get() & 0xff;
                    inputSize += 2;
                    if (dec0212 == null)    // JIS02012 not supported
                        return CoderResult.unmappableForLength(inputSize);
                    outputChar = dec0212.decodeDouble(b1-0x80, b2-0x80);
                } else {                     // JIS0201 JIS0208
                    if (src.remaining() < 1)
                       return CoderResult.UNDERFLOW;
                    b2 = src.get() & 0xff;
                    inputSize++;
                    outputChar = decodeDouble(b1, b2);
                }
            }
            if (outputChar == UNMAPPABLE_DECODING) {
                return CoderResult.unmappableForLength(inputSize);
            }
        if (dst.remaining() < 1)
            return CoderResult.OVERFLOW;
        dst.put(outputChar);
        mark += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 18
Source File: SingleByteEncoder.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;

            char e = index2.charAt(index1[(c & mask1) >> shift]
                                   + (c & mask2));

            // If output byte is zero because input char is zero
            // then character is mappable, o.w. fail
            if (e == '\u0000' && c != '\u0000')
                return CoderResult.unmappableForLength(1);

            sp++;
            da[dp++] = (byte)e;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 19
Source File: SimpleEUCDecoder.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            int byte1, byte2;
            int inputSize = 1;
            char outputChar = '\uFFFD';

            byte1 = sa[sp] & 0xff;

            if ( byte1 <= 0x9f ) {  // < 0x9f has its own table (G0)
                if (byte1 == SS2 || byte1 == SS3 ) {
                    // No support provided for G2/G3 at this time.
                    return CoderResult.malformedForLength(1);
                }
                outputChar = byteToCharTable.charAt(byte1);
            } else if (byte1 < 0xa1 || byte1 > 0xfe) {  // invalid range?
                return CoderResult.malformedForLength(1);
            } else {                                        // (G1)
                if (sl - sp < 2) {
                    return CoderResult.UNDERFLOW;
                }
                byte2 = sa[sp + 1] & 0xff;
                inputSize++;
                if ( byte2 < 0xa1 || byte2 > 0xfe) {
                    return CoderResult.malformedForLength(2);
                }
                outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
            }
            if  (outputChar == '\uFFFD') {
                return CoderResult.unmappableForLength(inputSize);
            }
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 20
Source File: DoubleByte.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            int b1 = src.get() & 0xff;
            int inSize = 1;
            if (b1 == SO) {  // Shift out
                if (currentState != SBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = DBCS;
            } else if (b1 == SI) {
                if (currentState != DBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = SBCS;
            } else {
                char c = UNMAPPABLE_DECODING;
                if (currentState == SBCS) {
                    c = b2cSB[b1];
                    if (c == UNMAPPABLE_DECODING)
                        return CoderResult.unmappableForLength(1);
                } else {
                    if (src.remaining() < 1)
                        return CoderResult.UNDERFLOW;
                    int b2 = src.get()&0xff;
                    if (b2 < b2Min || b2 > b2Max ||
                        (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
                        if (!isDoubleByte(b1, b2))
                            return CoderResult.malformedForLength(2);
                        return CoderResult.unmappableForLength(2);
                    }
                    inSize++;
                }

                if (dst.remaining() < 1)
                    return CoderResult.OVERFLOW;

                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}