Creates a reverse map name corresponding to an address contained in an array of 4 bytes (for an IPv4 address) or 16 bytes (for an IPv6 address). @param addr The address from which to build a name. @return The name corresponding to the address in the reverse map.
(byte [] addr)
| 28 | * @return The name corresponding to the address in the reverse map. |
| 29 | */ |
| 30 | public static Name |
| 31 | fromAddress(byte [] addr) { |
| 32 | if (addr.length != 4 && addr.length != 16) |
| 33 | throw new IllegalArgumentException("array must contain " + |
| 34 | "4 or 16 elements"); |
| 35 | |
| 36 | StringBuffer sb = new StringBuffer(); |
| 37 | if (addr.length == 4) { |
| 38 | for (int i = addr.length - 1; i >= 0; i--) { |
| 39 | sb.append(addr[i] & 0xFF); |
| 40 | if (i > 0) |
| 41 | sb.append("."); |
| 42 | } |
| 43 | } else { |
| 44 | int [] nibbles = new int[2]; |
| 45 | for (int i = addr.length - 1; i >= 0; i--) { |
| 46 | nibbles[0] = (addr[i] & 0xFF) >> 4; |
| 47 | nibbles[1] = (addr[i] & 0xFF) & 0xF; |
| 48 | for (int j = nibbles.length - 1; j >= 0; j--) { |
| 49 | sb.append(Integer.toHexString(nibbles[j])); |
| 50 | if (i > 0 || j > 0) |
| 51 | sb.append("."); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | if (addr.length == 4) |
| 58 | return Name.fromString(sb.toString(), inaddr4); |
| 59 | else |
| 60 | return Name.fromString(sb.toString(), inaddr6); |
| 61 | } |
| 62 | catch (TextParseException e) { |
| 63 | throw new IllegalStateException("name cannot be invalid"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Creates a reverse map name corresponding to an address contained in |
no test coverage detected