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.seventh_root.ld33.common.network.packet.Packet.java

License:Apache License

public void writeString(ByteBuf buf, String string) throws UnsupportedEncodingException {
    buf.writeBytes((string + "\0").getBytes("utf8"));
}

From source file:com.seventh_root.ld33.common.network.packet.serverbound.PlayerLoginServerBoundPacket.java

License:Apache License

@Override
public void write(ByteBuf buf) throws UnsupportedEncodingException {
    super.write(buf);
    writeString(buf, getPlayerName());//from ww  w  .j  a v a2  s .  c  om
    buf.writeInt(getEncryptedPassword().length);
    buf.writeBytes(getEncryptedPassword());
    buf.writeBoolean(isSignUp());
}

From source file:com.shekar.msrp.codec.MsrpResponseStatus.java

void encode(ByteBuf buf) {
    if (bytes == null) {
        MsrpHeaders.encodeUtf8(String.valueOf(code()), buf);
        buf.writeByte(MsrpConstants.SP);
        if (comment() != null) {
            MsrpHeaders.encodeUtf8(comment(), buf);
        }//from w ww  .  j av a2  s .c o m
    } else {
        buf.writeBytes(bytes);
    }
}

From source file:com.soho.framework.server.netty.http.HttpServletHandler.java

License:Apache License

private void sendError(ChannelHandlerContext ctx, HttpResponseStatus status) {
    String text = "Failure: " + status.toString() + "\r\n";
    ByteBuf byteBuf = Unpooled.buffer();
    byte[] bytes = null;
    try {/*  w w  w. j a va 2  s . com*/
        bytes = text.getBytes("utf-8");
        byteBuf.writeBytes(bytes);
    } catch (UnsupportedEncodingException e) {
    }

    FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, byteBuf);
    HttpHeaders headers = response.headers();

    headers.add(CONTENT_TYPE, "text/plain;charset=utf-8");
    headers.add(CACHE_CONTROL, "no-cache");
    headers.add(PRAGMA, "No-cache");
    headers.add(SERVER, ServerConfig.getServerName());
    headers.add(CONTENT_LENGTH, byteBuf.readableBytes());
    ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}

From source file:com.splicemachine.stream.KryoEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception {
    outStream.reset();/*  w ww  .  ja  va 2 s . c  o m*/

    Kryo encoder = kp.get();
    try {
        encoder.writeClassAndObject(output, in);
    } finally {
        kp.returnInstance(encoder);
    }

    output.flush();
    byte[] outArray = outStream.toByteArray();
    out.writeShort(outArray.length);
    out.writeBytes(outArray);
}

From source file:com.spotify.ffwd.riemann.RiemannResponder.java

License:Apache License

private ByteBuf ack(boolean ok) throws IOException {
    final Proto.Msg m = Proto.Msg.newBuilder().setOk(ok).build();

    final ByteBuf b = Unpooled.buffer();

    try (final ByteBufOutputStream output = new ByteBufOutputStream(b)) {
        m.writeTo(output);/*from w ww. j a v a 2  s .co m*/

        final ByteBuf frame = output.buffer();
        final ByteBuf buffer = Unpooled.buffer(4 + frame.readableBytes());

        buffer.writeInt(frame.readableBytes());
        buffer.writeBytes(frame);

        return buffer;
    } finally {
        b.release();
    }
}

From source file:com.spotify.ffwd.riemann.RiemannSerialization.java

License:Apache License

public ByteBuf encodeAll0(Collection<Object> messages) throws IOException {
    final Proto.Msg.Builder builder = Proto.Msg.newBuilder();

    for (final Object d : messages) {
        if (d instanceof Metric) {
            builder.addEvents(encodeMetric0((Metric) d));
        } else if (d instanceof Event) {
            builder.addEvents(encodeEvent0((Event) d));
        }/*w  w  w .  j a va  2s.  co  m*/
    }

    final Proto.Msg m = builder.build();

    final ByteBuf work = Unpooled.buffer();

    try (final ByteBufOutputStream output = new ByteBufOutputStream(work)) {
        m.writeTo(output);

        final ByteBuf result = Unpooled.buffer();

        result.writeInt(work.writerIndex());
        result.writeBytes(work);

        return result;
    } finally {
        work.release();
    }
}

From source file:com.spotify.folsom.client.ascii.SetRequest.java

License:Apache License

private static ByteBuf toBufferWithValueAndNewLine(final ByteBufAllocator alloc, ByteBuffer dst, byte[] value) {
    ByteBuf buffer = toBuffer(alloc, dst, value.length + NEWLINE_BYTES.length);
    buffer.writeBytes(value);
    buffer.writeBytes(NEWLINE_BYTES);//from   w w w .  j av a 2  s  .co  m
    return buffer;
}

From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoderTest.java

License:Apache License

@Test
public void test() throws Exception {
    GetRequest request = new GetRequest(KEY, OpCode.GET, 123, OPAQUE);
    BinaryMemcacheDecoder decoder = new BinaryMemcacheDecoder();

    ByteBuf cb = Unpooled.buffer(30);
    cb.writeByte(0x81);/* ww  w  .  j av a2  s.c  o  m*/
    cb.writeByte(OpCode.GET);
    cb.writeShort(3);
    cb.writeByte(0);
    cb.writeZero(1);
    cb.writeShort(0);
    cb.writeInt(6);
    cb.writeInt(request.getOpaque());
    cb.writeLong(258);
    cb.writeBytes(KEY.getBytes());
    cb.writeBytes(VALUE.getBytes());

    List<Object> out = Lists.newArrayList();
    decoder.decode(null, cb, out);
    @SuppressWarnings("unchecked")
    List<ResponsePacket> replies = (List<ResponsePacket>) out.get(0);
    request.handle(replies);

    GetResult<byte[]> getResult = request.get();
    assertEquals(258, getResult.getCas());
    assertEquals(VALUE, TRANSCODER.decode(getResult.getValue()));
}

From source file:com.spotify.folsom.client.binary.SetRequest.java

License:Apache License

private static ByteBuf toBufferWithValue(final ByteBufAllocator alloc, ByteBuffer dst, byte[] value) {
    ByteBuf buffer = toBuffer(alloc, dst, value.length);
    buffer.writeBytes(value);
    return buffer;
}