@Test
 public void testDecodeWithMessageIdAndAck() throws IOException {
   Packet packet = decoder.decodePacket("5:1+::{\"name\":\"tobi\"}", null);
   Assert.assertEquals(PacketType.EVENT, packet.getType());
   Assert.assertEquals(1, (long) packet.getId());
   Assert.assertEquals(Packet.ACK_DATA, packet.getAck());
   Assert.assertEquals("tobi", packet.getName());
 }
  @Test
  public void testDecodeWithData() throws IOException {
    JacksonJsonSupport jsonSupport = new JacksonJsonSupport(new Configuration());
    jsonSupport.addEventMapping("edwald", HashMap.class, Integer.class, String.class);
    Decoder decoder = new Decoder(jsonSupport, ackManager);

    Packet packet =
        decoder.decodePacket("5:::{\"name\":\"edwald\",\"args\":[{\"a\": \"b\"},2,\"3\"]}", null);
    Assert.assertEquals(PacketType.EVENT, packet.getType());
    Assert.assertEquals("edwald", packet.getName());
    Assert.assertEquals(3, packet.getArgs().size());
    Map obj = (Map) packet.getArgs().get(0);
    Assert.assertEquals("b", obj.get("a"));
    Assert.assertEquals(2, packet.getArgs().get(1));
    Assert.assertEquals("3", packet.getArgs().get(2));
  }
 @Test
 public void testDecode() throws IOException {
   Packet packet = decoder.decodePacket("5:::{\"name\":\"woot\"}", null);
   Assert.assertEquals(PacketType.EVENT, packet.getType());
   Assert.assertEquals("woot", packet.getName());
 }
  public void encodePacket(
      Packet packet, ByteBuf buffer, ByteBufAllocator allocator, boolean binary)
      throws IOException {
    ByteBuf buf = buffer;
    if (!binary) {
      buf = allocateBuffer(allocator);
    }
    byte type = toChar(packet.getType().getValue());
    buf.writeByte(type);

    try {
      switch (packet.getType()) {
        case PONG:
          {
            buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8));
            break;
          }

        case OPEN:
          {
            ByteBufOutputStream out = new ByteBufOutputStream(buf);
            jsonSupport.writeValue(out, packet.getData());
            break;
          }

        case MESSAGE:
          {
            ByteBuf encBuf = null;

            if (packet.getSubType() == PacketType.ERROR) {
              encBuf = allocateBuffer(allocator);

              ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
              jsonSupport.writeValue(out, packet.getData());
            }

            if (packet.getSubType() == PacketType.EVENT || packet.getSubType() == PacketType.ACK) {

              List<Object> values = new ArrayList<Object>();
              if (packet.getSubType() == PacketType.EVENT) {
                values.add(packet.getName());
              }

              encBuf = allocateBuffer(allocator);

              List<Object> args = packet.getData();
              values.addAll(args);
              ByteBufOutputStream out = new ByteBufOutputStream(encBuf);
              jsonSupport.writeValue(out, values);

              if (!jsonSupport.getArrays().isEmpty()) {
                packet.initAttachments(jsonSupport.getArrays().size());
                for (byte[] array : jsonSupport.getArrays()) {
                  packet.addAttachment(Unpooled.wrappedBuffer(array));
                }
                packet.setSubType(PacketType.BINARY_EVENT);
              }
            }

            byte subType = toChar(packet.getSubType().getValue());
            buf.writeByte(subType);

            if (packet.hasAttachments()) {
              byte[] ackId = toChars(packet.getAttachments().size());
              buf.writeBytes(ackId);
              buf.writeByte('-');
            }

            if (packet.getSubType() == PacketType.CONNECT) {
              if (!packet.getNsp().isEmpty()) {
                buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
              }
            } else {
              if (!packet.getNsp().isEmpty()) {
                buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
                buf.writeByte(',');
              }
            }

            if (packet.getAckId() != null) {
              byte[] ackId = toChars(packet.getAckId());
              buf.writeBytes(ackId);
            }

            if (encBuf != null) {
              buf.writeBytes(encBuf);
              encBuf.release();
            }

            break;
          }
      }
    } finally {
      // we need to write a buffer in any case
      if (!binary) {
        buffer.writeByte(0);
        int length = buf.writerIndex();
        buffer.writeBytes(longToBytes(length));
        buffer.writeByte(0xff);
        buffer.writeBytes(buf);

        buf.release();
      }
    }
  }