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

The following examples show how to use java.nio.CharBuffer#remaining() . 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: CloverString.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Characters are copied from this sequence to the given {@link CharBuffer}.
 */
public void getChars(CharBuffer charBuffer) {
	if (count > charBuffer.remaining()) {
		throw new BufferOverflowException();
	}
	charBuffer.put(value, 0, count);
}
 
Example 2
Source File: SJIS_0213.java    From openjdk-jdk9 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 3
Source File: URLEncodedUtils.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
/**
 * Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
 *
 * @param content     the portion to decode
 * @param charset     the charset to use
 * @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
 * @return encoded string
 */
private static String urldecode(
        final String content,
        final Charset charset,
        final boolean plusAsBlank) {
    if (content == null) {
        return null;
    }
    ByteBuffer bb = ByteBuffer.allocate(content.length());
    CharBuffer cb = CharBuffer.wrap(content);
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (c == '%' && cb.remaining() >= 2) {
            char uc = cb.get();
            char lc = cb.get();
            int u = Character.digit(uc, 16);
            int l = Character.digit(lc, 16);
            if (u != -1 && l != -1) {
                bb.put((byte) ((u << 4) + l));
            } else {
                bb.put((byte) '%');
                bb.put((byte) uc);
                bb.put((byte) lc);
            }
        } else if (plusAsBlank && c == '+') {
            bb.put((byte) ' ');
        } else {
            bb.put((byte) c);
        }
    }
    bb.flip();
    return charset.decode(bb).toString();
}
 
Example 4
Source File: ByteBufUtil.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset) {
    final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
    int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar());
    boolean release = true;
    final ByteBuf dst;
    if (enforceHeap) {
        dst = alloc.heapBuffer(length);
    } else {
        dst = alloc.buffer(length);
    }
    try {
        final ByteBuffer dstBuf = dst.internalNioBuffer(0, length);
        final int pos = dstBuf.position();
        CoderResult cr = encoder.encode(src, dstBuf, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dstBuf);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
        release = false;
        return dst;
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    } finally {
        if (release) {
            dst.release();
        }
    }
}
 
Example 5
Source File: ISCII91.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult implFlush(CharBuffer out) {
    if(needFlushing) {
        if (out.remaining() < 1) {
            return CoderResult.OVERFLOW;
        } else {
            out.put(contextChar);
        }
    }
    contextChar = INVALID_CHAR;
    needFlushing = false;
    return CoderResult.UNDERFLOW;
}
 
