Esp3Packet implements EspPacket { @Override public byte[] asByteArray() { int size = Esp3PacketHeader.ESP3_HEADER_SIZE + data.length + optionalData.length + 1; byte[] packetBytes = new byte[size]; byte[] src = header.asByteArray(); byte[] dest = packetBytes; int srcPos = 0; int destPos = 0; int length = src.length; System.arraycopy(src, srcPos, dest, destPos, length); destPos += length; src = data; length = data.length; System.arraycopy(src, srcPos, dest, destPos, length); destPos += length; src = optionalData; length = optionalData.length; System.arraycopy(src, srcPos, dest, destPos, length); byte crc8 = CRC8.calculate( packetBytes, Esp3PacketHeader.ESP3_HEADER_SIZE, packetBytes.length - Esp3PacketHeader.ESP3_HEADER_SIZE - 1 ); packetBytes[packetBytes.length - 1] = crc8; return packetBytes; } Esp3Packet(Esp3PacketHeader.PacketType packetType, byte[] data, byte[] optionalData); @Override byte[] asByteArray(); Esp3PacketHeader.PacketType getPacketType(); byte[] getData(); byte[] getOptionalData();  }
@Test public void testNullData() throws Exception { Esp3PacketHeader.PacketType packetType = Esp3PacketHeader.PacketType.RADIO; byte[] data = null; byte[] optData = null; byte[] expectedByteArray = createPacketAsByteArray( packetType, new byte[]{}, new byte[]{} ); Esp3Packet packet = new Esp3Packet(packetType, data, data); Assert.assertArrayEquals(expectedByteArray, packet.asByteArray()); }