Java Code Examples for io.netty.buffer.Unpooled.buffer()

The following are Jave code examples for showing how to use buffer() of the io.netty.buffer.Unpooled class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: angel   File: ServerDenseFloatRowTest.java   Source Code and License Vote up 6 votes
@Test
public void testUpdate() throws Exception {
  serverDenseFloatRow = new ServerDenseFloatRow(rowId, startCol, endCol);
  ByteBuf buf = Unpooled.buffer(16);
  buf.writeFloat((float) 0.00);
  buf.writeFloat((float) 1.00);
  buf.writeFloat((float) -1.00);
  float newValue0 = buf.getFloat(0) + serverDenseFloatRow.getData().get(0);
  float newValue1 = buf.getFloat(4) + serverDenseFloatRow.getData().get(1);
  serverDenseFloatRow.update(RowType.T_FLOAT_DENSE, buf, 3);
  assertEquals(serverDenseFloatRow.getData().get(0), newValue0, 0.000);
  assertEquals(serverDenseFloatRow.getData().get(1), newValue1, 0.000);
  assertEquals(serverDenseFloatRow.getData().get(2), -1, 0.000);

  serverDenseFloatRow = new ServerDenseFloatRow(rowId, startCol, endCol);
  buf = Unpooled.buffer(0);
  buf.writeInt(0);
  buf.writeFloat((float) 1.00);
  buf.writeInt(2);
  buf.writeFloat((float) -2.00);
  serverDenseFloatRow.update(RowType.T_FLOAT_SPARSE, buf, 2);
  assertEquals(serverDenseFloatRow.getData().get(0), 1, 0.000);
  assertEquals(serverDenseFloatRow.getData().get(1), 0, 0.000);
  assertEquals(serverDenseFloatRow.getData().get(2), -2, 0.000);
}
 
Example 2
Project: angel   File: ServerDenseFloatRowTest.java   Source Code and License Vote up 6 votes
@Test
public void testReadFrom() throws Exception {
  ByteBuf buf = Unpooled.buffer(16);
  buf.writeFloat((float) 10.00);
  buf.writeFloat((float) 11.00);
  buf.writeFloat((float) 12.00);
  serverDenseFloatRow.update(RowType.T_FLOAT_DENSE, buf, 3);
  DataOutputStream out = new DataOutputStream(new FileOutputStream("data"));
  serverDenseFloatRow.writeTo(out);
  out.close();
  DataInputStream in = new DataInputStream(new FileInputStream("data"));
  ServerDenseFloatRow newServerDenseFloatRow = new ServerDenseFloatRow(rowId, startCol, endCol);
  newServerDenseFloatRow.readFrom(in);

  assertEquals(newServerDenseFloatRow.getData().get(0), serverDenseFloatRow.getData().get(0),
      0.00);
  assertEquals(newServerDenseFloatRow.getData().get(1), serverDenseFloatRow.getData().get(1),
      0.00);
  assertEquals(newServerDenseFloatRow.getData().get(2), serverDenseFloatRow.getData().get(2),
      0.00);
}
 
Example 3
Project: angel   File: ServerDenseDoubleRowTest.java   Source Code and License Vote up 6 votes
@Test
public void testReadFrom() throws Exception {
  ByteBuf buf = Unpooled.buffer(16);
  buf.writeDouble(10.00);
  buf.writeDouble(11.00);
  buf.writeDouble(12.00);
  serverDenseDoubleRow.update(RowType.T_DOUBLE_DENSE, buf, 3);
  DataOutputStream out = new DataOutputStream(new FileOutputStream("data"));
  serverDenseDoubleRow.writeTo(out);
  out.close();
  DataInputStream in = new DataInputStream(new FileInputStream("data"));
  ServerDenseDoubleRow newServerDenseDoubleRow =
      new ServerDenseDoubleRow(rowId, startCol, endCol);
  newServerDenseDoubleRow.readFrom(in);
  in.close();
  assertEquals(newServerDenseDoubleRow.getData().get(0), serverDenseDoubleRow.getData().get(0),
      0.00);
  assertEquals(newServerDenseDoubleRow.getData().get(1), serverDenseDoubleRow.getData().get(1),
      0.00);
  assertEquals(newServerDenseDoubleRow.getData().get(2), serverDenseDoubleRow.getData().get(2),
      0.00);
}
 
