@Ignore @Test public void benchComputeChecksum() { ByteBuffer buffer = createMockHeaders(); IPv4Header header = new IPv4Header(buffer); long start = System.currentTimeMillis(); for (int i = 0; i < 5000000; ++i) { header.computeChecksum(); } long duration = System.currentTimeMillis() - start; System.out.println("5000000 IP checksums: " + duration + "ms"); }
public void computeChecksum() { setChecksum((short) 0); byte[] rawArray = raw.array(); int rawArrayOffset = raw.arrayOffset(); int sum = 0; for (int i = 0; i < headerLength / 2; ++i) { sum += (rawArray[rawArrayOffset + 2 * i] & 0xff) << 8 | (rawArray[rawArrayOffset + 2 * i + 1] & 0xff); } while ((sum & ~0xffff) != 0) { sum = (sum & 0xffff) + (sum >> 16); } setChecksum((short) ~sum); }
IPv4Header { public void computeChecksum() { setChecksum((short) 0); byte[] rawArray = raw.array(); int rawArrayOffset = raw.arrayOffset(); int sum = 0; for (int i = 0; i < headerLength / 2; ++i) { sum += (rawArray[rawArrayOffset + 2 * i] & 0xff) << 8 | (rawArray[rawArrayOffset + 2 * i + 1] & 0xff); } while ((sum & ~0xffff) != 0) { sum = (sum & 0xffff) + (sum >> 16); } setChecksum((short) ~sum); } }
IPv4Header { public void computeChecksum() { setChecksum((short) 0); byte[] rawArray = raw.array(); int rawArrayOffset = raw.arrayOffset(); int sum = 0; for (int i = 0; i < headerLength / 2; ++i) { sum += (rawArray[rawArrayOffset + 2 * i] & 0xff) << 8 | (rawArray[rawArrayOffset + 2 * i + 1] & 0xff); } while ((sum & ~0xffff) != 0) { sum = (sum & 0xffff) + (sum >> 16); } setChecksum((short) ~sum); } IPv4Header(ByteBuffer raw); }
IPv4Header { public void computeChecksum() { setChecksum((short) 0); byte[] rawArray = raw.array(); int rawArrayOffset = raw.arrayOffset(); int sum = 0; for (int i = 0; i < headerLength / 2; ++i) { sum += (rawArray[rawArrayOffset + 2 * i] & 0xff) << 8 | (rawArray[rawArrayOffset + 2 * i + 1] & 0xff); } while ((sum & ~0xffff) != 0) { sum = (sum & 0xffff) + (sum >> 16); } setChecksum((short) ~sum); } IPv4Header(ByteBuffer raw); boolean isSupported(); Protocol getProtocol(); int getHeaderLength(); int getTotalLength(); void setTotalLength(int totalLength); int getSource(); int getDestination(); void setSource(int source); void setDestination(int destination); void swapSourceAndDestination(); ByteBuffer getRaw(); IPv4Header copyTo(ByteBuffer target); IPv4Header copy(); void computeChecksum(); short getChecksum(); static int readVersion(ByteBuffer buffer); static int readLength(ByteBuffer buffer); }
IPv4Header { public void computeChecksum() { setChecksum((short) 0); byte[] rawArray = raw.array(); int rawArrayOffset = raw.arrayOffset(); int sum = 0; for (int i = 0; i < headerLength / 2; ++i) { sum += (rawArray[rawArrayOffset + 2 * i] & 0xff) << 8 | (rawArray[rawArrayOffset + 2 * i + 1] & 0xff); } while ((sum & ~0xffff) != 0) { sum = (sum & 0xffff) + (sum >> 16); } setChecksum((short) ~sum); } IPv4Header(ByteBuffer raw); boolean isSupported(); Protocol getProtocol(); int getHeaderLength(); int getTotalLength(); void setTotalLength(int totalLength); int getSource(); int getDestination(); void setSource(int source); void setDestination(int destination); void swapSourceAndDestination(); ByteBuffer getRaw(); IPv4Header copyTo(ByteBuffer target); IPv4Header copy(); void computeChecksum(); short getChecksum(); static int readVersion(ByteBuffer buffer); static int readLength(ByteBuffer buffer);  }