Search in sources :

Example 6 with CoderResult

use of java.nio.charset.CoderResult in project tomcat by apache.

the class TestUtf8 method doTest.

private void doTest(CharsetDecoder decoder, Utf8TestCase testCase, int flags) {
    int len = testCase.input.length;
    ByteBuffer bb = ByteBuffer.allocate(len);
    CharBuffer cb = CharBuffer.allocate(len);
    // Configure decoder to fail on an error
    decoder.reset();
    decoder.onMalformedInput(CodingErrorAction.REPORT);
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    // an invalid sequence has been provided
    for (int i = 0; i < len; i++) {
        bb.put((byte) testCase.input[i]);
        bb.flip();
        CoderResult cr = decoder.decode(bb, cb, false);
        if (cr.isError()) {
            int expected = testCase.invalidIndex;
            if ((flags & ERROR_POS_PLUS1) != 0) {
                expected += 1;
            }
            if ((flags & ERROR_POS_PLUS2) != 0) {
                expected += 2;
            }
            if ((flags & ERROR_POS_PLUS4) != 0) {
                expected += 4;
            }
            Assert.assertEquals(testCase.description, expected, i);
            break;
        }
        bb.compact();
    }
    // Configure decoder to replace on an error
    decoder.reset();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    // Add each byte one at a time.
    bb.clear();
    cb.clear();
    for (int i = 0; i < len; i++) {
        bb.put((byte) testCase.input[i]);
        bb.flip();
        CoderResult cr = decoder.decode(bb, cb, false);
        if (cr.isError()) {
            Assert.fail(testCase.description);
        }
        bb.compact();
    }
    // For incomplete sequences at the end of the input need to tell
    // the decoder the input has ended
    bb.flip();
    CoderResult cr = decoder.decode(bb, cb, true);
    if (cr.isError()) {
        Assert.fail(testCase.description);
    }
    cb.flip();
    String expected = testCase.outputReplaced;
    if ((flags & REPLACE_SWALLOWS_TRAILER) != 0) {
        expected = expected.substring(0, expected.length() - 1);
    }
    if ((flags & REPLACE_MISSING1) != 0) {
        expected = expected.substring(0, 1) + expected.substring(2, expected.length());
    }
    if ((flags & REPLACE_MISSING2) != 0) {
        expected = expected.substring(0, 1) + expected.substring(3, expected.length());
    }
    if ((flags & REPLACE_MISSING4) != 0) {
        expected = expected.substring(0, 1) + expected.substring(5, expected.length());
    }
    Assert.assertEquals(testCase.description, expected, cb.toString());
}
Also used : CharBuffer(java.nio.CharBuffer) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 7 with CoderResult

use of java.nio.charset.CoderResult in project buck by facebook.

the class NulTerminatedCharsetDecoder method decodeChunk.

private Result decodeChunk(ByteBuffer in, int nulOffset, CharBuffer out, boolean endOfInput) {
    Result result;
    if (nulOffset == in.limit()) {
        // We didn't find a NUL terminator. Decode what we can, but tell
        // the caller we need to keep going.
        CoderResult decoderResult = decoder.decode(in, out, endOfInput);
        result = new Result(false, decoderResult);
    } else {
        // We found a NUL terminator, but we don't know if out has enough capacity
        // to hold the values up to that point.
        //
        // Temporarily limit the buffer to exclude the NUL we found,
        // decode as much as we can, and check if we made it to the NUL.
        int oldLimit = in.limit();
        in.limit(nulOffset);
        CoderResult decoderResult = decoder.decode(in, out, true);
        boolean nulTerminatorReached = !in.hasRemaining();
        result = new Result(nulTerminatorReached, decoderResult);
        in.limit(oldLimit);
        if (nulTerminatorReached) {
            // We consumed the entire buffer, so move past the NUL terminator.
            in.position(nulOffset + 1);
        }
    }
    return result;
}
Also used : CoderResult(java.nio.charset.CoderResult) CoderResult(java.nio.charset.CoderResult)

Example 8 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by ParanoidAndroid.

the class LoggingPrintStream method write.

@Override
public synchronized void write(byte[] bytes, int start, int count) {
    if (decoder == null) {
        encodedBytes = ByteBuffer.allocate(80);
        decodedChars = CharBuffer.allocate(80);
        decoder = Charset.defaultCharset().newDecoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    }
    int end = start + count;
    while (start < end) {
        // copy some bytes from the array to the long-lived buffer. This
        // way, if we end with a partial character we don't lose it.
        int numBytes = Math.min(encodedBytes.remaining(), end - start);
        encodedBytes.put(bytes, start, numBytes);
        start += numBytes;
        encodedBytes.flip();
        CoderResult coderResult;
        do {
            // decode bytes from the byte buffer into the char buffer
            coderResult = decoder.decode(encodedBytes, decodedChars, false);
            // copy chars from the char buffer into our string builder
            decodedChars.flip();
            builder.append(decodedChars);
            decodedChars.clear();
        } while (coderResult.isOverflow());
        encodedBytes.compact();
    }
    flush(false);
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 9 with CoderResult

use of java.nio.charset.CoderResult in project android_frameworks_base by ParanoidAndroid.

the class FastXmlSerializer method flush.

public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Example 10 with CoderResult

use of java.nio.charset.CoderResult in project fastjson by alibaba.

the class IOUtils method decode.

public static void decode(CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
        CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = charsetDecoder.flush(charByte);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
    } catch (CharacterCodingException x) {
        // so this shouldn't happen
        throw new JSONException("utf8 decode error, " + x.getMessage(), x);
    }
}
Also used : JSONException(com.alibaba.fastjson.JSONException) CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)123 CharBuffer (java.nio.CharBuffer)81 ByteBuffer (java.nio.ByteBuffer)35 IOException (java.io.IOException)25 CharsetDecoder (java.nio.charset.CharsetDecoder)25 Charset (java.nio.charset.Charset)15 CharacterCodingException (java.nio.charset.CharacterCodingException)13 CharsetEncoder (java.nio.charset.CharsetEncoder)13 BufferUnderflowException (java.nio.BufferUnderflowException)3 BufferOverflowException (java.nio.BufferOverflowException)2 CloseReason (javax.websocket.CloseReason)2 ArrayDecoder (sun.nio.cs.ArrayDecoder)2 ArrayEncoder (sun.nio.cs.ArrayEncoder)2 JSONException (com.alibaba.fastjson.JSONException)1 SimpleDiagnosticPosition (com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition)1 ParseException (io.druid.java.util.common.parsers.ParseException)1 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Cipher (javax.crypto.Cipher)1