Example usage for io.netty.buffer ByteBuf toString

List of usage examples for io.netty.buffer ByteBuf toString

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf toString.

Prototype

public abstract String toString(int index, int length, Charset charset);

Source Link

Document

Decodes this buffer's sub-region into a string with the specified character set.

Usage

From source file:io.advantageous.conekt.dns.impl.netty.decoder.TextDecoder.java

License:Open Source License

/**
 * Returns a decoded TXT (text) resource record, stored as an
 * {@link java.util.ArrayList} of {@code String}s.
 *
 * @param response the DNS response that contains the resource record being
 *                 decoded/* ww  w  .ja  va  2 s  .  c  om*/
 * @param resource the resource record being decoded
 */
@Override
public List<String> decode(DnsResponse response, DnsResource resource) {
    List<String> list = new ArrayList<>();
    ByteBuf data = resource.content().readerIndex(response.originalIndex());
    int index = data.readerIndex();
    while (index < data.writerIndex()) {
        int len = data.getUnsignedByte(index++);
        list.add(data.toString(index, len, CharsetUtil.UTF_8));
        index += len;
    }
    return list;
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Retrieves a domain name given a buffer containing a DNS packet. If the
 * name contains a pointer, the position of the buffer will be set to
 * directly after the pointer's index after the name has been read.
 *
 * @param buf the byte buffer containing the DNS packet
 * @return the domain name for an entry//w w  w  .java 2s . c o  m
 */
public static String readName(ByteBuf buf) {
    int position = -1;
    StringBuilder name = new StringBuilder();
    for (int len = buf.readUnsignedByte(); buf.isReadable() && len != 0; len = buf.readUnsignedByte()) {
        boolean pointer = (len & 0xc0) == 0xc0;
        if (pointer) {
            if (position == -1) {
                position = buf.readerIndex() + 1;
            }
            buf.readerIndex((len & 0x3f) << 8 | buf.readUnsignedByte());
        } else {
            name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append(".");
            buf.skipBytes(len);
        }
    }
    if (position != -1) {
        buf.readerIndex(position);
    }
    if (name.length() == 0) {
        return null;
    }
    return name.substring(0, name.length() - 1);
}

From source file:io.advantageous.conekt.dns.impl.netty.DnsResponseDecoder.java

License:Open Source License

/**
 * Retrieves a domain name given a buffer containing a DNS packet without
 * advancing the readerIndex for the buffer.
 *
 * @param buf    the byte buffer containing the DNS packet
 * @param offset the position at which the name begins
 * @return the domain name for an entry//from  w  w w.  j  a  v a2s. co m
 */
public static String getName(ByteBuf buf, int offset) {
    StringBuilder name = new StringBuilder();
    for (int len = buf.getUnsignedByte(offset++); buf.writerIndex() > offset
            && len != 0; len = buf.getUnsignedByte(offset++)) {
        boolean pointer = (len & 0xc0) == 0xc0;
        if (pointer) {
            offset = (len & 0x3f) << 8 | buf.getUnsignedByte(offset++);
        } else {
            name.append(buf.toString(offset, len, CharsetUtil.UTF_8)).append(".");
            offset += len;
        }
    }
    if (name.length() == 0) {
        return null;
    }
    return name.substring(0, name.length() - 1);
}

From source file:io.atomix.cluster.messaging.impl.MessageDecoder.java

License:Apache License

static String readString(ByteBuf buffer, int length, Charset charset) {
    if (buffer.isDirect()) {
        final String result = buffer.toString(buffer.readerIndex(), length, charset);
        buffer.skipBytes(length);//  w  w w  . j  ava 2  s .  co m
        return result;
    } else if (buffer.hasArray()) {
        final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length,
                charset);
        buffer.skipBytes(length);
        return result;
    } else {
        final byte[] array = new byte[length];
        buffer.readBytes(array);
        return new String(array, charset);
    }
}

From source file:io.hekate.network.netty.NettyMessage.java

License:Apache License

static String utf(ByteBuf buf) throws IOException {
    try {//from ww w .  j a v  a2 s.c  om
        int len = buf.readInt();

        String string = buf.toString(buf.readerIndex(), len, StandardCharsets.UTF_8);

        buf.skipBytes(len);

        return string;
    } catch (IndexOutOfBoundsException e) {
        throw endOfStream(e);
    }
}

From source file:io.hydramq.core.type.converters.StringConverter.java

License:Open Source License

@Override
public String read(final ConversionContext context, final ByteBuf buffer) {
    int size = buffer.readInt();
    String value = buffer.toString(buffer.readerIndex(), size, CharsetUtil.UTF_8);
    buffer.skipBytes(size);/*from   www .  j a  v a2  s. c  om*/
    return value;
}

From source file:io.jsync.dns.impl.netty.decoder.TextDecoder.java

License:Open Source License

/**
 * Returns a decoded TXT (text) resource record, stored as an
 * {@link ArrayList} of {@code String}s.
 *
 * @param response the DNS response that contains the resource record being
 *                 decoded//from   w ww  .j ava 2  s  .c  o m
 * @param resource the resource record being decoded
 */
@Override
public List<String> decode(DnsResponse response, DnsResource resource) {
    List<String> list = new ArrayList<String>();
    ByteBuf data = resource.content().readerIndex(response.originalIndex());
    int index = data.readerIndex();
    while (index < data.writerIndex()) {
        int len = data.getUnsignedByte(index++);
        list.add(data.toString(index, len, CharsetUtil.UTF_8));
        index += len;
    }
    return list;
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static String utf8Slice(JSObject object, int start, int end) {
    ByteBuf b = extract(object);
    return b.toString(start, (end - start), UTF8);
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static String asciiSlice(JSObject object, int start, int end) {
    ByteBuf b = extract(object);
    return b.toString(start, (end - start), ASCII);
}

From source file:io.nodyn.buffer.Buffer.java

License:Apache License

public static String ucs2Slice(JSObject object, int start, int end) {
    ByteBuf b = extract(object);
    return b.toString(start, (end - start), UCS2);
}