Example usage for io.netty.util CharsetUtil decoder

List of usage examples for io.netty.util CharsetUtil decoder

Introduction

In this page you can find the example usage for io.netty.util CharsetUtil decoder.

Prototype

public static CharsetDecoder decoder(Charset charset) 

Source Link

Document

Returns a cached thread-local CharsetDecoder for the specified Charset .

Usage

From source file:io.gatling.netty.util.ahc.ByteBufUtils.java

License:Apache License

private static CharBuffer decode(ByteBuf src, Charset charset) {
    int readerIndex = src.readerIndex();
    int len = src.readableBytes();
    final CharsetDecoder decoder = CharsetUtil.decoder(charset);
    CharBuffer dst = pooledCharBuffer(len, decoder);
    if (src.nioBufferCount() == 1) {
        // Use internalNioBuffer(...) to reduce object creation.
        // BEWARE: NOT THREAD-SAFE
        decode(decoder, src.internalNioBuffer(readerIndex, len), dst);
    } else {/*ww  w  . j  a  v  a2 s  .c  o  m*/
        // We use a heap buffer as CharsetDecoder is most likely able to use a fast-path if src and dst buffers
        // are both backed by a byte array.
        ByteBuf buffer = src.alloc().heapBuffer(len);
        try {
            buffer.writeBytes(src, readerIndex, len);
            // Use internalNioBuffer(...) to reduce object creation.
            decode(decoder, buffer.internalNioBuffer(buffer.readerIndex(), len), dst);
        } finally {
            // Release the temporary buffer again.
            buffer.release();
        }
    }
    dst.flip();
    return dst;
}