public static ByteBuf copiedBuffer(
            CharSequence string, int offset, int length, Charset charset) {
        if (string == null) {
            throw new NullPointerException("string");
        }
        if (length == 0) {
            return EMPTY_BUFFER;
        }

        if (string instanceof CharBuffer) {
            CharBuffer buf = (CharBuffer) string;
            if (buf.hasArray()) {
                return copiedBuffer(
                        buf.array(),
                        buf.arrayOffset() + buf.position() + offset,
                        length, charset);
            }

            buf = buf.slice();
            buf.limit(length);
            buf.position(offset);
            return copiedBuffer(buf, charset);
        }

        return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
    }
public static ByteBuf copiedBuffer ( CharSequence string , int offset , int length , Charset charset ) { if ( string == null ) { throw new NullPointerException ( "string" ) ; } if ( length == 0 ) { return EMPTY_BUFFER ; } if ( string instanceof CharBuffer ) { CharBuffer buf = ( CharBuffer ) string ; if ( buf . hasArray ( ) ) { return copiedBuffer ( buf . array ( ) , buf . arrayOffset ( ) + buf . position ( ) + offset , length , charset ) ; } buf = buf . slice ( ) ; buf . limit ( length ) ; buf . position ( offset ) ; return copiedBuffer ( buf , charset ) ; } return copiedBuffer ( CharBuffer . wrap ( string , offset , offset + length ) , charset ) ; }
Creates a new big-endian buffer whose content is a subregion of
the specified {@code string} encoded in the specified {@code charset}.
The new buffer's {@code readerIndex} and {@code writerIndex} are
{@code 0} and the length of the encoded string respectively.