类java.nio.charset.CoderResult源码实例Demo

下面列出了怎么用java.nio.charset.CoderResult的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: netbeans   文件: HtmlDataObject.java

@Override
protected CoderResult implFlush(ByteBuffer out) {
    CoderResult res;
    if (buffer != null) {
        res = handleHead(null, out);
        return res;
    }
    else if (remainder != null) {
        encoder.encode(remainder, out, true);
    }
    else {
        CharBuffer empty = (CharBuffer) CharBuffer.allocate(0).flip();
        encoder.encode(empty, out, true);
    }
    res = encoder.flush(out);
    return res;
}
 

@Override
protected CoderResult implFlush(CharBuffer out) {
    CoderResult res;
    if (buffer != null) {
        res = handleHead(null, out);
        return res;
    }
    else if (remainder != null) {
        decoder.decode(remainder, out, true);
    }
    else {
        ByteBuffer empty = (ByteBuffer) ByteBuffer.allocate(0).flip();
        decoder.decode(empty, out, true);
    }
    res = decoder.flush(out);
    return res;
}
 

/**
 * <p>Decodes a byte in <i>base 64 mode</i>. Will directly write a character to the output 
 * buffer if completed.</p>
 * 
 * @param in The input buffer
 * @param out The output buffer
 * @param lastRead Last byte read from the input buffer
 * @return CoderResult.malformed if a non-base 64 character was encountered in strict 
 *   mode, null otherwise
 */
private CoderResult handleBase64(ByteBuffer in, CharBuffer out, byte lastRead) {
	CoderResult result = null;
	int sextet = base64.getSextet(lastRead);
	if (sextet >= 0) {
		bitsRead += 6;
		if (bitsRead < 16) {
			tempChar += sextet << (16 - bitsRead);
		} else {
			bitsRead -= 16;
			tempChar += sextet >> (bitsRead);
			out.put((char) tempChar);
			tempChar = (sextet << (16 - bitsRead)) & 0xFFFF;
		}
	} else {
		if (strict)
			return malformed(in);
		out.put((char) lastRead);
		if (base64bitsWaiting())
			result = malformed(in);
		setUnshifted();
	}
	return result;
}
 
源代码4 项目: jdk8u60   文件: ZipCoder.java

String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode. ZipCoder uses
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
        if (clen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
源代码5 项目: openjdk-8-source   文件: EUC_JP.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码6 项目: TencentKona-8   文件: UTF_8.java

private static CoderResult malformedN(ByteBuffer src, int nb) {
    switch (nb) {
    case 1:
    case 2:                    // always 1
        return CoderResult.malformedForLength(1);
    case 3:
        int b1 = src.get();
        int b2 = src.get();    // no need to lookup b3
        return CoderResult.malformedForLength(
            ((b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
             isNotContinuation(b2)) ? 1 : 2);
    case 4:  // we don't care the speed here
        b1 = src.get() & 0xff;
        b2 = src.get() & 0xff;
        if (b1 > 0xf4 ||
            (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) ||
            (b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
            isNotContinuation(b2))
            return CoderResult.malformedForLength(1);
        if (isNotContinuation(src.get()))
            return CoderResult.malformedForLength(2);
        return CoderResult.malformedForLength(3);
    default:
        assert false;
        return null;
    }
}
 
源代码7 项目: gemfirexd-oss   文件: DDMWriter.java

/**
	 * Write length delimited string
	 *
	 * @param s              value to be written with integer
	 * @param index          column index to put in warning
	 * @exception DRDAProtocolException
	 */
	protected void writeLDString(String s, int index) throws DRDAProtocolException
	{
		// Position on which to write the length of the string (in bytes). The
		// actual writing of the length is delayed until we have encoded the
		// string.
		final int lengthPos = buffer.position();
		// Position on which to start writing the string (right after length,
		// which is 2 bytes long).
		final int stringPos = lengthPos + 2;
		// don't send more than LONGVARCHAR_MAX_LEN bytes
		final int maxStrLen =
			Math.min(maxEncodedLength(s), FdocaConstants.LONGVARCHAR_MAX_LEN);

		ensureLength(2 + maxStrLen);

		// limit the writable area of the output buffer
		buffer.position(stringPos);
		buffer.limit(stringPos + maxStrLen);

		// encode the string
		CharBuffer input = CharBuffer.wrap(s);
		CoderResult res = encoder.encode(input, buffer, true);
		if (SanityManager.DEBUG) {
			// UNDERFLOW is returned if the entire string was encoded, OVERFLOW
			// is returned if the string was truncated at LONGVARCHAR_MAX_LEN
// GemStone changes BEGIN
		  if (res != CoderResult.UNDERFLOW && res != CoderResult.OVERFLOW)
// GemStone changes END
			SanityManager.ASSERT(
				res == CoderResult.UNDERFLOW || res == CoderResult.OVERFLOW,
				"Unexpected coder result: " + res);
		}

		// write the length in bytes
		buffer.putShort(lengthPos, (short) (maxStrLen - buffer.remaining()));

		// remove the limit on the output buffer
		buffer.limit(buffer.capacity());
	}
 
源代码8 项目: TencentKona-8   文件: SingleByteDecoder.java

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());
    }
}
 

