Search in sources :

Example 1 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 2 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 3 with CoderResult

use of java.nio.charset.CoderResult in project jmonkeyengine by jMonkeyEngine.

the class HttpZipLocator method getUTF8String.

private static String getUTF8String(byte[] b, int off, int len) throws CharacterCodingException {
    StringBuilder sb = new StringBuilder();
    int read = 0;
    while (read < len) {
        // Either read n remaining bytes in b or 250 if n is higher.
        int toRead = Math.min(len - read, byteBuf.capacity());
        boolean endOfInput = toRead < byteBuf.capacity();
        // read 'toRead' bytes into byteBuf
        byteBuf.put(b, off + read, toRead);
        // set limit to position and set position to 0
        // so data can be decoded
        byteBuf.flip();
        // decode data in byteBuf
        CoderResult result = utf8Decoder.decode(byteBuf, charBuf, endOfInput);
        // then the decoder expects more bytes but there are no more => error
        if (!result.isUnderflow() || !endOfInput) {
            result.throwException();
        }
        // flip the char buf to get the string just decoded
        charBuf.flip();
        // append the decoded data into the StringBuilder
        sb.append(charBuf.toString());
        // clear buffers for next use
        byteBuf.clear();
        charBuf.clear();
        read += toRead;
    }
    return sb.toString();
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 4 with CoderResult

use of java.nio.charset.CoderResult in project netty by netty.

the class ByteBufUtil method decodeString.

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);
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 5 with CoderResult

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

the class FastPrintWriter method flushLocked.

private void flushLocked() 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 (!mIoError) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytesLocked();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            if (!mIoError) {
                flushBytesLocked();
                mOutputStream.flush();
            }
        } else if (mWriter != null) {
            if (!mIoError) {
                mWriter.write(mText, 0, mPos);
                mWriter.flush();
            }
        } else {
            int nonEolOff = 0;
            final int sepLen = mSeparator.length();
            final int len = sepLen < mPos ? sepLen : mPos;
            while (nonEolOff < len && mText[mPos - 1 - nonEolOff] == mSeparator.charAt(mSeparator.length() - 1 - nonEolOff)) {
                nonEolOff++;
            }
            if (nonEolOff >= mPos) {
                mPrinter.println("");
            } else {
                mPrinter.println(new String(mText, 0, mPos - nonEolOff));
            }
        }
        mPos = 0;
    }
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) CoderResult(java.nio.charset.CoderResult)

Aggregations

CoderResult (java.nio.charset.CoderResult)210 CharBuffer (java.nio.CharBuffer)138 ByteBuffer (java.nio.ByteBuffer)85 CharsetDecoder (java.nio.charset.CharsetDecoder)45 IOException (java.io.IOException)36 CharsetEncoder (java.nio.charset.CharsetEncoder)31 CharacterCodingException (java.nio.charset.CharacterCodingException)30 Charset (java.nio.charset.Charset)18 ArrayDecoder (sun.nio.cs.ArrayDecoder)7 ArrayEncoder (sun.nio.cs.ArrayEncoder)7 JSONException (com.alibaba.fastjson.JSONException)3 BufferUnderflowException (java.nio.BufferUnderflowException)3 CloseReason (jakarta.websocket.CloseReason)2 UncheckedIOException (java.io.UncheckedIOException)2 BufferOverflowException (java.nio.BufferOverflowException)2 FileChannel (java.nio.channels.FileChannel)2 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 UnmappableCharacterException (java.nio.charset.UnmappableCharacterException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 CloseReason (javax.websocket.CloseReason)2