Java Code Examples for java.nio.CharBuffer#arrayOffset()

The following examples show how to use java.nio.CharBuffer#arrayOffset() . 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: X11CNS11643.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(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 ( c > '\u007f'&& c < '\uFFFE') {
                int nb = toEUC(c, bb);
                if (nb != -1) {
                    int p = 0;
                    if (nb == 4)
                        p = (bb[1] & 0xff) - 0xa0;
                    if (p == plane) {
                        if (dl - dp < 2)
                            return CoderResult.OVERFLOW;
                        if (nb == 2) {
                            da[dp++] = (byte)(bb[0] & 0x7f);
                            da[dp++] = (byte)(bb[1] & 0x7f);
                        } else {
                            da[dp++] = (byte)(bb[2] & 0x7f);
                            da[dp++] = (byte)(bb[3] & 0x7f);
                        }
                        sp++;
                        continue;
                    }
                }
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 2
Source File: US_ASCII.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 (b >= 0) {
                if (dp >= dl)
                    return CoderResult.OVERFLOW;
                da[dp++] = (char)b;
                sp++;
                continue;
            }
            return CoderResult.malformedForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 3
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From jdk8u-dev-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 + 1 < sl) {
            int b1 = sa[sp] & 0xff;
            int b2 = sa[sp + 1] & 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 (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += 2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 4
Source File: X11GB2312.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(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 (c <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int ncode = encodeDouble(c);
            if (ncode != 0 && c != '\u0000' ) {
                da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
                da[dp++] = (byte) (ncode & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 5
Source File: WingDings.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected CoderResult encodeLoop(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 (dl - dp < 1)
                return CoderResult.OVERFLOW;
            if (!canEncode(c))
                return CoderResult.unmappableForLength(1);
            sp++;
            da[dp++] = table[c - 0x2700];
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 6
Source File: Symbol.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(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 (dl - dp < 1)
                return CoderResult.OVERFLOW;
            if (!canEncode(c))
                return CoderResult.unmappableForLength(1);
            sp++;
            if (c >= 0x2200 && c <= 0x22ef){
                da[dp++] = table_math[c - 0x2200];
            } else if (c >= 0x0391 && c <= 0x03d6) {
                da[dp++]= table_greek[c - 0x0391];
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 7
Source File: X11CNS11643.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(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 (c >= '\uFFFE' || c <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int cns = getNative(c);
            int p = cns >> 16;
            if (p == 1 && plane == 0 ||
                p == 2 && plane == 2 ||
                p == 3 && plane == 3) {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) ((cns  >> 8) & 0x7f);
                da[dp++] = (byte) (cns & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 8
Source File: SingleByteDecoder.java    From TencentKona-8 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 9
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From openjdk-jdk8u 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 + 1 < sl) {
            int b1 = sa[sp] & 0xff;
            int b2 = sa[sp + 1] & 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 (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += 2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 10
Source File: X11CNS11643.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(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 (c >= '\uFFFE' || c <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int cns = getNative(c);
            int p = cns >> 16;
            if (p == 1 && plane == 0 ||
                p == 2 && plane == 2 ||
                p == 3 && plane == 3) {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) ((cns  >> 8) & 0x7f);
                da[dp++] = (byte) (cns & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 11
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 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 + 1 < sl) {
            int b1 = sa[sp] & 0xff;
            int b2 = sa[sp + 1] & 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 (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += 2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 12
Source File: SingleByteEncoder.java    From TencentKona-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();
    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 13
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 14
Source File: SJIS_0213.java    From openjdk-jdk8u-backup 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();

    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) {
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                int b2 = sa[sp + 1] & 0xff;
                c = decodeDouble(b1, b2);
                inSize++;
                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 (dl - dp < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                da[dp++] = cc[0];
                da[dp++] = cc[1];
            } else {
                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 15
Source File: DBCS_IBM_EBCDIC_Encoder.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 outputSize = 0;             // size of output
    int spaceNeeded;

    try {
        while (sp < sl) {
            int index;
            int theBytes;
            char c = sa[sp];
            if (Surrogate.is(c)) {
                if (sgp.parse(c, sa, sp, sl) < 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 (currentState == DBCS && b1 == 0x00) {
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                currentState = SBCS;
                da[dp++] = SI;
            } else if (currentState == SBCS && b1 != 0x00) {
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                currentState = DBCS;
                da[dp++] = SO;
            }
            if (currentState == DBCS)
                spaceNeeded = 2;
            else
                spaceNeeded = 1;
            if (dl - dp < spaceNeeded)
                return CoderResult.OVERFLOW;

            if (currentState == SBCS)
                da[dp++] = b2;
            else {
                da[dp++] = b1;
                da[dp++] = b2;
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 16
Source File: SimpleEUCEncoder.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);

    int     index;
    int     spaceNeeded;
    int     i;

    try {
        while (sp < sl) {
            boolean allZeroes = true;
            char inputChar = sa[sp];
            if (Character.isSurrogate(inputChar)) {
                if (sgp.parse(inputChar, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }

            if (inputChar >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            String theChars;
            char   aChar;

             // We have a valid character, get the bytes for it
            index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2);

            if (index < 7500)
                theChars = index2;
            else if (index < 15000) {
                 index = index - 7500;
                 theChars = index2a;
            } else if (index < 22500){
                index = index - 15000;
                theChars = index2b;
            }
            else {
                index = index - 22500;
                theChars = index2c;
            }

            aChar = theChars.charAt(2*index);
            outputByte[0] = (byte)((aChar & 0xff00)>>8);
            outputByte[1] = (byte)(aChar & 0x00ff);
            aChar = theChars.charAt(2*index + 1);
            outputByte[2] = (byte)((aChar & 0xff00)>>8);
            outputByte[3] = (byte)(aChar & 0x00ff);

        for (i = 0; i < outputByte.length; i++) {
            if (outputByte[i] != 0x00) {
            allZeroes = false;
            break;
            }
        }

        if (allZeroes && inputChar != '\u0000') {
            return CoderResult.unmappableForLength(1);
        }

        int oindex = 0;

        for (spaceNeeded = outputByte.length;
             spaceNeeded > 1; spaceNeeded--){
            if (outputByte[oindex++] != 0x00 )
                break;
        }

        if (dp + spaceNeeded > dl)
            return CoderResult.OVERFLOW;

        for (i = outputByte.length - spaceNeeded;
             i < outputByte.length; i++) {
                da[dp++] = outputByte[i];
        }
        sp++;
    }
    return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 17
Source File: DoubleByte.java    From openjdk-jdk9 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: 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 19
Source File: ISO2022.java    From dragonwell8_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);

    int outputSize = 0;
    byte[]  outputByte = new byte[8];
    newshiftout = shiftout;
    newSODesDefined = SODesDefined;
    newSS2DesDefined = SS2DesDefined;
    newSS3DesDefined = SS3DesDefined;

    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 < 0x80) {     // ASCII
                if (shiftout){
                    newshiftout = false;
                    outputSize = 2;
                    outputByte[0] = ISO_SI;
                    outputByte[1] = (byte)(c & 0x7f);
                } else {
                    outputSize = 1;
                    outputByte[0] = (byte)(c & 0x7f);
                }
                if(sa[sp] == '\n'){
                    newSODesDefined = false;
                    newSS2DesDefined = false;
                    newSS3DesDefined = false;
                }
            } else {
                outputSize = unicodeToNative(c, outputByte);
                if (outputSize == 0) {
                    return CoderResult.unmappableForLength(1);
                }
            }
            if (dl - dp < outputSize)
                return CoderResult.OVERFLOW;

            for (int i = 0; i < outputSize; i++)
                da[dp++] = outputByte[i];
            sp++;
            shiftout = newshiftout;
            SODesDefined = newSODesDefined;
            SS2DesDefined = newSS2DesDefined;
            SS3DesDefined = newSS3DesDefined;
        }
        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 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];
            int bb = encodeChar(c);
            if (bb == UNMAPPABLE_ENCODING) {
                if (Character.isSurrogate(c)) {
                    if (sgp().parse(c, sa, sp, sl) < 0)
                        return sgp.error();
                    return sgp.unmappableResult();
                }
                return CoderResult.unmappableForLength(1);
            }
            if (bb > MAX_SINGLEBYTE) {  // DoubleByte
                if (currentState == SBCS) {
                    if (dl - dp < 1)
                        return CoderResult.OVERFLOW;
                    currentState = DBCS;
                    da[dp++] = SO;
                }
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)(bb >> 8);
                da[dp++] = (byte)bb;
            } else {                    // SingleByte
                if (currentState == DBCS) {
                    if (dl - dp < 1)
                        return CoderResult.OVERFLOW;
                    currentState = SBCS;
                    da[dp++] = SI;
                }
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)bb;

            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}