Search in sources :

Example 41 with CoderResult

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

the class StrictJarManifest method writeEntry.

private static void writeEntry(OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException {
    String nameString = name.toString();
    os.write(nameString.getBytes(StandardCharsets.US_ASCII));
    os.write(VALUE_SEPARATOR);
    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2);
    CharBuffer cBuf = CharBuffer.wrap(value);
    while (true) {
        CoderResult r = encoder.encode(cBuf, bBuf, true);
        if (CoderResult.UNDERFLOW == r) {
            r = encoder.flush(bBuf);
        }
        os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
        os.write(LINE_SEPARATOR);
        if (CoderResult.UNDERFLOW == r) {
            break;
        }
        os.write(' ');
        bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
}
Also used : CharBuffer(java.nio.CharBuffer) CoderResult(java.nio.charset.CoderResult)

Example 42 with CoderResult

use of java.nio.charset.CoderResult in project voltdb by VoltDB.

the class ByteBufUtil method encodeString0.

static ByteBuf encodeString0(ByteBufAllocator alloc, boolean enforceHeap, CharBuffer src, Charset charset) {
    final CharsetEncoder encoder = CharsetUtil.encoder(charset);
    int length = (int) ((double) src.remaining() * encoder.maxBytesPerChar());
    boolean release = true;
    final ByteBuf dst;
    if (enforceHeap) {
        dst = alloc.heapBuffer(length);
    } else {
        dst = alloc.buffer(length);
    }
    try {
        final ByteBuffer dstBuf = dst.internalNioBuffer(0, length);
        final int pos = dstBuf.position();
        CoderResult cr = encoder.encode(src, dstBuf, true);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        cr = encoder.flush(dstBuf);
        if (!cr.isUnderflow()) {
            cr.throwException();
        }
        dst.writerIndex(dst.writerIndex() + dstBuf.position() - pos);
        release = false;
        return dst;
    } catch (CharacterCodingException x) {
        throw new IllegalStateException(x);
    } finally {
        if (release) {
            dst.release();
        }
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CharsetEncoder(java.nio.charset.CharsetEncoder) ByteBuffer(java.nio.ByteBuffer) CoderResult(java.nio.charset.CoderResult)

Example 43 with CoderResult

use of java.nio.charset.CoderResult in project async-http-client by AsyncHttpClient.

the class Utf8ByteBufCharsetDecoder method handleSplitCharBuffer.

private void handleSplitCharBuffer(ByteBuffer nioBuffer, boolean endOfInput) throws CharacterCodingException {
    // TODO we could save charSize
    int missingBytes = charSize(splitCharBuffer.get(0)) - splitCharBuffer.position();
    if (nioBuffer.remaining() < missingBytes) {
        if (endOfInput) {
            throw new CharacterCodingException();
        }
        // still not enough bytes
        splitCharBuffer.put(nioBuffer);
    } else {
        // FIXME better way?
        for (int i = 0; i < missingBytes; i++) {
            splitCharBuffer.put(nioBuffer.get());
        }
        splitCharBuffer.flip();
        CoderResult res = decoder.decode(splitCharBuffer, charBuffer, endOfInput && !nioBuffer.hasRemaining());
        if (res.isError()) {
            res.throwException();
        }
        splitCharBuffer.clear();
    }
}
Also used : CharacterCodingException(java.nio.charset.CharacterCodingException) CoderResult(java.nio.charset.CoderResult)

Example 44 with CoderResult

use of java.nio.charset.CoderResult in project logging-log4j2 by apache.

the class TextEncoderHelper method flushRemainingBytes.

private static ByteBuffer flushRemainingBytes(final CharsetEncoder charsetEncoder, final ByteBufferDestination destination, ByteBuffer temp) throws CharacterCodingException {
    CoderResult result;
    do {
        // write any final bytes to the output buffer once the overall input sequence has been read
        result = charsetEncoder.flush(temp);
        temp = drainIfByteBufferFull(destination, temp, result);
    } while (// byte buffer has been drained: retry
    result.isOverflow());
    if (!result.isUnderflow()) {
        // we should have fully flushed the remaining bytes
        result.throwException();
    }
    return temp;
}
Also used : CoderResult(java.nio.charset.CoderResult)

Example 45 with CoderResult

use of java.nio.charset.CoderResult in project logging-log4j2 by apache.

the class TextEncoderHelper method encodeAsMuchAsPossible.

private static ByteBuffer encodeAsMuchAsPossible(final CharsetEncoder charsetEncoder, final CharBuffer charBuf, final boolean endOfInput, final ByteBufferDestination destination, ByteBuffer temp) throws CharacterCodingException {
    CoderResult result;
    do {
        result = charsetEncoder.encode(charBuf, temp, endOfInput);
        temp = drainIfByteBufferFull(destination, temp, result);
    } while (// byte buffer has been drained: retry
    result.isOverflow());
    if (!result.isUnderflow()) {
        // we should have fully read the char buffer contents
        result.throwException();
    }
    return temp;
}
Also used : 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