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 File: XmlFileEncodingQueryImpl.java    From netbeans with 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 #2
Source File: HtmlDataObject.java    From netbeans with 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 #3
Source File: ZipCoder.java    From jdk8u60 with 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 #4
Source File: UTF7StyleCharsetDecoder.java    From uiautomator-unicode-input-helper with 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 #5
Source File: US_ASCII.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 < 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 #6
Source File: UTF_8.java    From jdk8u60 with 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 #7
Source File: CharsetEncoderTest.java    From j2objc with 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 #8
Source File: CESU_8.java    From openjdk-8-source with 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 #9
Source File: CoderResultTest.java    From j2objc with 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 #10
Source File: ISO2022_CN.java    From openjdk-jdk8u-backup with 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 #11
Source File: ISO2022_JP.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult implFlush(ByteBuffer out) {
    if (currentMode != ASCII) {
        if (out.remaining() < 3)
            return CoderResult.OVERFLOW;
        out.put((byte)0x1b);
        out.put((byte)0x28);
        out.put((byte)0x42);
        currentMode = ASCII;
    }
    return CoderResult.UNDERFLOW;
}
 
Example #12
Source File: UTF_8.java    From TencentKona-8 with 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 #13
Source File: DDMWriter.java    From gemfirexd-oss with 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 #14
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 #15
Source File: EUC_TW.java    From dragonwell8_jdk with 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 File: StringCoding.java    From jdk-1.7-annotated with Apache License 2.0 5 votes vote down vote up
static byte[] encode(Charset cs, char[] ca, int off, int len) {
    CharsetEncoder ce = cs.newEncoder();
    int en = scale(len, ce.maxBytesPerChar());
    byte[] ba = new byte[en];
    if (len == 0)
        return ba;
    boolean isTrusted = false;
    if (System.getSecurityManager() != null) {
        if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
            ca =  Arrays.copyOfRange(ca, off, off + len);
            off = 0;
        }
    }
    ce.onMalformedInput(CodingErrorAction.REPLACE)
      .onUnmappableCharacter(CodingErrorAction.REPLACE)
      .reset();
    if (ce instanceof ArrayEncoder) {
        int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
        return safeTrim(ba, blen, cs, isTrusted);
    } else {
        ByteBuffer bb = ByteBuffer.wrap(ba);
        CharBuffer cb = CharBuffer.wrap(ca, off, len);
        try {
            CoderResult cr = ce.encode(cb, bb, true);
            if (!cr.isUnderflow())
                cr.throwException();
            cr = ce.flush(bb);
            if (!cr.isUnderflow())
                cr.throwException();
        } catch (CharacterCodingException x) {
            throw new Error(x);
        }
        return safeTrim(ba, bb.position(), cs, isTrusted);
    }
}
 
Example #17
Source File: ByteBufUtil.java    From netty-4.1.22 with 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 #18
Source File: StringCoding.java    From openjdk-jdk8u with 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 #19
Source File: ImapRequestLineReader.java    From james-project with 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 #20
Source File: ISO_8859_1.java    From openjdk-8 with 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 #21
Source File: ISO2022_JP.java    From jdk8u-jdk with 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 #22
Source File: CESU_8.java    From Bytecoder with 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 #23
Source File: PerforceShiftJISCharset.java    From p4ic4idea with 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 #24
Source File: SingleByte.java    From jdk8u-jdk with 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 #25
Source File: US_ASCII.java    From Bytecoder with 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 #26
Source File: EUC_JP.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #27
Source File: EUC_JP_LINUX_OLD.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #28
Source File: ISO2022_CN.java    From jdk8u-dev-jdk with 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 #29
Source File: EUC_JP_LINUX_OLD.java    From jdk8u_jdk with 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 #30
Source File: UTF_8.java    From openjdk-8-source with 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);
}