Java Code Examples for java.net.Inet6Address#getByAddress()
The following examples show how to use
java.net.Inet6Address#getByAddress() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FlowLabelFilterSpecIPv6.java From netphony-network-protocols with Apache License 2.0 | 6 votes |
public void decode(byte[] bytes, int offset) { length = (int)(bytes[offset]|bytes[offset+1]); int headerSize = 4; int currentIndex = offset + headerSize; byte[] readAddress = new byte[16]; System.arraycopy(bytes,currentIndex,readAddress,0,16); try{ srcAddress = (Inet6Address) Inet6Address.getByAddress(readAddress); currentIndex = currentIndex + 16; }catch(UnknownHostException e){ // FIXME: Poner logs con respecto a excepcion } flowLabel = (int)(bytes[currentIndex+1]|bytes[currentIndex+2]|bytes[currentIndex+3]); }
Example 2
Source File: NativeSocketAddress.java From Bytecoder with Apache License 2.0 | 6 votes |
/** * Return an InetAddress to represent the value of the address in the * sin4_addr or sin6_addr fields. For IPv6 addresses, the Inet6Address is * created with the sin6_scope_id in the sockaddr_in6 structure. */ private InetAddress address(int family) { int len; int offset; int scope_id; if (family == AF_INET) { len = 4; offset = OFFSET_SIN4_ADDR; scope_id = 0; } else { len = 16; offset = OFFSET_SIN6_ADDR; scope_id = UNSAFE.getInt(address + OFFSET_SIN6_SCOPE_ID); } byte[] bytes = new byte[len]; UNSAFE.copyMemory(null, address + offset, bytes, ARRAY_BASE_OFFSET, len); try { if (scope_id == 0) { return InetAddress.getByAddress(bytes); } else { return Inet6Address.getByAddress(null, bytes, scope_id); } } catch (UnknownHostException e) { throw new InternalError(e); } }
Example 3
Source File: ScopeIPv6.java From netphony-network-protocols with Apache License 2.0 | 6 votes |
@Override public void decode(byte[] bytes, int offset) { length = (int)(bytes[offset]|bytes[offset+1]); int headerSize = 4; int unprocessedBytes = length - headerSize; int currentIndex = offset+headerSize; while(unprocessedBytes > 0){ byte[] readAddress = new byte[16]; System.arraycopy(bytes,currentIndex,readAddress,0,16); try{ Inet6Address newAddress = (Inet6Address) Inet6Address.getByAddress(readAddress); addSourceIpAddress(newAddress); currentIndex = currentIndex + 16; unprocessedBytes = unprocessedBytes - 16; }catch(UnknownHostException e){ // FIXME: Poner logs con respecto a excepcion } } }
Example 4
Source File: ServerUnicastOption.java From dhcp4j with Apache License 2.0 | 5 votes |
@Nonnull public InetAddress getIp() { ByteBuffer buf = ByteBuffer.wrap(getData()); final byte[] dst = new byte[16]; buf.get(dst, 0, 16); try { return Inet6Address.getByAddress(dst); } catch (UnknownHostException e) { // This can happen only if the IP byte array is of illegal length throw new IllegalStateException("Illegal IP address", e); } }
Example 5
Source File: TestInetAddressServlet.java From appengine-java-vm-runtime with Apache License 2.0 | 5 votes |
/** * Get instances of InetAddress by calling InetAddress.getByAddress(String, byte[]), * and passing in a 16-byte address. Then test their properties. * @param response HttpServletResponse used to return failure message * @throws IOException If method being tested throws this. * @throws AssertionFailedException If an assertion fails */ private static void testGetByAddress6(HttpServletResponse response) throws IOException, AssertionFailedException { //Passing in 17-bytes should throw an exception byte[] badAddr = new byte[17]; UnknownHostException caught = null; try { InetAddress.getByAddress(badAddr); } catch (UnknownHostException e) { caught = e; } assertNotNull("caught", caught, response); //Next we try an Ipv4-apped address byte[] mappedAddr = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 127, 0, 0, 1}; //square brackets should get stripped in name Inet4Address inet4Addr = (Inet4Address) InetAddress.getByAddress("[localhost]", mappedAddr); testLocalHost(inet4Addr, response); //Now we test a real IPv6 address byte[] addr = new byte[] {0x10, (byte) 0x80, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0x20, 0xC, 0x41, 0x7A}; String addressString = "1080:0:0:0:8:800:200C:417A"; String hostName = "my.host"; Inet6Address inet6Addr = (Inet6Address) InetAddress.getByAddress(hostName, addr); testNameAndAddress6(hostName, addressString, addr, 0, inet6Addr, response); //Now we test a real IPv6 address with a scope ID int scopeID = 17; addressString = "1080:0:0:0:8:800:200C:417A%17"; inet6Addr = Inet6Address.getByAddress(hostName, addr, scopeID); testNameAndAddress6(hostName, addressString, addr, scopeID, inet6Addr, response); }
Example 6
Source File: NetflowV9Decoder.java From datacollector with Apache License 2.0 | 5 votes |
public static Field getIPV6AddressAsString(byte[] bytes) throws OnRecordErrorException { try { InetAddress addr = Inet6Address.getByAddress(bytes); return Field.create(addr.getHostAddress()); } catch (UnknownHostException e) { throw new OnRecordErrorException(Errors.NETFLOW_13, e.getClass().getSimpleName(), e.getMessage(), e); } }
Example 7
Source File: SourceAddressChannelHandlerTest.java From zuul with Apache License 2.0 | 5 votes |
@Test public void mapsIpv4AddressFromIpv6Address() throws Exception { // Can't think of a reason why this would ever come up, but testing it just in case. // ::ffff:127.0.0.1 Inet6Address address = Inet6Address.getByAddress( "localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, 127, 0, 0, 1}, -1); assertEquals(0, address.getScopeId()); String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080)); assertEquals("127.0.0.1", addressString); }
Example 8
Source File: ServerUnicastOption.java From dhcp4j with Apache License 2.0 | 5 votes |
@Nonnull public InetAddress getIp() { ByteBuffer buf = ByteBuffer.wrap(getData()); final byte[] dst = new byte[16]; buf.get(dst, 0, 16); try { return Inet6Address.getByAddress(dst); } catch (UnknownHostException e) { // This can happen only if the IP byte array is of illegal length throw new IllegalStateException("Illegal IP address", e); } }
Example 9
Source File: IPv6AddressRROSubobject.java From netphony-network-protocols with Apache License 2.0 | 5 votes |
/** * Decodes the body of the SubObject */ public void decode(){ byte[] ipadd=new byte[16]; System.arraycopy(this.subobject_bytes,2, ipadd, 0, 16); try { ipv6address=(Inet6Address)Inet6Address.getByAddress(ipadd); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } prefix=(int)this.subobject_bytes[18]; }
Example 10
Source File: IaAddressOption.java From dhcp4j with Apache License 2.0 | 5 votes |
@Nonnull public InetAddress getIp() { ByteBuffer buf = ByteBuffer.wrap(getData()); final byte[] dst = new byte[16]; buf.get(dst, 0, 16); try { return Inet6Address.getByAddress(dst); } catch (UnknownHostException e) { // This can happen only if the IP byte array is of illegal length throw new IllegalStateException("Illegal IP address", e); } }
Example 11
Source File: IPv6AddressTest.java From java-ipv6 with Apache License 2.0 | 5 votes |
@Test public void constructFromInet6AddressWithScopeId() throws UnknownHostException { byte[] bytes = fromString("2001:db8:85a3::8a2e:370:7334").toByteArray(); final InetAddress inetAddress = Inet6Address.getByAddress("host", bytes, 12); assertEquals("2001:db8:85a3::8a2e:370:7334", fromInetAddress(inetAddress).toString()); }
Example 12
Source File: Inet6AddressSerializationTest.java From TencentKona-8 with GNU General Public License v2.0 | 4 votes |
static void testSerializedLo0Inet6Address() throws IOException { System.err.println("\n testSerializedLo0Inet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedLo0Inet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedLo0Inet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } // displayExpectedInet6Address(expectedInet6Address); byte[] serializedAddress = SerialData_ifname_lo0; try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockLo0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockLo0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockLo0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockLo0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockLo0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 13
Source File: Inet6AddressSerializationTest.java From jdk8u_jdk with GNU General Public License v2.0 | 4 votes |
static void testSerializedLo0Inet6Address() throws IOException { System.err.println("\n testSerializedLo0Inet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedLo0Inet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedLo0Inet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } // displayExpectedInet6Address(expectedInet6Address); byte[] serializedAddress = SerialData_ifname_lo0; try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockLo0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockLo0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockLo0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockLo0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockLo0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 14
Source File: ErrorSpecIPv6.java From netphony-network-protocols with Apache License 2.0 | 4 votes |
@Override public void decode(byte[] bytes, int offset) { length = (int)(bytes[offset]|bytes[offset+1]); int headerSize = 4; int unprocessedBytes = length - headerSize; int currentIndex = offset+headerSize; if(unprocessedBytes > 0){ byte[] readAddress = new byte[16]; System.arraycopy(bytes,currentIndex,readAddress,0,16); try{ errorNodeAddress = (Inet6Address) Inet6Address.getByAddress(readAddress); currentIndex = currentIndex + 16; unprocessedBytes = unprocessedBytes - 16; }catch(UnknownHostException e){ // FIXME: Poner logs con respecto a excepcion } if(unprocessedBytes > 0){ flags = (int)(bytes[currentIndex]); currentIndex = currentIndex + 1; unprocessedBytes = unprocessedBytes - 1; if(unprocessedBytes > 0){ errorCode = (int)(bytes[currentIndex]); currentIndex = currentIndex + 1; unprocessedBytes = unprocessedBytes - 1; if(unprocessedBytes > 0){ errorValue = (int)(bytes[currentIndex]|bytes[currentIndex+1]); currentIndex = currentIndex + 2; unprocessedBytes = unprocessedBytes - 2; }else{ // Malformed Object } }else{ // Malformed Object } }else{ // Malformed Object } }// Malformed Object }
Example 15
Source File: Inet6AddressSerializationTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
static void testSerializedLo0Inet6Address() throws IOException { System.err.println("\n testSerializedLo0Inet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedLo0Inet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedLo0Inet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } // displayExpectedInet6Address(expectedInet6Address); byte[] serializedAddress = SerialData_ifname_lo0; try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockLo0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockLo0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockLo0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockLo0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockLo0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 16
Source File: Inet6AddressSerializationTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 4 votes |
static void testSerializedE1000gInet6Address() throws IOException { System.err.println("\n testSerializedE1000gInet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface .getByName(NETWORK_IF_E1000G0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedE1000gInet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedE1000gInet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } byte[] serializedAddress = SerialData_ifname_e1000g0; // displayExpectedInet6Address(expectedInet6Address); try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockE1000g0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockE1000g0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockE1000g0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 17
Source File: Inet6AddressSerializationTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
static void testSerializedE1000gInet6Address() throws IOException { System.err.println("\n testSerializedE1000gInet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface .getByName(NETWORK_IF_E1000G0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedE1000gInet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedE1000gInet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } byte[] serializedAddress = SerialData_ifname_e1000g0; // displayExpectedInet6Address(expectedInet6Address); try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockE1000g0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockE1000g0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockE1000g0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 18
Source File: Inet6AddressSerializationTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void testSerializedLo0Inet6Address() throws IOException { System.err.println("\n testSerializedLo0Inet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface.getByName(NETWORK_IF_LO0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedLo0Inet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedLo0Inet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress(LOCALHOSTNAME, LOOPBACKIPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } // displayExpectedInet6Address(expectedInet6Address); byte[] serializedAddress = SerialData_ifname_lo0; try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockLo0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockLo0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockLo0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockLo0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockLo0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockLo0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockLo0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 19
Source File: Inet6AddressSerializationTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
static void testSerializedE1000gInet6Address() throws IOException { System.err.println("\n testSerializedE1000gInet6Address: enter \n"); boolean testWithNetIf = true; boolean useMockInet6Address = false; NetworkInterface testNetIf = NetworkInterface .getByName(NETWORK_IF_E1000G0); Inet6Address expectedInet6Address = null; if (testNetIf != null) { System.err .println("\n testSerializedE1000gInet6Address: using netif \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, testNetIf); } catch (UnknownHostException ukhEx) { ukhEx.printStackTrace(); testWithNetIf = true; useMockInet6Address = true; } } else { System.err .println("\n testSerializedE1000gInet6Address: using index \n"); try { expectedInet6Address = Inet6Address.getByAddress( E1000G0HOSTNAME, E1000G0IPV6ADDRESS, SCOPE_ID_ZERO); } catch (UnknownHostException ukhEx1) { ukhEx1.printStackTrace(); useMockInet6Address = true; } testWithNetIf = false; } byte[] serializedAddress = SerialData_ifname_e1000g0; // displayExpectedInet6Address(expectedInet6Address); try (ByteArrayInputStream bis = new ByteArrayInputStream( serializedAddress); ObjectInputStream oin = new ObjectInputStream(bis)) { Inet6Address deserializedIPV6Addr = (Inet6Address) oin.readObject(); System.err.println("Deserialized Inet6Address " + deserializedIPV6Addr); if (!useMockInet6Address) { assertHostNameEqual(expectedInet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( expectedInet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getBareHostAddress(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(expectedInet6Address.getAddress(), deserializedIPV6Addr.getAddress()); assertScopeIdEqual(expectedInet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); if (testWithNetIf) { assertNetworkInterfaceEqual( expectedInet6Address.getScopedInterface(), deserializedIPV6Addr.getScopedInterface()); } else { assertNetworkInterfaceEqual(null, deserializedIPV6Addr.getScopedInterface()); } } else { // use MockLo0Inet6Address assertHostNameEqual(MockE1000g0Inet6Address.getHostName(), deserializedIPV6Addr.getHostName()); if (testWithNetIf) { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddress(), deserializedIPV6Addr.getHostAddress()); } else { assertHostAddressEqual( MockE1000g0Inet6Address.getHostAddressWithIndex(), deserializedIPV6Addr.getHostAddress()); } assertAddressEqual(MockE1000g0Inet6Address.getAddress(), deserializedIPV6Addr.getAddress()); if (testWithNetIf) { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeId(), deserializedIPV6Addr.getScopeId()); } else { assertScopeIdEqual(MockE1000g0Inet6Address.getScopeZero(), deserializedIPV6Addr.getScopeId()); } assertNetworkInterfaceNameEqual( MockE1000g0Inet6Address.getScopeIfName(), deserializedIPV6Addr.getScopedInterface()); } } catch (Exception e) { System.err.println("Exception caught during deserialization"); failed = true; e.printStackTrace(); } }
Example 20
Source File: AbstractXBeeDevice.java From xbee-java with Mozilla Public License 2.0 | 3 votes |
/** * Returns the destination IPv6 address. * * @return The configured destination IPv6 address. * * @throws TimeoutException if there is a timeout reading the IPv6 * destination address. * @throws XBeeException if there is any other XBee related exception. * * @see #setIPv6DestinationAddress(Inet6Address) * @see java.net.Inet6Address * * @since 1.2.1 */ public Inet6Address getIPv6DestinationAddress() throws TimeoutException, XBeeException { try { return (Inet6Address) Inet6Address.getByAddress(getParameter("DL")); } catch (UnknownHostException e) { throw new XBeeException(e); } }