protected CoderResult implFlush(ByteBuffer out) {
    if (currentState == DBCS) {
        if (out.remaining() < 1)
            return CoderResult.OVERFLOW;
        out.put(SI);
    }
    implReset();
    return CoderResult.UNDERFLOW;
}
 
源代码10 项目: openjdk-jdk8u   文件: US_ASCII.java

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());
    }
}
 

private CoderResult decodeMoreBytesToCharacterBuffer(boolean endOfInput) throws DecodingException {
    final CoderResult coderResult = decoder.decode(buffer, charBuffer, endOfInput);
    if (coderResult.isOverflow()) {
        upsizeCharBuffer();
        return decodeMoreBytesToCharacterBuffer(endOfInput);
    } else if (coderResult.isError()) {
        throw new DecodingException(HumanReadableText.BAD_IO_ENCODING, "Bad character encoding");
    } else if (coderResult.isUnderflow()) {
        buffer.clear();
    }
    return coderResult;
}
 
源代码12 项目: Bytecoder   文件: CESU_8.java

private static CoderResult malformedN(ByteBuffer src, int nb) {
    switch (nb) {
    case 1:
    case 2:                    // always 1
        return CoderResult.malformedForLength(1);
    case 3:
        int b1 = src.get();
        int b2 = src.get();    // no need to lookup b3
        return CoderResult.malformedForLength(
            ((b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) ||
             isNotContinuation(b2)) ? 1 : 2);
    case 4:  // we don't care the speed here
        b1 = src.get() & 0xff;
        b2 = src.get() & 0xff;
        if (b1 > 0xf4 ||
            (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) ||
            (b1 == 0xf4 && (b2 & 0xf0) != 0x80) ||
            isNotContinuation(b2))
            return CoderResult.malformedForLength(1);
        if (isNotContinuation(src.get()))
            return CoderResult.malformedForLength(2);
        return CoderResult.malformedForLength(3);
    default:
        assert false;
        return null;
    }
}
 
源代码13 项目: p4ic4idea   文件: PerforceShiftJISCharset.java

/**
 * Implementation of the encoding loop. Apply Perforce specific updates,
 * then reset the encoder and encode the characters to bytes.
 */
protected CoderResult encodeLoop(CharBuffer cb, ByteBuffer bb) {
	CharBuffer tmpcb = CharBuffer.allocate(cb.remaining());
	while (cb.hasRemaining()) {
		tmpcb.put(cb.get());
	}
	tmpcb.rewind();
	update(tmpcb);
	encoder.reset();
	CoderResult cr = encoder.encode(tmpcb, bb, true);
	cb.position(cb.position() - tmpcb.remaining());
	return (cr);
}
 
源代码14 项目: jdk8u-jdk   文件: SingleByte.java

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();

    CoderResult cr = CoderResult.UNDERFLOW;
    if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
    }

    while (sp < sl) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
            if (Character.isSurrogate(c)) {
                if (sgp == null)
                    sgp = new Surrogate.Parser();
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return withResult(sgp.error(), src, sp, dst, dp);
                return withResult(sgp.unmappableResult(), src, sp, dst, dp);
            }
            return withResult(CoderResult.unmappableForLength(1),
                       src, sp, dst, dp);
        }
        da[dp++] = (byte)b;
        sp++;
    }
    return withResult(cr, src, sp, dst, dp);
}
 