Example 4
Project: incubator-plc4x   File: IsoTPProtocolTest.java   Source Code and License Vote up 6 votes
@Test
@Tag("fast")
public void encodeConnectionRequest() throws Exception {
    ChannelHandlerContext ctx = new MockChannelHandlerContext();
    ByteBuf buf = Unpooled.buffer();
    ConnectionRequestTpdu tpdu = new ConnectionRequestTpdu((short)0x1, (short)(0x2), ProtocolClass.CLASS_0, Collections.emptyList(), buf);
    ArrayList<Object> out = new ArrayList<>();

    isoTPProtocol.encode(ctx, tpdu, out);

    assertTrue(out.size() == 1, "Message not decoded");

    ByteBuf userData = ((IsoOnTcpMessage)out.get(0)).getUserData();
    
    assertTrue(userData.writerIndex() == 7, "Incorrect message length");
    assertTrue(userData.readByte() == (byte)0x6, "Incorrect header length");
    assertTrue(userData.readByte() == TpduCode.CONNECTION_REQUEST.getCode(), "Incorrect Tpdu code");
    assertTrue(userData.readShort() == (short)0x1, "Incorrect destination reference code");
    assertTrue(userData.readShort() == (short)0x2, "Incorrect source reference code");
    assertTrue(userData.readByte() == ProtocolClass.CLASS_0.getCode(), "Incorrect protocol class");
}
 
Example 5
Project: angel   File: ServerSparseDoubleRowTest.java   Source Code and License Vote up 6 votes
@Test
public void testSerialize() throws Exception {
  ByteBuf buf = Unpooled.buffer(16);
  serverSparseDoubleRow.setClock(8);
  buf.writeDouble(0.00);
  buf.writeDouble(1.00);
  buf.writeDouble(-1.00);
  serverSparseDoubleRow.update(RowType.T_DOUBLE_DENSE, buf, 3);
  serverSparseDoubleRow.serialize(buf);
  assertEquals(serverSparseDoubleRow.getRowId(), buf.readInt());
  assertEquals(serverSparseDoubleRow.getClock(), buf.readInt());
  assertEquals(serverSparseDoubleRow.getStartCol(), buf.readLong());
  assertEquals(serverSparseDoubleRow.getEndCol(), buf.readLong());
  assertEquals(serverSparseDoubleRow.getRowVersion(), buf.readInt());
  assertEquals(3, buf.readInt());
}
 
Example 6
Project: BaseClient   File: EntityPlayerMP.java   Source Code and License Vote up 6 votes
public void displayVillagerTradeGui(IMerchant villager)
{
    this.getNextWindowId();
    this.openContainer = new ContainerMerchant(this.inventory, villager, this.worldObj);
    this.openContainer.windowId = this.currentWindowId;
    this.openContainer.onCraftGuiOpened(this);
    IInventory iinventory = ((ContainerMerchant)this.openContainer).getMerchantInventory();
    IChatComponent ichatcomponent = villager.getDisplayName();
    this.playerNetServerHandler.sendPacket(new S2DPacketOpenWindow(this.currentWindowId, "minecraft:villager", ichatcomponent, iinventory.getSizeInventory()));
    MerchantRecipeList merchantrecipelist = villager.getRecipes(this);

    if (merchantrecipelist != null)
    {
        PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
        packetbuffer.writeInt(this.currentWindowId);
        merchantrecipelist.writeToBuf(packetbuffer);
        this.playerNetServerHandler.sendPacket(new S3FPacketCustomPayload("MC|TrList", packetbuffer));
    }
}
 
Example 7
Project: message-broker   File: BasicCancelTest.java   Source Code and License Vote up 6 votes
@Test
public void testEncodeDecodeWithFalseFlags() throws Exception {
    BasicCancel testFrame = new BasicCancel(1,
                                            ShortString.parseString("1"),
                                            false);
    ByteBuf buf = Unpooled.buffer((int) testFrame.getMethodBodySize());
    testFrame.writeMethod(buf);
    BasicCancel decodedFrame = (BasicCancel) BasicCancel.getFactory()
                                                        .newInstance(buf, 1, testFrame.getMethodBodySize());

    Assert.assertEquals(decodedFrame.getChannel(), testFrame.getChannel(), "Decoded frame's channel should match"
            + " the original frame's channel");
    Assert.assertEquals(decodedFrame.getConsumerTag(), testFrame.getConsumerTag(), "Decoded frame's consumer-tag "
            + "should match the original frame's consumer-tag");
    Assert.assertEquals(decodedFrame.isNoWait(), testFrame.isNoWait(), "Decoded frame's no-wait bit "
            + "should match the original frame's no-wait bit");

}
 
