ParseUtil { public static String parseUtAddrV6toIP(int[] utAddrV6) { if (utAddrV6.length != 4) { throw new IllegalArgumentException("ut_addr_v6 must have exactly 4 elements"); } if (utAddrV6[1] == 0 && utAddrV6[2] == 0 && utAddrV6[3] == 0) { if (utAddrV6[0] == 0) { return "::"; } byte[] ipv4 = ByteBuffer.allocate(4).putInt(utAddrV6[0]).array(); try { return InetAddress.getByAddress(ipv4).getHostAddress(); } catch (UnknownHostException e) { return Constants.UNKNOWN; } } byte[] ipv6 = ByteBuffer.allocate(16).putInt(utAddrV6[0]).putInt(utAddrV6[1]).putInt(utAddrV6[2]) .putInt(utAddrV6[3]).array(); try { return InetAddress.getByAddress(ipv6).getHostAddress() .replaceAll("((?:(?:^|:)0+\\b){2,}):?(?!\\S*\\b\\1:0+\\b)(\\S*)", "::$2"); } catch (UnknownHostException e) { return Constants.UNKNOWN; } } private  ParseUtil(); static long parseHertz(String hertz); static int parseLastInt(String s, int i); static long parseLastLong(String s, long li); static double parseLastDouble(String s, double d); static String parseLastString(String s); static String byteArrayToHexString(byte[] bytes); static byte[] hexStringToByteArray(String digits); static byte[] asciiStringToByteArray(String text, int length); static byte[] longToByteArray(long value, int valueSize, int length); static long strToLong(String str, int size); static long byteArrayToLong(byte[] bytes, int size); static float byteArrayToFloat(byte[] bytes, int size, int fpBits); static long unsignedIntToLong(int unsignedValue); static long unsignedLongToSignedLong(long unsignedValue); static String hexStringToString(String hexString); static int parseIntOrDefault(String s, int defaultInt); static long parseLongOrDefault(String s, long defaultLong); static long parseUnsignedLongOrDefault(String s, long defaultLong); static double parseDoubleOrDefault(String s, double defaultDouble); static long parseDHMSOrDefault(String s, long defaultLong); static String parseUuidOrDefault(String s, String defaultStr); static String getSingleQuoteStringValue(String line); static String getDoubleQuoteStringValue(String line); static String getStringBetween(String line, char c); static int getFirstIntValue(String line); static int getNthIntValue(String line, int n); static String removeMatchingString(final String original, final String toRemove); static long[] parseStringToLongArray(String s, int[] indices, int length, char delimiter); static int countStringToLongArray(String s, char delimiter); static String getTextBetweenStrings(String text, String before, String after); static long filetimeToUtcMs(long filetime, boolean local); static String parseMmDdYyyyToYyyyMmDD(String dateString); static OffsetDateTime parseCimDateTimeToOffset(String cimDateTime); static boolean filePathStartsWith(List<String> prefixList, String path); static long parseDecimalMemorySizeToBinary(String size); static Pair<String, String> parsePnPDeviceIdToVendorProductId(String pnpDeviceId); static long parseLshwResourceString(String resources); static Pair<String, String> parseLspciMachineReadable(String line); static long parseLspciMemorySize(String line); static List<Integer> parseHyphenatedIntList(String str); static String parseUtAddrV6toIP(int[] utAddrV6); static long hexStringToLong(String hexString, long defaultValue); static String removeLeadingDots(String dotPrefixedStr); static final Pattern whitespacesColonWhitespace; static final Pattern whitespaces; static final Pattern notDigits; static final Pattern startWithNotDigits; static final Pattern slash; }
@Test public void testParseUtAddrV6toIP() { int[] zero = { 0, 0, 0, 0 }; int[] loopback = { 0, 0, 0, 1 }; String v6test = "2001:db8:85a3::8a2e:370:7334"; int[] v6 = new int[4]; v6[0] = Integer.parseUnsignedInt("20010db8", 16); v6[1] = Integer.parseUnsignedInt("85a30000", 16); v6[2] = Integer.parseUnsignedInt("00008a2e", 16); v6[3] = Integer.parseUnsignedInt("03707334", 16); String v4test = "127.0.0.1"; int[] v4 = new int[4]; v4[0] = (127 << 24) + 1; assertEquals("Unspecified address failed", "::", ParseUtil.parseUtAddrV6toIP(zero)); assertEquals("Loopback address failed", "::1", ParseUtil.parseUtAddrV6toIP(loopback)); assertEquals("V6 parsing failed", v6test, ParseUtil.parseUtAddrV6toIP(v6)); assertEquals("V4 parsig failed", v4test, ParseUtil.parseUtAddrV6toIP(v4)); }