源代码15 项目: openjdk-8-source   文件: EUC_JP_LINUX_OLD.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码16 项目: jdk8u-dev-jdk   文件: ISO2022_CN.java

protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
源代码17 项目: openjdk-jdk8u   文件: StringCoding.java

char[] decode(byte[] ba, int off, int len) {
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        cd.reset();
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // Substitution is always enabled,
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}
 
源代码18 项目: jdk8u_jdk   文件: SingleByte.java

private static final CoderResult withResult(CoderResult cr,
                                            Buffer src, int sp,
                                            Buffer dst, int dp)
{
    src.position(sp - src.arrayOffset());
    dst.position(dp - dst.arrayOffset());
    return cr;
}
 
源代码19 项目: jdk8u-jdk   文件: US_ASCII.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码20 项目: jdk8u60   文件: UTF_8.java

private static CoderResult malformed(ByteBuffer src, int sp,
                                     CharBuffer dst, int dp,
                                     int nb)
{
    src.position(sp - src.arrayOffset());
    CoderResult cr = malformedN(src, nb);
    updatePositions(src, sp, dst, dp);
    return cr;
}
 
源代码21 项目: netty-4.1.22   文件: ByteBufUtil.java

private static void decodeString(CharsetDecoder decoder, ByteBuffer src, CharBuffer dst) {
    try {
        CoderResult cr = decoder.decode(src, dst, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = decoder.flush(dst);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    }
}
 
源代码22 项目: openjdk-8-source   文件: UTF_8.java

protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
源代码23 项目: jdk8u_jdk   文件: EUC_JP_LINUX_OLD.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码24 项目: Bytecoder   文件: US_ASCII.java

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());
    }
}
 
源代码25 项目: jdk8u-jdk   文件: ISO2022_JP.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码26 项目: openjdk-8   文件: ISO_8859_1.java

protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
源代码27 项目: j2objc   文件: CharsetEncoderTest.java

public void testMalformedSurrogatePair() throws Exception {
    // malformed: low surrogate first is detected as an error.
    Charset cs = Charset.forName("UTF-32BE");
    CharsetEncoder e = cs.newEncoder();
    ByteBuffer bb = ByteBuffer.allocate(128);
    CoderResult cr = e.encode(CharBuffer.wrap(new char[] { '\udf9f' }), bb, false);
    assertTrue(cr.toString(), cr.isMalformed());
    assertEquals(1, cr.length());
}
 
源代码28 项目: openjdk-8-source   文件: CESU_8.java

private static CoderResult malformed(ByteBuffer src, int sp,
                                     CharBuffer dst, int dp,
                                     int nb)
{
    src.position(sp - src.arrayOffset());
    CoderResult cr = malformedN(src, nb);
    updatePositions(src, sp, dst, dp);
    return cr;
}
 
源代码29 项目: j2objc   文件: CoderResultTest.java

/**
 * Test method isError().
 * 
 */
public void testIsError() {
	assertFalse(CoderResult.UNDERFLOW.isError());
	assertFalse(CoderResult.OVERFLOW.isError());
	assertTrue(CoderResult.malformedForLength(1).isError());
	assertTrue(CoderResult.unmappableForLength(1).isError());
}
 
源代码30 项目: JDKSourceCode1.8   文件: StringCoding.java

char[] decode(byte[] ba, int off, int len) {
    int en = scale(len, cd.maxCharsPerByte());
    char[] ca = new char[en];
    if (len == 0)
        return ca;
    if (cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
        return safeTrim(ca, clen, cs, isTrusted);
    } else {
        cd.reset();
        ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
        CharBuffer cb = CharBuffer.wrap(ca);
        try {
            CoderResult cr = cd.decode(bb, cb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = cd.flush(cb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            // Substitution is always enabled,
            // so this shouldn't happen
            throw new Error(x);
        }
        return safeTrim(ca, cb.position(), cs, isTrusted);
    }
}