Esp2Packet implements EspPacket { @Override public byte[] asByteArray() { int size = Esp2PacketHeader.ESP2_HEADER_SIZE + data.length + ESP2_PACKET_CHECKSUM_LENGTH; 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); packetBytes[size - 1] = checksum( packetBytes, Esp2PacketHeader.ESP2_HEADER_H_SEQ_LENGTH_INDEX, size - ESP2_PACKET_CHECKSUM_LENGTH - Esp2PacketHeader.ESP2_HEADER_SYNC_BYTE_1_LENGTH - Esp2PacketHeader.ESP2_HEADER_SYNC_BYTE_2_LENGTH ); return packetBytes; } Esp2Packet(Esp2PacketHeader.PacketType packetType, byte[] data); @Override byte[] asByteArray(); Esp2PacketHeader.PacketType getPacketType(); byte[] getData(); static final byte ESP2_PACKET_DATA_LENGTH; static final int ESP2_PACKET_DATA_INDEX; static final byte ESP2_PACKET_CHECKSUM_LENGTH; static final int ESP2_PACKET_CHECKSUM_INDEX; }
@Test public void testNullData() throws Exception { Esp2PacketHeader.PacketType packetType = Esp2PacketHeader.PacketType.TCT; byte[] expectedByteArray = createPacketAsByteArray( packetType, new byte[10] ); byte[] data = null; Esp2Packet packet = new Esp2Packet(packetType, data); Assert.assertArrayEquals(expectedByteArray, packet.asByteArray()); }