Beispiel #1
0
 @Test
 public void testProtocol() {
   Data data = new Data();
   data.id = 1;
   data.cmd = 2;
   data.type = 0;
   data.bData = 3;
   data.iData = 4;
   byte[] buf = new byte[Protocol.getMaxLength()];
   int len = Protocol.toByteArray(data, buf);
   for (int i = 0; i < len; i++) {
     System.out.print(String.format("%02X ", buf[i]));
   }
 }
Beispiel #2
0
  @Test
  public void testStreams() throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(byteArrayOutputStream);
    for (int i = 0; i < 10; i++) {
      Data data = new Data();
      data.id = i;
      data.cmd = (byte) i;
      data.type = 0;
      data.bData = 0;

      Protocol.toOutputStream(data, outputStream);

      DataInputStream inputStream =
          new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
      Data newData = Protocol.fromInputStream(inputStream);
      byteArrayOutputStream.reset();

      assertEquals(data.id, newData.id);
      assertEquals(data.cmd, newData.cmd);
    }
  }