Merge "Add some useful helpers and constants."

am: ed8d236319

Change-Id: I3a934e19ece91bf5957e4c5fa289bbc1a6242915
This commit is contained in:
Chalard Jean
2017-10-06 09:48:32 +00:00
committed by android-build-merger
3 changed files with 37 additions and 0 deletions

View File

@@ -93,6 +93,10 @@ public final class BitUtils {
return s & 0xffff;
}
public static int uint16(byte hi, byte lo) {
return ((hi & 0xff) << 8) | (lo & 0xff);
}
public static long uint32(int i) {
return i & 0xffffffffL;
}

View File

@@ -106,6 +106,20 @@ public final class NetworkConstants {
public static final int RFC7421_PREFIX_LENGTH = 64;
public static final int RFC6177_MIN_PREFIX_LENGTH = 48;
/**
* ICMP common (v4/v6) constants.
*
* See also:
* - https://tools.ietf.org/html/rfc792
* - https://tools.ietf.org/html/rfc4443
*/
public static final int ICMP_HEADER_TYPE_OFFSET = 0;
public static final int ICMP_HEADER_CODE_OFFSET = 1;
public static final int ICMP_HEADER_CHECKSUM_OFFSET = 2;
public static final int ICMP_ECHO_IDENTIFIER_OFFSET = 4;
public static final int ICMP_ECHO_SEQUENCE_NUMBER_OFFSET = 6;
public static final int ICMP_ECHO_DATA_OFFSET = 8;
/**
* ICMPv6 constants.
*

View File

@@ -55,6 +55,25 @@ public class BitUtilsTest {
assertEquals(65535, uint16((short)65535));
}
@Test
public void testUnsignedShortComposition() {
byte b0 = 0;
byte b1 = 1;
byte b2 = 2;
byte b10 = 10;
byte b16 = 16;
byte b128 = -128;
byte b224 = -32;
byte b255 = -1;
assertEquals(0x0000, uint16(b0, b0));
assertEquals(0xffff, uint16(b255, b255));
assertEquals(0x0a01, uint16(b10, b1));
assertEquals(0x8002, uint16(b128, b2));
assertEquals(0x01ff, uint16(b1, b255));
assertEquals(0x80ff, uint16(b128, b255));
assertEquals(0xe010, uint16(b224, b16));
}
@Test
public void testUnsignedIntWideningConversions() {
assertEquals(0, uint32(0));