Example 6
Source File: HKSCS.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()) {
            char[] cc = null;
            int b1 = src.get() & 0xff;
            int inSize = 1, outSize = 1;
            char c = decodeSingle(b1);
            if (c == UNMAPPABLE_DECODING) {
                if (src.remaining() < 1)
                    return CoderResult.UNDERFLOW;
                int b2 = src.get() & 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 {
                        outSize = 2;
                    }
                }
            }
            if (dst.remaining() < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                dst.put(Surrogate.high(0x20000 + c));
                dst.put(Surrogate.low(0x20000 + c));
            } else {
                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 7
Source File: EUC_JP.java    From jdk8u60 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 8
Source File: COMPOUND_TEXT_Decoder.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult normalBytes(short newByte, CharBuffer cb) {
    CoderResult cr = CoderResult.UNDERFLOW;
    if ((newByte >= 0x00 && newByte <= 0x1F) || // C0
        (newByte >= 0x80 && newByte <= 0x9F)) { // C1
        char newChar;

        switch (newByte) {
        case 0x1B:
            state = ESCAPE_SEQUENCE;
            queue.write(newByte);
            return cr;
        case 0x9B:
            state = CONTROL_SEQUENCE_PIF;
            versionSequenceAllowed = false;
            queue.write(newByte);
            return cr;
        case 0x09:
            versionSequenceAllowed = false;
            newChar = '\t';
            break;
        case 0x0A:
            versionSequenceAllowed = false;
            newChar = '\n';
            break;
        default:
            versionSequenceAllowed = false;
            return cr;
        }
        if (!cb.hasRemaining())
            return CoderResult.OVERFLOW;
        else
            cb.put(newChar);
    } else {
        CharsetDecoder decoder;
        boolean high;
        versionSequenceAllowed = false;

        if (newByte >= 0x20 && newByte <= 0x7F) {
            decoder = glDecoder;
            high = glHigh;
        } else /* if (newByte >= 0xA0 && newByte <= 0xFF) */ {
            decoder = grDecoder;
            high = grHigh;
        }
        if (lastDecoder != null && decoder != lastDecoder) {
            cr = flushDecoder(lastDecoder, cb);
        }
        lastDecoder = decoder;

        if (decoder != null) {
            byte b = (byte)newByte;
            if (high) {
                b |= 0x80;
            } else {
                b &= 0x7F;
            }
            inBB.put(b);
            inBB.flip();
            cr = decoder.decode(inBB, cb, false);
            if (!inBB.hasRemaining() || cr.isMalformed()) {
                inBB.clear();
            } else {
              int pos = inBB.limit();
              inBB.clear();
              inBB.position(pos);
            }
        } else if (cb.remaining() < replacement().length()) {
            cb.put(replacement());
        } else {
            return CoderResult.OVERFLOW;
        }
    }
    return cr;
}
 
Example 9
Source File: UTF_32Coder.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (src.remaining() < 4)
        return CoderResult.UNDERFLOW;
    int mark = src.position();
    int cp;
    try {
        if (currentBO == NONE) {
            cp = ((src.get() & 0xff) << 24) |
                 ((src.get() & 0xff) << 16) |
                 ((src.get() & 0xff) <<  8) |
                 (src.get() & 0xff);
            if (cp == BOM_BIG && expectedBO != LITTLE) {
                currentBO = BIG;
                mark += 4;
            } else if (cp == BOM_LITTLE && expectedBO != BIG) {
                currentBO = LITTLE;
                mark += 4;
            } else {
                if (expectedBO == NONE)
                    currentBO = BIG;
                else
                    currentBO = expectedBO;
                src.position(mark);
            }
        }
        while (src.remaining() >= 4) {
            cp = getCP(src);
            if (Character.isBmpCodePoint(cp)) {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                mark += 4;
                dst.put((char) cp);
            } else if (Character.isValidCodePoint(cp)) {
                if (dst.remaining() < 2)
                    return CoderResult.OVERFLOW;
                mark += 4;
                dst.put(Character.highSurrogate(cp));
                dst.put(Character.lowSurrogate(cp));
            } else {
                return CoderResult.malformedForLength(4);
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 10
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 11
Source File: ISO2022.java    From jdk8u-dev-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 12
Source File: COMPOUND_TEXT_Decoder.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult normalBytes(short newByte, CharBuffer cb) {
    CoderResult cr = CoderResult.UNDERFLOW;
    if ((newByte >= 0x00 && newByte <= 0x1F) || // C0
        (newByte >= 0x80 && newByte <= 0x9F)) { // C1
        char newChar;

        switch (newByte) {
        case 0x1B:
            state = ESCAPE_SEQUENCE;
            queue.write(newByte);
            return cr;
        case 0x9B:
            state = CONTROL_SEQUENCE_PIF;
            versionSequenceAllowed = false;
            queue.write(newByte);
            return cr;
        case 0x09:
            versionSequenceAllowed = false;
            newChar = '\t';
            break;
        case 0x0A:
            versionSequenceAllowed = false;
            newChar = '\n';
            break;
        default:
            versionSequenceAllowed = false;
            return cr;
        }
        if (!cb.hasRemaining())
            return CoderResult.OVERFLOW;
        else
            cb.put(newChar);
    } else {
        CharsetDecoder decoder;
        boolean high;
        versionSequenceAllowed = false;

        if (newByte >= 0x20 && newByte <= 0x7F) {
            decoder = glDecoder;
            high = glHigh;
        } else /* if (newByte >= 0xA0 && newByte <= 0xFF) */ {
            decoder = grDecoder;
            high = grHigh;
        }
        if (lastDecoder != null && decoder != lastDecoder) {
            cr = flushDecoder(lastDecoder, cb);
        }
        lastDecoder = decoder;

        if (decoder != null) {
            byte b = (byte)newByte;
            if (high) {
                b |= 0x80;
            } else {
                b &= 0x7F;
            }
            inBB.put(b);
            inBB.flip();
            cr = decoder.decode(inBB, cb, false);
            if (!inBB.hasRemaining() || cr.isMalformed()) {
                inBB.clear();
            } else {
              int pos = inBB.limit();
              inBB.clear();
              inBB.position(pos);
            }
        } else if (cb.remaining() < replacement().length()) {
            cb.put(replacement());
        } else {
            return CoderResult.OVERFLOW;
        }
    }
    return cr;
}
 
Example 13
Source File: EUC_JP_OLD.java    From hottub 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 = REPLACE_CHAR; // U+FFFD;

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

            if ((b1 & 0x80) == 0) {
                outputChar = (char)b1;
            } else {    // Multibyte char
                if ((b1 & 0xff) == 0x8f) {   // JIS0212
                    if (src.remaining() < 2)
                       return CoderResult.UNDERFLOW;
                    b1 = src.get() & 0xff;
                    b2 = src.get() & 0xff;
                    inputSize += 2;
                    outputChar = decode0212(b1-0x80, b2-0x80);
                } else {
                  // JIS0208
                    if (src.remaining() < 1)
                       return CoderResult.UNDERFLOW;
                    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 14
Source File: UTF_32Coder.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    if (src.remaining() < 4)
        return CoderResult.UNDERFLOW;
    int mark = src.position();
    int cp;
    try {
        if (currentBO == NONE) {
            cp = ((src.get() & 0xff) << 24) |
                 ((src.get() & 0xff) << 16) |
                 ((src.get() & 0xff) <<  8) |
                 (src.get() & 0xff);
            if (cp == BOM_BIG && expectedBO != LITTLE) {
                currentBO = BIG;
                mark += 4;
            } else if (cp == BOM_LITTLE && expectedBO != BIG) {
                currentBO = LITTLE;
                mark += 4;
            } else {
                if (expectedBO == NONE)
                    currentBO = BIG;
                else
                    currentBO = expectedBO;
                src.position(mark);
            }
        }
        while (src.remaining() >= 4) {
            cp = getCP(src);
            if (Character.isBmpCodePoint(cp)) {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                mark += 4;
                dst.put((char) cp);
            } else if (Character.isValidCodePoint(cp)) {
                if (dst.remaining() < 2)
                    return CoderResult.OVERFLOW;
                mark += 4;
                dst.put(Character.highSurrogate(cp));
                dst.put(Character.lowSurrogate(cp));
            } else {
                return CoderResult.malformedForLength(4);
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 15
Source File: HKSCS.java    From dragonwell8_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()) {
            char[] cc = null;
            int b1 = src.get() & 0xff;
            int inSize = 1, outSize = 1;
            char c = decodeSingle(b1);
            if (c == UNMAPPABLE_DECODING) {
                if (src.remaining() < 1)
                    return CoderResult.UNDERFLOW;
                int b2 = src.get() & 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 {
                        outSize = 2;
                    }
                }
            }
            if (dst.remaining() < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                dst.put(Surrogate.high(0x20000 + c));
                dst.put(Surrogate.low(0x20000 + c));
            } else {
                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 16
Source File: HKSCS.java    From openjdk-8 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()) {
            char[] cc = null;
            int b1 = src.get() & 0xff;
            int inSize = 1, outSize = 1;
            char c = decodeSingle(b1);
            if (c == UNMAPPABLE_DECODING) {
                if (src.remaining() < 1)
                    return CoderResult.UNDERFLOW;
                int b2 = src.get() & 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 {
                        outSize = 2;
                    }
                }
            }
            if (dst.remaining() < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                dst.put(Surrogate.high(0x20000 + c));
                dst.put(Surrogate.low(0x20000 + c));
            } else {
                dst.put(c);
            }
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 17
Source File: COMPOUND_TEXT_Decoder.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult normalBytes(short newByte, CharBuffer cb) {
    CoderResult cr = CoderResult.UNDERFLOW;
    if ((newByte >= 0x00 && newByte <= 0x1F) || // C0
        (newByte >= 0x80 && newByte <= 0x9F)) { // C1
        char newChar;

        switch (newByte) {
        case 0x1B:
            state = ESCAPE_SEQUENCE;
            queue.write(newByte);
            return cr;
        case 0x9B:
            state = CONTROL_SEQUENCE_PIF;
            versionSequenceAllowed = false;
            queue.write(newByte);
            return cr;
        case 0x09:
            versionSequenceAllowed = false;
            newChar = '\t';
            break;
        case 0x0A:
            versionSequenceAllowed = false;
            newChar = '\n';
            break;
        default:
            versionSequenceAllowed = false;
            return cr;
        }
        if (!cb.hasRemaining())
            return CoderResult.OVERFLOW;
        else
            cb.put(newChar);
    } else {
        CharsetDecoder decoder;
        boolean high;
        versionSequenceAllowed = false;

        if (newByte >= 0x20 && newByte <= 0x7F) {
            decoder = glDecoder;
            high = glHigh;
        } else /* if (newByte >= 0xA0 && newByte <= 0xFF) */ {
            decoder = grDecoder;
            high = grHigh;
        }
        if (lastDecoder != null && decoder != lastDecoder) {
            cr = flushDecoder(lastDecoder, cb);
        }
        lastDecoder = decoder;

        if (decoder != null) {
            byte b = (byte)newByte;
            if (high) {
                b |= 0x80;
            } else {
                b &= 0x7F;
            }
            inBB.put(b);
            inBB.flip();
            cr = decoder.decode(inBB, cb, false);
            if (!inBB.hasRemaining() || cr.isMalformed()) {
                inBB.clear();
            } else {
              int pos = inBB.limit();
              inBB.clear();
              inBB.position(pos);
            }
        } else if (cb.remaining() < replacement().length()) {
            cb.put(replacement());
        } else {
            return CoderResult.OVERFLOW;
        }
    }
    return cr;
}
 
Example 18
Source File: StaEDIStreamReader.java    From staedi with Apache License 2.0 4 votes vote down vote up
@Override
public int getTextCharacters(int sourceStart,
                             char[] target,
                             int targetStart,
                             int length) {

    ensureOpen();
    checkTextState();

    if (target == null) {
        throw new NullPointerException("Null target array");
    }
    if (targetStart < 0) {
        throw new IndexOutOfBoundsException("targetStart < 0");
    }
    if (targetStart > target.length) {
        throw new IndexOutOfBoundsException("targetStart > target.length");
    }
    if (length < 0) {
        throw new IndexOutOfBoundsException("length < 0");
    }
    if (length > target.length) {
        throw new IndexOutOfBoundsException("length (" + length + ") > target.length (" + target.length + ")");
    }

    final CharBuffer buffer = getBuffer();
    final char[] contents = buffer.array();
    final int count = buffer.remaining();

    if (sourceStart < 0) {
        throw new IndexOutOfBoundsException("sourceStart < 0");
    }

    if (sourceStart > count) {
        throw new IndexOutOfBoundsException("sourceStart > source length");
    }

    int toCopy = Math.min(count - sourceStart, length);
    System.arraycopy(contents, sourceStart, target, targetStart, toCopy);

    return toCopy;
}
 
Example 19
Source File: DoubleByte.java    From jdk8u60 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);
    }
}
 
Example 20
Source File: CharsetEncoder.java    From Java8CN with Apache License 2.0 3 votes vote down vote up
/**
 * Convenience method that encodes the remaining content of a single input
 * character buffer into a newly-allocated byte buffer.
 *
 * <p> This method implements an entire <a href="#steps">encoding
 * operation</a>; that is, it resets this encoder, then it encodes the
 * characters in the given character buffer, and finally it flushes this
 * encoder.  This method should therefore not be invoked if an encoding
 * operation is already in progress.  </p>
 *
 * @param  in
 *         The input character buffer
 *
 * @return A newly-allocated byte buffer containing the result of the
 *         encoding operation.  The buffer's position will be zero and its
 *         limit will follow the last byte written.
 *
 * @throws  IllegalStateException
 *          If an encoding operation is already in progress
 *
 * @throws  MalformedInputException
 *          If the character sequence starting at the input buffer's current
 *          position is not a legal sixteen-bit Unicode sequence and the current malformed-input action
 *          is {@link CodingErrorAction#REPORT}
 *
 * @throws  UnmappableCharacterException
 *          If the character sequence starting at the input buffer's current
 *          position cannot be mapped to an equivalent byte sequence and
 *          the current unmappable-character action is {@link
 *          CodingErrorAction#REPORT}
 */
public final ByteBuffer encode(CharBuffer in)
    throws CharacterCodingException
{
    int n = (int)(in.remaining() * averageBytesPerChar());
    ByteBuffer out = ByteBuffer.allocate(n);

    if ((n == 0) && (in.remaining() == 0))
        return out;
    reset();
    for (;;) {
        CoderResult cr = in.hasRemaining() ?
            encode(in, out, true) : CoderResult.UNDERFLOW;
        if (cr.isUnderflow())
            cr = flush(out);

        if (cr.isUnderflow())
            break;
        if (cr.isOverflow()) {
            n = 2*n + 1;    // Ensure progress; n might be 0!
            ByteBuffer o = ByteBuffer.allocate(n);
            out.flip();
            o.put(out);
            out = o;
            continue;
        }
        cr.throwException();
    }
    out.flip();
    return out;
}