Java Code Examples for java.nio.charset.CoderResult#malformedForLength()

The following examples show how to use java.nio.charset.CoderResult#malformedForLength() . 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: US_ASCII.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private CoderResult decodeBufferLoop(ByteBuffer src,
                                     CharBuffer dst)
{
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            byte b = src.get();
            if (b >= 0) {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                dst.put((char)b);
                mark++;
                continue;
            }
            return CoderResult.malformedForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 2
Source File: UTF_8.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static CoderResult malformedForLength(ByteBuffer src,
                                              int mark,
                                              int malformedNB)
{
    src.position(mark);
    return CoderResult.malformedForLength(malformedNB);
}
 
Example 3
Source File: Surrogate.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates one or two UTF-16 characters to represent the given UCS-4
 * character.
 *
 * @param  uc   The UCS-4 character
 * @param  len  The number of input bytes from which the UCS-4 value
 *              was constructed (used when creating result objects)
 * @param  da   The destination array, to which one or two UTF-16
 *              characters will be written
 * @param  dp   The destination position
 * @param  dl   The destination limit
 *
 * @returns  Either a positive count of the number of UTF-16 characters
 *           written to the destination buffer, or -1, in which case
 *           error() will return a descriptive result object
 */
public int generate(int uc, int len, char[] da, int dp, int dl) {
    if (Character.isBmpCodePoint(uc)) {
        char c = (char) uc;
        if (Character.isSurrogate(c)) {
            error = CoderResult.malformedForLength(len);
            return -1;
        }
        if (dl - dp < 1) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        da[dp] = c;
        error = null;
        return 1;
    } else if (Character.isValidCodePoint(uc)) {
        if (dl - dp < 2) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        da[dp] = Character.highSurrogate(uc);
        da[dp + 1] = Character.lowSurrogate(uc);
        error = null;
        return 2;
    } else {
        error = CoderResult.unmappableForLength(len);
        return -1;
    }
}
 
Example 4
Source File: UTF_8.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static CoderResult malformedForLength(ByteBuffer src,
                                              int sp,
                                              CharBuffer dst,
                                              int dp,
                                              int malformedNB)
{
    updatePositions(src, sp, dst, dp);
    return CoderResult.malformedForLength(malformedNB);
}
 
Example 5
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From openjdk-jdk8u-backup 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 6
Source File: Surrogate.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates one or two UTF-16 characters to represent the given UCS-4
 * character.
 *
 * @param  uc   The UCS-4 character
 * @param  len  The number of input bytes from which the UCS-4 value
 *              was constructed (used when creating result objects)
 * @param  dst  The destination buffer, to which one or two UTF-16
 *              characters will be written
 *
 * @returns  Either a positive count of the number of UTF-16 characters
 *           written to the destination buffer, or -1, in which case
 *           error() will return a descriptive result object
 */
public int generate(int uc, int len, CharBuffer dst) {
    if (Character.isBmpCodePoint(uc)) {
        char c = (char) uc;
        if (Character.isSurrogate(c)) {
            error = CoderResult.malformedForLength(len);
            return -1;
        }
        if (dst.remaining() < 1) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        dst.put(c);
        error = null;
        return 1;
    } else if (Character.isValidCodePoint(uc)) {
        if (dst.remaining() < 2) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        dst.put(Character.highSurrogate(uc));
        dst.put(Character.lowSurrogate(uc));
        error = null;
        return 2;
    } else {
        error = CoderResult.unmappableForLength(len);
        return -1;
    }
}
 
Example 7
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From jdk8u-jdk 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 8
Source File: UTF_32Coder.java    From openjdk-8-source 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: Surrogate.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Generates one or two UTF-16 characters to represent the given UCS-4
 * character.
 *
 * @param  uc   The UCS-4 character
 * @param  len  The number of input bytes from which the UCS-4 value
 *              was constructed (used when creating result objects)
 * @param  dst  The destination buffer, to which one or two UTF-16
 *              characters will be written
 *
 * @return  Either a positive count of the number of UTF-16 characters
 *          written to the destination buffer, or -1, in which case
 *          error() will return a descriptive result object
 */
public int generate(int uc, int len, CharBuffer dst) {
    if (Character.isBmpCodePoint(uc)) {
        char c = (char) uc;
        if (Character.isSurrogate(c)) {
            error = CoderResult.malformedForLength(len);
            return -1;
        }
        if (dst.remaining() < 1) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        dst.put(c);
        error = null;
        return 1;
    } else if (Character.isValidCodePoint(uc)) {
        if (dst.remaining() < 2) {
            error = CoderResult.OVERFLOW;
            return -1;
        }
        dst.put(Character.highSurrogate(uc));
        dst.put(Character.lowSurrogate(uc));
        error = null;
        return 2;
    } else {
        error = CoderResult.unmappableForLength(len);
        return -1;
    }
}
 
Example 10
Source File: SimpleEUCDecoder.java    From openjdk-8 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: ISO2022.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();
    int b1 = 0, b2 = 0, b3 = 0;

    try {
        while (src.hasRemaining()) {
            b1 = src.get();
            int inputSize = 1;
            switch (b1) {
                case ISO_SO:
                    shiftout = true;
                    break;
                case ISO_SI:
                    shiftout = false;
                    break;
                case ISO_ESC:
                    if (src.remaining() < minDesignatorLength)
                        return CoderResult.UNDERFLOW;

                    int desig = findDesigBuf(src, SODesig);
                    if (desig != -1) {
                        curSODes = desig;
                        inputSize = SODesig[desig].length + 1;
                        break;
                    }
                    desig = findDesigBuf(src, SS2Desig);
                    if (desig != -1) {
                        curSS2Des = desig;
                        inputSize = SS2Desig[desig].length + 1;
                        break;
                    }
                    desig = findDesigBuf(src, SS3Desig);
                    if (desig != -1) {
                        curSS3Des = desig;
                        inputSize = SS3Desig[desig].length + 1;
                        break;
                    }

                    if (src.remaining() < 1)
                        return CoderResult.UNDERFLOW;
                    b1 = src.get();
                    switch(b1) {
                        case ISO_SS2_7:
                            if (src.remaining() < 2)
                                return CoderResult.UNDERFLOW;
                            b2 = src.get();
                            b3 = src.get();
                            if (dst.remaining() < 1)
                                return CoderResult.OVERFLOW;
                            dst.put(decode((byte)b2,
                                           (byte)b3,
                                           SS2Flag));
                            inputSize = 4;
                            break;
                        case ISO_SS3_7:
                            if (src.remaining() < 2)
                                return CoderResult.UNDERFLOW;
                            b2 = src.get();
                            b3 = src.get();
                            if (dst.remaining() < 1)
                                return CoderResult.OVERFLOW;
                            dst.put(decode((byte)b2,
                                           (byte)b3,
                                           SS3Flag));
                            inputSize = 4;
                            break;
                        default:
                            return CoderResult.malformedForLength(2);
                    }
                    break;
                default:
                    if (dst.remaining() < 1)
                        return CoderResult.OVERFLOW;
                    if (!shiftout) {
                        dst.put((char)(b1 & 0xff));
                    } else {
                        if (dst.remaining() < 1)
                            return CoderResult.OVERFLOW;
                        if (src.remaining() < 1)
                            return CoderResult.UNDERFLOW;
                        b2 = src.get() & 0xff;
                        dst.put(decode((byte)b1,
                                              (byte)b2,
                                                SOFlag));
                        inputSize = 2;
                    }
                    break;
            }
            mark += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } catch (Exception e) { e.printStackTrace(); return CoderResult.OVERFLOW; }
    finally {
        src.position(mark);
    }
}
 
Example 12
Source File: DoubleByte.java    From dragonwell8_jdk 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 {
        // don't check dp/dl together here, it's possible to
        // decdoe a SO/SI without space in output buffer.
        while (sp < sl) {
            int b1 = sa[sp] & 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 (sl - sp < 2)
                        return CoderResult.UNDERFLOW;
                    int b2 = sa[sp + 1] & 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 (dl - dp < 1)
                    return CoderResult.OVERFLOW;

                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 13
Source File: ISO2022.java    From jdk8u-jdk 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, b3 = 0;

    try {
        while (src.hasRemaining()) {
            b1 = src.get();
            int inputSize = 1;
            switch (b1) {
                case ISO_SO:
                    shiftout = true;
                    break;
                case ISO_SI:
                    shiftout = false;
                    break;
                case ISO_ESC:
                    if (src.remaining() < minDesignatorLength)
                        return CoderResult.UNDERFLOW;

                    int desig = findDesigBuf(src, SODesig);
                    if (desig != -1) {
                        curSODes = desig;
                        inputSize = SODesig[desig].length + 1;
                        break;
                    }
                    desig = findDesigBuf(src, SS2Desig);
                    if (desig != -1) {
                        curSS2Des = desig;
                        inputSize = SS2Desig[desig].length + 1;
                        break;
                    }
                    desig = findDesigBuf(src, SS3Desig);
                    if (desig != -1) {
                        curSS3Des = desig;
                        inputSize = SS3Desig[desig].length + 1;
                        break;
                    }

                    if (src.remaining() < 1)
                        return CoderResult.UNDERFLOW;
                    b1 = src.get();
                    switch(b1) {
                        case ISO_SS2_7:
                            if (src.remaining() < 2)
                                return CoderResult.UNDERFLOW;
                            b2 = src.get();
                            b3 = src.get();
                            if (dst.remaining() < 1)
                                return CoderResult.OVERFLOW;
                            dst.put(decode((byte)b2,
                                           (byte)b3,
                                           SS2Flag));
                            inputSize = 4;
                            break;
                        case ISO_SS3_7:
                            if (src.remaining() < 2)
                                return CoderResult.UNDERFLOW;
                            b2 = src.get();
                            b3 = src.get();
                            if (dst.remaining() < 1)
                                return CoderResult.OVERFLOW;
                            dst.put(decode((byte)b2,
                                           (byte)b3,
                                           SS3Flag));
                            inputSize = 4;
                            break;
                        default:
                            return CoderResult.malformedForLength(2);
                    }
                    break;
                default:
                    if (dst.remaining() < 1)
                        return CoderResult.OVERFLOW;
                    if (!shiftout) {
                        dst.put((char)(b1 & 0xff));
                    } else {
                        if (dst.remaining() < 1)
                            return CoderResult.OVERFLOW;
                        if (src.remaining() < 1)
                            return CoderResult.UNDERFLOW;
                        b2 = src.get() & 0xff;
                        dst.put(decode((byte)b1,
                                              (byte)b2,
                                                SOFlag));
                        inputSize = 2;
                    }
                    break;
            }
            mark += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } catch (Exception e) { e.printStackTrace(); return CoderResult.OVERFLOW; }
    finally {
        src.position(mark);
    }
}
 
Example 14
Source File: EUC_TW.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();

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

    int inSize;
    int outSize;

    try {
        while (sp < sl) {
            char c = sa[sp];
            inSize = 1;
            if (c < 0x80) {  // ASCII
                bb[0] = (byte)c;
                outSize = 1;
            } else {
                outSize = toEUC(c, bb);
                if (outSize == -1) {
                    // to check surrogates only after BMP failed
                    // has the benefit of improving the BMP encoding
                    // 10% faster, with the price of the slowdown of
                    // supplementary character encoding. given the use
                    // of supplementary characters is really rare, this
                    // is something worth doing.
                    if (Character.isHighSurrogate(c)) {
                        if ((sp + 1) == sl)
                            return CoderResult.UNDERFLOW;
                        if (!Character.isLowSurrogate(sa[sp + 1]))
                            return CoderResult.malformedForLength(1);
                        outSize = toEUC(c, sa[sp+1], bb);
                            inSize = 2;
                    } else if (Character.isLowSurrogate(c)) {
                        return CoderResult.malformedForLength(1);
                    }
                }
            }
            if (outSize == -1)
                return CoderResult.unmappableForLength(inSize);
            if ( dl - dp < outSize)
                return CoderResult.OVERFLOW;
            for (int i = 0; i < outSize; i++)
                da[dp++] = bb[i];
            sp  += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 15
Source File: EUC_TW.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();

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

    int inSize;
    int outSize;

    try {
        while (sp < sl) {
            char c = sa[sp];
            inSize = 1;
            if (c < 0x80) {  // ASCII
                bb[0] = (byte)c;
                outSize = 1;
            } else {
                outSize = toEUC(c, bb);
                if (outSize == -1) {
                    // to check surrogates only after BMP failed
                    // has the benefit of improving the BMP encoding
                    // 10% faster, with the price of the slowdown of
                    // supplementary character encoding. given the use
                    // of supplementary characters is really rare, this
                    // is something worth doing.
                    if (Character.isHighSurrogate(c)) {
                        if ((sp + 1) == sl)
                            return CoderResult.UNDERFLOW;
                        if (!Character.isLowSurrogate(sa[sp + 1]))
                            return CoderResult.malformedForLength(1);
                        outSize = toEUC(c, sa[sp+1], bb);
                            inSize = 2;
                    } else if (Character.isLowSurrogate(c)) {
                        return CoderResult.malformedForLength(1);
                    }
                }
            }
            if (outSize == -1)
                return CoderResult.unmappableForLength(inSize);
            if ( dl - dp < outSize)
                return CoderResult.OVERFLOW;
            for (int i = 0; i < outSize; i++)
                da[dp++] = bb[i];
            sp  += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 16
Source File: SJIS_0213.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected 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) {
            int db;
            char c = sa[sp];
            if (leftoverBase != 0) {
                boolean isComp = false;
                db = encodeComposite(leftoverBase, c);
                if (db == UNMAPPABLE)
                    db = encodeChar(leftoverBase);
                else
                    isComp = true;
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)(db >> 8);
                da[dp++] = (byte)db;
                leftoverBase = 0;
                if (isComp) {
                    sp++;
                    continue;
                }
            }
            if (isCompositeBase(c)) {
                leftoverBase = c;
            } else {
                db = encodeChar(c);
                if (db <= MAX_SINGLEBYTE) {      // SingleByte
                    if (dl <= dp)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)db;
                } else if (db != UNMAPPABLE) {   // DoubleByte
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)(db >> 8);
                    da[dp++] = (byte)db;
                } else if (Character.isHighSurrogate(c)) {
                    if ((sp + 1) == sl)
                        return CoderResult.UNDERFLOW;
                    char c2 = sa[sp + 1];
                    if (!Character.isLowSurrogate(c2))
                        return CoderResult.malformedForLength(1);
                    db = encodeSurrogate(c, c2);
                    if (db == UNMAPPABLE)
                        return CoderResult.unmappableForLength(2);
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)(db >> 8);
                    da[dp++] = (byte)db;
                    sp++;
                } else if (Character.isLowSurrogate(c)) {
                    return CoderResult.malformedForLength(1);
                } else {
                    return CoderResult.unmappableForLength(1);
                }
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 17
Source File: DoubleByte.java    From jdk8u_jdk 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 {
        // don't check dp/dl together here, it's possible to
        // decdoe a SO/SI without space in output buffer.
        while (sp < sl) {
            int b1 = sa[sp] & 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 (sl - sp < 2)
                        return CoderResult.UNDERFLOW;
                    int b2 = sa[sp + 1] & 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 (dl - dp < 1)
                    return CoderResult.OVERFLOW;

                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 18
Source File: EUC_TW.java    From openjdk-8 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();

    int inSize;
    int outSize;

    try {
        while (sp < sl) {
            char c = sa[sp];
            inSize = 1;
            if (c < 0x80) {  // ASCII
                bb[0] = (byte)c;
                outSize = 1;
            } else {
                outSize = toEUC(c, bb);
                if (outSize == -1) {
                    // to check surrogates only after BMP failed
                    // has the benefit of improving the BMP encoding
                    // 10% faster, with the price of the slowdown of
                    // supplementary character encoding. given the use
                    // of supplementary characters is really rare, this
                    // is something worth doing.
                    if (Character.isHighSurrogate(c)) {
                        if ((sp + 1) == sl)
                            return CoderResult.UNDERFLOW;
                        if (!Character.isLowSurrogate(sa[sp + 1]))
                            return CoderResult.malformedForLength(1);
                        outSize = toEUC(c, sa[sp+1], bb);
                            inSize = 2;
                    } else if (Character.isLowSurrogate(c)) {
                        return CoderResult.malformedForLength(1);
                    }
                }
            }
            if (outSize == -1)
                return CoderResult.unmappableForLength(inSize);
            if ( dl - dp < outSize)
                return CoderResult.OVERFLOW;
            for (int i = 0; i < outSize; i++)
                da[dp++] = bb[i];
            sp  += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 19
Source File: SJIS_0213.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected 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) {
            int db;
            char c = sa[sp];
            if (leftoverBase != 0) {
                boolean isComp = false;
                db = encodeComposite(leftoverBase, c);
                if (db == UNMAPPABLE)
                    db = encodeChar(leftoverBase);
                else
                    isComp = true;
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)(db >> 8);
                da[dp++] = (byte)db;
                leftoverBase = 0;
                if (isComp) {
                    sp++;
                    continue;
                }
            }
            if (isCompositeBase(c)) {
                leftoverBase = c;
            } else {
                db = encodeChar(c);
                if (db <= MAX_SINGLEBYTE) {      // SingleByte
                    if (dl <= dp)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)db;
                } else if (db != UNMAPPABLE) {   // DoubleByte
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)(db >> 8);
                    da[dp++] = (byte)db;
                } else if (Character.isHighSurrogate(c)) {
                    if ((sp + 1) == sl)
                        return CoderResult.UNDERFLOW;
                    char c2 = sa[sp + 1];
                    if (!Character.isLowSurrogate(c2))
                        return CoderResult.malformedForLength(1);
                    db = encodeSurrogate(c, c2);
                    if (db == UNMAPPABLE)
                        return CoderResult.unmappableForLength(2);
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = (byte)(db >> 8);
                    da[dp++] = (byte)db;
                    sp++;
                } else if (Character.isLowSurrogate(c)) {
                    return CoderResult.malformedForLength(1);
                } else {
                    return CoderResult.unmappableForLength(1);
                }
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 20
Source File: EUC_TW.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();

    int inSize;
    int outSize;

    try {
        while (sp < sl) {
            char c = sa[sp];
            inSize = 1;
            if (c < 0x80) {  // ASCII
                bb[0] = (byte)c;
                outSize = 1;
            } else {
                outSize = toEUC(c, bb);
                if (outSize == -1) {
                    // to check surrogates only after BMP failed
                    // has the benefit of improving the BMP encoding
                    // 10% faster, with the price of the slowdown of
                    // supplementary character encoding. given the use
                    // of supplementary characters is really rare, this
                    // is something worth doing.
                    if (Character.isHighSurrogate(c)) {
                        if ((sp + 1) == sl)
                            return CoderResult.UNDERFLOW;
                        if (!Character.isLowSurrogate(sa[sp + 1]))
                            return CoderResult.malformedForLength(1);
                        outSize = toEUC(c, sa[sp+1], bb);
                            inSize = 2;
                    } else if (Character.isLowSurrogate(c)) {
                        return CoderResult.malformedForLength(1);
                    }
                }
            }
            if (outSize == -1)
                return CoderResult.unmappableForLength(inSize);
            if ( dl - dp < outSize)
                return CoderResult.OVERFLOW;
            for (int i = 0; i < outSize; i++)
                da[dp++] = bb[i];
            sp  += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}