Example 8
Project: DecompiledMinecraft   File: MinecraftServer.java   Source Code and License Vote up 5 votes
private void addFaviconToStatusResponse(ServerStatusResponse response)
{
    File file1 = this.getFile("server-icon.png");

    if (file1.isFile())
    {
        ByteBuf bytebuf = Unpooled.buffer();

        try
        {
            BufferedImage bufferedimage = ImageIO.read(file1);
            Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]);
            Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]);
            ImageIO.write(bufferedimage, "PNG", (OutputStream)(new ByteBufOutputStream(bytebuf)));
            ByteBuf bytebuf1 = Base64.encode(bytebuf);
            response.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8));
        }
        catch (Exception exception)
        {
            logger.error((String)"Couldn\'t load server icon", (Throwable)exception);
        }
        finally
        {
            bytebuf.release();
        }
    }
}
 
Example 9
Project: CustomWorldGen   File: EntityPlayerMP.java   Source Code and License Vote up 5 votes
public void openBook(ItemStack stack, EnumHand hand)
{
    Item item = stack.getItem();

    if (item == Items.WRITTEN_BOOK)
    {
        PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
        packetbuffer.writeEnumValue(hand);
        this.connection.sendPacket(new SPacketCustomPayload("MC|BOpen", packetbuffer));
    }
}
 
Example 10
Project: spark_deep   File: EchoClientHandler.java   Source Code and License Vote up 5 votes
/**
     * Creates a client-side handler.
     */
    public EchoClientHandler() {
        firstMessage = Unpooled.buffer(EchoClient.SIZE);
//        for (int i = 0; i < firstMessage.capacity(); i ++) {
//            firstMessage.writeByte("4".getBytes()[0]);
//        }

        firstMessage.writeByte("qwe".getBytes()[0]);
    }
 
Example 11
Project: ProtocolSupportPocketStuff   File: PEPacket.java   Source Code and License Vote up 5 votes
public ByteBuf encode(Connection connection) {
	ByteBuf serializer = Unpooled.buffer();
	VarNumberSerializer.writeVarInt(serializer, getPacketId());
	serializer.writeByte(0);
	serializer.writeByte(0);
	toData(connection, serializer);
	return serializer;
}
 
Example 12
Project: JavaNRPC   File: NrpcChannel.java   Source Code and License Vote up 5 votes
public void sendRpcMessage(Socket socket, NrpcMeta requestMessage) throws IOException {
    readBuffer.reset();  //清空应答的读缓冲
    byte[] bytes = requestMessage.toByteArray();
    int totalSize = Constants.HEADER_SIZE + bytes.length;
    ByteBuf buffer = Unpooled.buffer(totalSize);
    buffer.writeBytes(new byte[]{'N', 'R', 'P', 'C'});
    buffer.writeInt(totalSize);
    buffer.writeBytes(new byte[]{0, 0, 0, 0});
    buffer.writeBytes(bytes);
    DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
    dOut.write(buffer.array());
    dOut.flush();
}
 
Example 13
Project: message-broker   File: BasicConsumeTest.java   Source Code and License Vote up 5 votes
@Test
public void testEncodeDecode() throws Exception {
    BasicConsume testFrame = new BasicConsume(1,
                                              ShortString.parseString("queue"),
                                              ShortString.parseString("1"),
                                              true,
                                              true,
                                              true,
                                              true,
                                              FieldTable.EMPTY_TABLE);
    ByteBuf buf = Unpooled.buffer((int) testFrame.getMethodBodySize());
    testFrame.writeMethod(buf);
    BasicConsume decodedFrame = (BasicConsume) BasicConsume.getFactory()
                                                           .newInstance(buf, 1, testFrame.getMethodBodySize());

    Assert.assertEquals(decodedFrame.getChannel(), testFrame.getChannel(), "Decoded frame's channel should match"
            + " the original frame's channel");
    Assert.assertEquals(decodedFrame.getQueue(), testFrame.getQueue(), "Decoded frame's queue should match"
            + " the original frame's queue");
    Assert.assertEquals(decodedFrame.getConsumerTag(), testFrame.getConsumerTag(), "Decoded frame's consumer-tag "
            + "should match the original frame's consumer-tag");
    Assert.assertEquals(decodedFrame.isNoLocal(), testFrame.isNoLocal(), "Decoded frame's no-local bit should "
            + "match the original frame's no-local bit");
    Assert.assertEquals(decodedFrame.isNoAck(), testFrame.isNoAck(), "Decoded frame's no-ack bit should match"
            + " the original frame's no-ack bit");
    Assert.assertEquals(decodedFrame.isExclusive(), testFrame.isExclusive(), "Decoded frame's exclusive bit "
            + "should match the original frame's exclusive bit");
    Assert.assertEquals(decodedFrame.isNoWait(), testFrame.isNoWait(), "Decoded frame's no-wait bit "
            + "should match the original frame's no-wait bit");
    Assert.assertEquals(decodedFrame.getArguments(), testFrame.getArguments(), "Decoded frame's arguments "
            + "should match the original frame's arguments");
}
 
