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:appeng.util.item.AEItemStack.java

License:Open Source License

@Override
void readNBT(final ByteBuf i) throws IOException {
    if (this.hasTagCompound()) {
        final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        final DataOutputStream data = new DataOutputStream(bytes);

        CompressedStreamTools.write((NBTTagCompound) this.getTagCompound(), data);

        final byte[] tagBytes = bytes.toByteArray();
        final int size = tagBytes.length;

        i.writeInt(size);/*from   w  ww  .j ava  2s .c  om*/
        i.writeBytes(tagBytes);
    }
}

From source file:at.yawk.accordion.codec.unsafe.NamedObjectCodec.java

License:Mozilla Public License

private static void writeClassName(ByteBuf buf, String className) {
    buf.writeBytes(className.getBytes(StandardCharsets.UTF_8));
    buf.writeByte(0);//from  ww w. j  a  v a  2 s  .  c  o  m
}

From source file:at.yawk.accordion.compression.SnappyCompressorTest.java

License:Mozilla Public License

private void testCompressDecompress(byte[] payload) {
    ByteBuf unc = Unpooled.buffer();
    unc.writeBytes(payload);

    ByteBuf comp = SnappyCompressor.getInstance().encode(unc);
    ByteBuf unc2 = SnappyCompressor.getInstance().decode(comp);

    unc.resetReaderIndex();//from ww  w  .  j ava 2s . c  om
    unc2.resetReaderIndex();

    assertEquals(unc, unc2);
}

From source file:at.yawk.accordion.distributed.InternalProtocol.java

License:Mozilla Public License

/**
 * Write a byte array. Maximum length 0xff bytes.
 *///from   w w w .j  a  va2 s  .co  m
static void writeByteArray(ByteBuf to, byte[] array) {
    to.writeByte(array.length);
    to.writeBytes(array);
}

From source file:at.yawk.accordion.distributed.InternalProtocol.java

License:Mozilla Public License

/**
 * Encode a packet to be read by other nodes.
 *///from w w w  .  ja v a 2  s.c om
static ByteBuf encodePacket(byte[] typeBytes, long id, ByteBuf payload, Compressor compressor) {
    ByteBuf body = Unpooled.buffer();
    // channel
    writeByteArray(body, typeBytes);
    // payload
    body.writeBytes(payload);

    ByteBuf compressedBody = compressor.encode(body);

    ByteBuf full = Unpooled.buffer();
    // id header
    full.writeLong(id);
    // body
    full.writeBytes(compressedBody);
    return full;
}

From source file:at.yawk.accordion.simulation.Simulation.java

License:Mozilla Public License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    Logger.getRootLogger().addAppender(new ConsoleAppender(new SimpleLayout(), ConsoleAppender.SYSTEM_OUT));
    Logger.getRootLogger().setLevel(Level.DEBUG);

    Simulation simulation = new Simulation();
    simulation.populate();// w  w w  . j  a  va  2  s . co  m
    TimeUnit.SECONDS.sleep(1);

    simulation.nodes.get(simulation.tiers[0][0]).getConnectionManager().getChannel("test").subscribe(msg -> {
        int len = msg.readInt();
        byte[] blob = new byte[len];
        msg.readBytes(blob);
        Log.getDefaultLogger().info("Received: " + new String(blob));
    });
    TimeUnit.SECONDS.sleep(1);

    ByteBuf msg = Unpooled.buffer();
    byte[] text = "ping".getBytes();
    msg.writeInt(text.length);
    msg.writeBytes(text);

    simulation.nodes.get(simulation.tiers[0][1]).getConnectionManager().getChannel("test").publish(msg);
}

From source file:at.yawk.dbus.protocol.auth.CommandCodec.java

protected void encode(ChannelHandlerContext ctx, Command msg, ByteBuf out) throws Exception {
    out.writeBytes(msg.getSerialized().getBytes(CHARSET));
    out.writeBytes(CRLF);/*from   w  ww .j a v  a  2  s .com*/
    log.trace("Sent message {}", msg.getSerialized());
}

From source file:at.yawk.votifier.VersionEncoder.java

License:Mozilla Public License

@Override
protected void encode(ChannelHandlerContext ctx, VotifierVersion msg, ByteBuf out) throws Exception {
    String message = "VOTIFIER " + msg.getName() + "\n";
    // apparently votifier uses platform encoding by default .-., we'll use UTF-8
    byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
    out.writeBytes(messageBytes);
}

From source file:bftsmart.communication.client.netty.NettyTOMMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext context, TOMMessage sm, ByteBuf buffer) throws Exception {
    byte[] msgData;
    byte[] signatureData = null;

    msgData = sm.serializedMessage;//from w  w  w  . j a v  a 2 s .  com
    if (sm.signed) {
        //signature was already produced before            
        signatureData = sm.serializedMessageSignature;
    }

    int dataLength = Integer.BYTES + msgData.length + Integer.BYTES
            + (signatureData != null ? signatureData.length : 0);

    /* msg size */
    buffer.writeInt(dataLength);

    /* data to be sent */
    buffer.writeInt(msgData.length);
    buffer.writeBytes(msgData);

    /* signature */
    if (signatureData != null) {
        buffer.writeInt(signatureData.length);
        buffer.writeBytes(signatureData);
    } else {
        buffer.writeInt(0);
    }

    context.flush();
}

From source file:blazingcache.client.CacheClient.java

License:Apache License

private ByteBuf cacheByteArray(byte[] data) {
    ByteBuf buffer;
    if (offHeap) {
        buffer = allocator.directBuffer(data.length);
    } else {/*w w w .j a v a 2 s .co  m*/
        buffer = allocator.heapBuffer(data.length);
    }
    buffer.writeBytes(data);
    return buffer;
}