Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

Transfers this buffer's data to the specified destination starting at the current readerIndex until the destination's position reaches its limit, and increases the readerIndex by the number of the transferred bytes.

Usage

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

@Nullable
static String readCString(ByteBuf buffer) {
    byte[] bytes = new byte[buffer.bytesBefore((byte) 0) + 1];
    if (bytes.length == 0) {
        return null;
    }//from   w  w w. ja  v  a  2s .c om
    buffer.readBytes(bytes);
    return new String(bytes, 0, bytes.length - 1, StandardCharsets.UTF_8);
}

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

@Nullable
private static char[] readCharArray(ByteBuf buffer) {
    byte[] bytes = new byte[buffer.bytesBefore((byte) 0) + 1];
    if (bytes.length == 0) {
        return null;
    }/* ww w  .j  av a 2s.  c o m*/
    buffer.readBytes(bytes);
    return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).array();
}

From source file:io.crate.protocols.postgres.PostgresWireProtocol.java

License:Apache License

private Properties readStartupMessage(ByteBuf buffer) {
    Properties properties = new Properties();
    ByteBuf byteBuf = buffer.readBytes(msgLength);
    while (true) {
        String key = readCString(byteBuf);
        if (key == null) {
            break;
        }//from   w  w w  . j av  a 2 s .c  o m
        String value = readCString(byteBuf);
        LOGGER.trace("payload: key={} value={}", key, value);
        if (!"".equals(key) && !"".equals(value)) {
            properties.setProperty(key, value);
        }
    }
    byteBuf.release();
    return properties;
}

From source file:io.crate.protocols.postgres.PostgresWireProtocolTest.java

License:Apache License

@Test
public void testHandleEmptySimpleQuery() throws Exception {
    PostgresWireProtocol ctx = new PostgresWireProtocol(mock(SQLOperations.class),
            new AlwaysOKNullAuthentication(), null);
    EmbeddedChannel channel = new EmbeddedChannel(ctx.decoder, ctx.handler);

    ByteBuf buffer = Unpooled.buffer();/*from  w w  w.  j  a  v a2 s. c om*/
    try {
        Messages.writeCString(buffer, ";".getBytes(StandardCharsets.UTF_8));
        ctx.handleSimpleQuery(buffer, channel);
    } finally {
        buffer.release();
    }

    ByteBuf firstResponse = channel.readOutbound();
    byte[] responseBytes = new byte[5];
    firstResponse.readBytes(responseBytes);
    // EmptyQueryResponse: 'I' | int32 len
    assertThat(responseBytes, is(new byte[] { 'I', 0, 0, 0, 4 }));

    ByteBuf secondResponse = channel.readOutbound();
    responseBytes = new byte[6];
    secondResponse.readBytes(responseBytes);
    // ReadyForQuery: 'Z' | int32 len | 'I'
    assertThat(responseBytes, is(new byte[] { 'Z', 0, 0, 0, 5, 'I' }));
}

From source file:io.crate.protocols.postgres.types.JsonType.java

License:Apache License

@Override
public Object readBinaryValue(ByteBuf buffer, int valueLength) {
    byte[] bytes = new byte[valueLength];
    buffer.readBytes(bytes);
    return decodeUTF8Text(bytes);
}

From source file:io.crate.protocols.postgres.types.PGType.java

License:Apache License

public Object readTextValue(ByteBuf buffer, int valueLength) {
    byte[] bytes = new byte[valueLength];
    buffer.readBytes(bytes);
    try {/*from  www .ja v a 2  s  . c om*/
        return decodeUTF8Text(bytes);
    } catch (Throwable t) {
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("decodeUTF8Text failed. input={} type={}", new String(bytes, StandardCharsets.UTF_8),
                    typName);
        }
        throw t;
    }
}

From source file:io.crate.protocols.postgres.types.VarCharType.java

License:Apache License

@Override
public Object readBinaryValue(ByteBuf buffer, int valueLength) {
    BytesRef bytesRef = new BytesRef(valueLength);
    bytesRef.length = valueLength;//from  w  w w  .j a  v  a 2 s  . co  m
    buffer.readBytes(bytesRef.bytes);
    return bytesRef;
}

From source file:io.datty.msgpack.core.AbstractMessageReader.java

License:Apache License

public String readString(ByteBuf buffer, int length, Charset charset) {
    if (length > buffer.readableBytes()) {
        throw new MessageParseException(
                "insufficient buffer length: " + buffer.readableBytes() + ", required length: " + length);
    }//from w w  w.  j  a v a2  s  . c o m
    if (buffer.hasArray()) {
        int start = buffer.readerIndex();
        int baseOffset = buffer.arrayOffset() + start;
        String str = new String(buffer.array(), baseOffset, length, charset);
        buffer.readerIndex(start + length);
        return str;
    } else {
        byte[] bytes = new byte[length];
        buffer.readBytes(bytes);
        return new String(bytes, charset);
    }
}

From source file:io.fabric8.kubeflix.examples.helloribbon.HelloRibbonServlet.java

License:Apache License

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    resp.setContentType("text/html");
    final PrintWriter out = resp.getWriter();
    template.requestBuilder().build().toObservable().toBlocking().forEach(new Action1<ByteBuf>() {
        @Override//from ww w  .  j  a v  a2s .  c  o m
        public void call(ByteBuf buffer) {
            byte[] bytes = new byte[buffer.readableBytes()];
            buffer.readBytes(bytes);
            String next = new String(bytes);
            out.println("<h1>" + next + "</h1>");
        }
    });
}

From source file:io.flood.rpc.network.NetPackage.java

License:Apache License

public static NetPackage create(int length, ByteBuf buffer) {
    NetPackage netPackage = new NetPackage();
    netPackage.length = length;/*w w w. j  a  va 2s  . co m*/
    if (length < HEADER_LENTH) {
        throw new IllegalArgumentException(
                "buffer's length less than header length(" + length + "<" + HEADER_LENTH + ")");
    }
    netPackage.sequeue = buffer.readInt();
    netPackage.version = buffer.readInt();
    netPackage.command = buffer.readInt();
    netPackage.state = buffer.readInt();
    netPackage.data = new byte[length - HEADER_LENTH];
    buffer.readBytes(netPackage.data);
    return netPackage;
}