Example 14
Project: neoscada   File: MessageManager.java   Source Code and License Vote up 5 votes
private ByteBuf mirrorUnknown ( final ByteBuf data, final byte typeId, final InformationStructure informationStructure, final byte size, final ASDUHeader header, final Cause newCause )
{
    logger.debug ( "Mirror unknown message -> {}", newCause );
    final ByteBuf reply = Unpooled.buffer ();

    EncodeHelper.encodeHeader ( typeId, informationStructure, this.options, (int)size, header.clone ( newCause ), reply );

    reply.writeBytes ( data ); // copy data

    return reply;
}
 
Example 15
Project: angel   File: ServerSparseDoubleRowTest.java   Source Code and License Vote up 5 votes
@Test
public void testBufferLen() throws Exception {
  ByteBuf buf = Unpooled.buffer(16);
  serverSparseDoubleRow.setClock(8);
  buf.writeDouble(0.00);
  buf.writeDouble(1.00);
  buf.writeDouble(-1.00);
  serverSparseDoubleRow.update(RowType.T_DOUBLE_DENSE, buf, 3);
  assertEquals(serverSparseDoubleRow.bufferLen(), 20 + 4 + 3 * 12);
}
 
Example 16
Project: angel   File: DefaultRowUpdaterTest.java   Source Code and License Vote up 5 votes
@Test
public void testUpdateIntDenseToIntDense() throws Exception {
  ServerDenseIntRow serverDenseIntRow = new ServerDenseIntRow(rowId, startCol, endCol);
  ByteBuf buf = Unpooled.buffer(16);
  buf.writeInt(0);
  buf.writeInt(1);
  buf.writeInt(-1);
  rowUpdater.updateIntDenseToIntDense(3, buf, serverDenseIntRow);
  assertEquals(serverDenseIntRow.getData().get(0), 0);
  assertEquals(serverDenseIntRow.getData().get(1), 1);
  assertEquals(serverDenseIntRow.getData().get(2), -1);
}
 
Example 17
Project: angel   File: ServerPartitionTest.java   Source Code and License Vote up 5 votes
@Test
public void testSerialize() throws Exception {
  ByteBuf buf = Unpooled.buffer(16);
  serverPartition.serialize(buf);
  assertEquals(partitionKey.getMatrixId(), buf.readInt());
  assertEquals(partitionKey.getPartitionId(), buf.readInt());
  assertEquals(partitionKey.getStartRow(), buf.readInt());
  assertEquals(partitionKey.getEndRow(), buf.readInt());
  assertEquals(partitionKey.getStartCol(), buf.readLong());
  assertEquals(partitionKey.getEndCol(), buf.readLong());

  assertEquals(rowType.getNumber(), buf.readInt());
  assertEquals(partitionKey.getEndRow() - partitionKey.getStartRow(), buf.readInt());
}
 
Example 18
Project: BaseClient   File: GuiMerchant.java   Source Code and License Vote up 5 votes
/**
 * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
 */
protected void actionPerformed(GuiButton button) throws IOException
{
    boolean flag = false;

    if (button == this.nextButton)
    {
        ++this.selectedMerchantRecipe;
        MerchantRecipeList merchantrecipelist = this.merchant.getRecipes(this.mc.thePlayer);

        if (merchantrecipelist != null && this.selectedMerchantRecipe >= merchantrecipelist.size())
        {
            this.selectedMerchantRecipe = merchantrecipelist.size() - 1;
        }

        flag = true;
    }
    else if (button == this.previousButton)
    {
        --this.selectedMerchantRecipe;

        if (this.selectedMerchantRecipe < 0)
        {
            this.selectedMerchantRecipe = 0;
        }

        flag = true;
    }

    if (flag)
    {
        ((ContainerMerchant)this.inventorySlots).setCurrentRecipeIndex(this.selectedMerchantRecipe);
        PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
        packetbuffer.writeInt(this.selectedMerchantRecipe);
        this.mc.getNetHandler().addToSendQueue(new C17PacketCustomPayload("MC|TrSel", packetbuffer));
    }
}
 
Example 19
Project: skylla   File: SkyllaBuffer.java   Source Code and License Vote up 4 votes
/**
 * Create an empty buffer.
 */
public SkyllaBuffer() {
    this(Unpooled.buffer());
}
 
Example 20
Project: RakNetty   File: RakNetByteBuf.java   Source Code and License Vote up 4 votes
public static RakNetByteBuf buffer() {
    return new RakNetByteBuf(Unpooled.buffer());
}