java.nio.charset.CoderResult Java Examples

The following examples show how to use java.nio.charset.CoderResult. 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 Project: netbeans   Author: apache   File: HtmlDataObject.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #2
Source Project: netbeans   Author: apache   File: XmlFileEncodingQueryImpl.java    License: Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source Project: uiautomator-unicode-input-helper   Author: sumio   File: UTF7StyleCharsetDecoder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <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;
}
 
Example #4
Source Project: jdk8u60   Author: chenghanpeng   File: ZipCoder.java    License: GNU General Public License v2.0 6 votes vote down vote up
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());
}
 
Example #5
Source Project: openjdk-8-source   Author: keerath   File: EUC_JP.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #6
Source Project: TencentKona-8   Author: Tencent   File: UTF_8.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #7
Source Project: gemfirexd-oss   Author: gemxd   File: DDMWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * 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());
	}
 
Example #8
Source Project: TencentKona-8   Author: Tencent   File: SingleByteDecoder.java    License: 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 Project: jdk8u-jdk   Author: frohoff   File: DBCS_IBM_EBCDIC_Encoder.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult implFlush(ByteBuffer out) {
    if (currentState == DBCS) {
        if (out.remaining() < 1)
            return CoderResult.OVERFLOW;
        out.put(SI);
    }
    implReset();
    return CoderResult.UNDERFLOW;
}
 
Example #10
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: US_ASCII.java    License: 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 #11
Source Project: james-project   Author: apache   File: ImapRequestLineReader.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #12
Source Project: Bytecoder   Author: mirkosertic   File: CESU_8.java    License: Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example #13
Source Project: p4ic4idea   Author: groboclown   File: PerforceShiftJISCharset.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source Project: jdk8u-jdk   Author: frohoff   File: SingleByte.java    License: GNU General Public License v2.0 5 votes vote down vote up
private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();

    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);
}
 
Example #15
Source Project: openjdk-8-source   Author: keerath   File: EUC_JP_LINUX_OLD.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #16
Source Project: jdk8u-dev-jdk   Author: frohoff   File: ISO2022_CN.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example #17
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: StringCoding.java    License: GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #18
Source Project: jdk8u_jdk   Author: JetBrains   File: SingleByte.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #19
Source Project: jdk8u-jdk   Author: frohoff   File: US_ASCII.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #20
Source Project: jdk8u60   Author: chenghanpeng   File: UTF_8.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #21
Source Project: netty-4.1.22   Author: tianheframe   File: ByteBufUtil.java    License: Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example #22
Source Project: openjdk-8-source   Author: keerath   File: UTF_8.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example #23
Source Project: jdk8u_jdk   Author: JetBrains   File: EUC_JP_LINUX_OLD.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #24
Source Project: Bytecoder   Author: mirkosertic   File: US_ASCII.java    License: Apache License 2.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 #25
Source Project: jdk8u-jdk   Author: frohoff   File: ISO2022_JP.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #26
Source Project: openjdk-8   Author: bpupadhyaya   File: ISO_8859_1.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #27
Source Project: j2objc   Author: google   File: CharsetEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #28
Source Project: openjdk-8-source   Author: keerath   File: CESU_8.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example #29
Source Project: j2objc   Author: google   File: CoderResultTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test method isError().
 * 
 */
public void testIsError() {
	assertFalse(CoderResult.UNDERFLOW.isError());
	assertFalse(CoderResult.OVERFLOW.isError());
	assertTrue(CoderResult.malformedForLength(1).isError());
	assertTrue(CoderResult.unmappableForLength(1).isError());
}
 
Example #30
Source Project: JDKSourceCode1.8   Author: wupeixuan   File: StringCoding.java    License: MIT License 5 votes vote down vote up
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);
    }
}