Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

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

Usage

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

private static DnsRecord newMappedAddressRecord(String name, String ipV4Addr) {
    final ByteBuf content = Unpooled.buffer();
    content.writeZero(10);// w  w w  .ja  v a  2s  .  co  m
    content.writeShort(0xFFFF);
    content.writeBytes(NetUtil.createByteArrayFromIpAddressString(ipV4Addr));
    return new DefaultDnsRawRecord(name, AAAA, 60, content);
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsTextEndpointGroupTest.java

License:Apache License

private static DnsRecord newTxtRecord(String hostname, String text) {
    final ByteBuf content = Unpooled.buffer();
    content.writeByte(text.length());//from ww w. j  av  a 2 s  . c o  m
    content.writeBytes(text.getBytes(StandardCharsets.US_ASCII));
    return new DefaultDnsRawRecord(hostname, TXT, 60, content);
}

From source file:com.linecorp.armeria.client.Http2ClientSettingsTest.java

License:Apache License

private static ByteBuf readGoAwayFrame(InputStream in) throws IOException {
    // Read a GOAWAY frame.
    final byte[] goAwayFrameBuf = readBytes(in, 9);
    final int payloadLength = payloadLength(goAwayFrameBuf);
    final ByteBuf buffer = ByteBufAllocator.DEFAULT.buffer(9 + payloadLength);
    buffer.writeBytes(goAwayFrameBuf);
    buffer.writeBytes(in, payloadLength);
    return buffer;
}

From source file:com.linecorp.armeria.common.HttpMessageAggregator.java

License:Apache License

@Override
public void accept(Void unused, Throwable cause) {
    if (cause != null) {
        fail(cause);//from   w  w  w  .  j  a v a2  s.c  o  m
        return;
    }

    final HttpData content;
    if (contentLength == 0) {
        content = HttpData.EMPTY_DATA;
    } else {
        if (alloc != null) {
            final ByteBuf merged = alloc.buffer(contentLength);
            for (int i = 0; i < contentList.size(); i++) {
                final HttpData data = contentList.set(i, null);
                if (data instanceof ByteBufHolder) {
                    ByteBufHolder byteBufData = (ByteBufHolder) data;
                    try {
                        merged.writeBytes(byteBufData.content());
                    } finally {
                        byteBufData.release();
                    }
                } else {
                    merged.writeBytes(data.array(), data.offset(), data.length());
                }
            }
            content = new ByteBufHttpData(merged, true);
        } else {
            final byte[] merged = new byte[contentLength];
            for (int i = 0, offset = 0; i < contentList.size(); i++) {
                final HttpData data = contentList.set(i, null);
                final int dataLength = data.length();
                System.arraycopy(data.array(), data.offset(), merged, offset, dataLength);
                offset += dataLength;
            }
            content = HttpData.of(merged);
        }
    }

    try {
        future.complete(onSuccess(content));
    } catch (Throwable e) {
        future.completeExceptionally(e);
    }
}

From source file:com.linecorp.armeria.internal.grpc.ArmeriaMessageFramer.java

License:Apache License

private ByteBuf write(ByteBuf message, boolean compressed) {
    try {/*from  w ww.ja  v a  2 s .  c om*/
        final int messageLength = message.readableBytes();
        if (maxOutboundMessageSize >= 0 && messageLength > maxOutboundMessageSize) {
            throw Status.RESOURCE_EXHAUSTED
                    .withDescription(
                            String.format("message too large %d > %d", messageLength, maxOutboundMessageSize))
                    .asRuntimeException();
        }
        final ByteBuf buf = alloc.buffer(HEADER_LENGTH + messageLength);
        buf.writeByte(compressed ? COMPRESSED : UNCOMPRESSED);
        buf.writeInt(messageLength);
        buf.writeBytes(message);
        return buf;
    } finally {
        message.release();
    }
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java

License:Apache License

@Test
public void deserializeRequest_byteBuf() throws Exception {
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray());
    SimpleRequest request = marshaller.deserializeRequest(new ByteBufOrStream(buf));
    assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(0);
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java

License:Apache License

@Test
public void deserializeRequest_wrappedByteBuf() throws Exception {
    marshaller = new GrpcMessageMarshaller<>(ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO,
            TestServiceGrpc.getUnaryCallMethod(),
            MessageMarshaller.builder().register(SimpleRequest.getDefaultInstance())
                    .register(SimpleResponse.getDefaultInstance()).build(),
            true);/*  ww w  .ja  va2  s  .c  o m*/
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray());
    SimpleRequest request = marshaller.deserializeRequest(new ByteBufOrStream(buf));
    assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.release();
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java

License:Apache License

@Test
public void deserializeResponse_bytebuf() throws Exception {
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.RESPONSE_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray());
    SimpleResponse response = marshaller.deserializeResponse(new ByteBufOrStream(buf));
    assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(0);
}

From source file:com.linecorp.armeria.internal.grpc.GrpcMessageMarshallerTest.java

License:Apache License

@Test
public void deserializeResponse_wrappedByteBuf() throws Exception {
    marshaller = new GrpcMessageMarshaller<>(ByteBufAllocator.DEFAULT, GrpcSerializationFormats.PROTO,
            TestServiceGrpc.getUnaryCallMethod(),
            MessageMarshaller.builder().register(SimpleRequest.getDefaultInstance())
                    .register(SimpleResponse.getDefaultInstance()).build(),
            true);//w w w.  jav a2  s.c o m
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.RESPONSE_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.RESPONSE_MESSAGE.toByteArray());
    SimpleResponse response = marshaller.deserializeResponse(new ByteBufOrStream(buf));
    assertThat(response).isEqualTo(GrpcTestUtil.RESPONSE_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.release();
}

From source file:com.linecorp.armeria.internal.grpc.GrpcTestUtil.java

License:Apache License

public static byte[] uncompressedFrame(ByteBuf proto) {
    ByteBuf buf = Unpooled.buffer();
    buf.writeByte(0);//from  ww  w  .  j  a  v a  2 s. c  om
    buf.writeInt(proto.readableBytes());
    buf.writeBytes(proto);
    proto.release();
    byte[] result = ByteBufUtil.getBytes(buf);
    buf.release();
    return result;
}