### Question:
PcapReader implements Iterable<Packet> { protected byte[] readPayload(byte[] packetData, int payloadDataStart, int payloadLength) { if (payloadLength < 0) { LOG.warn("Malformed packet - negative payload length. Returning empty payload."); return new byte[0]; } if (payloadDataStart > packetData.length) { LOG.warn("Payload start (" + payloadDataStart + ") is larger than packet data (" + packetData.length + "). Returning empty payload."); return new byte[0]; } if (payloadDataStart + payloadLength > packetData.length) { if (payloadDataStart + payloadLength <= snapLen) LOG.warn("Payload length field value (" + payloadLength + ") is larger than available packet data (" + (packetData.length - payloadDataStart) + "). Packet may be corrupted. Returning only available data."); payloadLength = packetData.length - payloadDataStart; } byte[] data = new byte[payloadLength]; System.arraycopy(packetData, payloadDataStart, data, 0, payloadLength); return data; } PcapReader(DataInputStream is); protected  PcapReader(LinkType lt); @Override Iterator<Packet> iterator(); static final Log LOG; static final long MAGIC_NUMBER; static final int HEADER_SIZE; static final int PCAP_HEADER_SNAPLEN_OFFSET; static final int PCAP_HEADER_LINKTYPE_OFFSET; static final int PACKET_HEADER_SIZE; static final int TIMESTAMP_OFFSET; static final int TIMESTAMP_MICROS_OFFSET; static final int CAP_LEN_OFFSET; static final int ETHERNET_HEADER_SIZE; static final int ETHERNET_TYPE_OFFSET; static final int ETHERNET_TYPE_IP; static final int ETHERNET_TYPE_IPV6; static final int ETHERNET_TYPE_8021Q; static final int SLL_HEADER_BASE_SIZE; static final int SLL_ADDRESS_LENGTH_OFFSET; static final int IPV6_HEADER_SIZE; static final int IP_VHL_OFFSET; static final int IP_TTL_OFFSET; static final int IP_TOTAL_LEN_OFFSET; static final int IPV6_PAYLOAD_LEN_OFFSET; static final int IPV6_HOPLIMIT_OFFSET; static final int IP_PROTOCOL_OFFSET; static final int IPV6_NEXTHEADER_OFFSET; static final int IP_SRC_OFFSET; static final int IPV6_SRC_OFFSET; static final int IP_DST_OFFSET; static final int IPV6_DST_OFFSET; static final int IP_ID_OFFSET; static final int IPV6_ID_OFFSET; static final int IP_FLAGS_OFFSET; static final int IPV6_FLAGS_OFFSET; static final int IP_FRAGMENT_OFFSET; static final int IPV6_FRAGMENT_OFFSET; static final int UDP_HEADER_SIZE; static final int PROTOCOL_HEADER_SRC_PORT_OFFSET; static final int PROTOCOL_HEADER_DST_PORT_OFFSET; static final int PROTOCOL_HEADER_TCP_SEQ_OFFSET; static final int PROTOCOL_HEADER_TCP_ACK_OFFSET; static final int TCP_HEADER_DATA_OFFSET; static final String PROTOCOL_ICMP; static final String PROTOCOL_TCP; static final String PROTOCOL_UDP; static final String PROTOCOL_FRAGMENT; public byte[] pcapHeader; public byte[] pcapPacketHeader; public byte[] packetData; }### Answer:
@Test public void readPayload() { byte[] stringBytes = new String("foo bar").getBytes(); byte[] payload = reader.readPayload(stringBytes, 4, stringBytes.length-4); assertEquals("bar", new String(payload)); }
@Test public void readPayloadBrokenOffset() { byte[] payload = reader.readPayload(new byte[1], 2, 1); assertTrue(0 == payload.length); }