List of usage examples for java.net InetAddress getByAddress
public static InetAddress getByAddress(byte[] addr) throws UnknownHostException
From source file:org.commoncrawl.io.internal.NIOHttpConnection.java
public void open() throws IOException { if (_state != State.IDLE) throw new IOException("Invalid State"); _openTime = System.currentTimeMillis(); if (_url.getHost().length() == 0 || _method.length() == 0 || _httpVersionString.length() == 0) throw new IOException("Invalid Base HTTP Parameters Specified"); setState(State.AWAITING_RESOLUTION, null); InetSocketAddress socketAddress = getProxyServer(); if (socketAddress == null) { InetAddress addressToConnectTo = null; // get host name ... String hostName = _url.getHost(); // figure out if it url is an explicit IP Address ... byte[] ipAddress = IPAddressUtils.textToNumericFormatV4(hostName); // if this IS an IP address (vs a hostname that needs to be resolved...) if (ipAddress != null) { // set address to connect to ... addressToConnectTo = InetAddress.getByAddress(ipAddress); }/*from w ww . j av a 2 s. c o m*/ // now if address to connect to is still null... if (addressToConnectTo == null) { // see if someone overloaded resolved address ... addressToConnectTo = getResolvedAddress(); } // now if address to connect to is not null, convert it to a socket address if (addressToConnectTo != null) { //TODO: FIX THIS FOR HTTPS socketAddress = new InetSocketAddress(addressToConnectTo, (_url.getPort() == -1) ? 80 : _url.getPort()); } } // now, if socket address is NOT null, directly connect to the specified address, bypassing dns lookup if (socketAddress != null) { startConnect(socketAddress); } // otherwise delegate to resolver (to figure out ip address) else { LOG.info("Sending Host:" + _url.getHost() + " to resolver"); _resolver.resolve(this, _url.getHost(), false, true, _dnsTimeout); } }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testResolveLocalAddress() throws Exception { final InternalAddressResolver addressResolver = new InternalAddressResolver(schemePortResolver, dnsResolver);/*from w w w .j av a2 s .c o m*/ final HttpHost target = new HttpHost("localhost"); final byte[] ip = new byte[] { 10, 0, 0, 10 }; final HttpRoute route = new HttpRoute(target, InetAddress.getByAddress(ip), false); final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveLocalAddress(route); Assert.assertNotNull(address); Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); Assert.assertEquals(0, address.getPort()); }
From source file:com.jkoolcloud.tnt4j.utils.Utils.java
/** * Resolves the specified IP Address to a host name. If no IP Address is * given, then resolves local host name. * * @param hostIp//from ww w . j a v a 2s.c om * host IP Address to resolve * @return host name */ public static String resolveAddressToHostName(byte[] hostIp) { String hostName = null; InetAddress host; try { if (hostIp == null || hostIp.length == 0) host = InetAddress.getLocalHost(); else host = InetAddress.getByAddress(hostIp); hostName = host.getHostName(); } catch (UnknownHostException e) { } return hostName; }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testResolveRemoteAddress() throws Exception { final InternalAddressResolver addressResolver = new InternalAddressResolver(schemePortResolver, dnsResolver);/*w ww . j a v a 2 s . co m*/ final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); Mockito.when(schemePortResolver.resolve(target)).thenReturn(123); final byte[] ip = new byte[] { 10, 0, 0, 10 }; Mockito.when(dnsResolver.resolve("somehost")) .thenReturn(new InetAddress[] { InetAddress.getByAddress(ip) }); final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route); Assert.assertNotNull(address); Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); Assert.assertEquals(123, address.getPort()); }
From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java
@Test public void testResolveRemoteAddressViaProxy() throws Exception { final InternalAddressResolver addressResolver = new InternalAddressResolver(schemePortResolver, dnsResolver);// ww w. j a va2 s . com final HttpHost target = new HttpHost("somehost", 80); final HttpHost proxy = new HttpHost("someproxy"); final HttpRoute route = new HttpRoute(target, null, proxy, false); Mockito.when(schemePortResolver.resolve(proxy)).thenReturn(8888); final byte[] ip = new byte[] { 10, 0, 0, 10 }; Mockito.when(dnsResolver.resolve("someproxy")) .thenReturn(new InetAddress[] { InetAddress.getByAddress(ip) }); final InetSocketAddress address = (InetSocketAddress) addressResolver.resolveRemoteAddress(route); Assert.assertNotNull(address); Assert.assertEquals(InetAddress.getByAddress(ip), address.getAddress()); Assert.assertEquals(8888, address.getPort()); }
From source file:org.javamrt.mrt.BGPFileReader.java
private MRTRecord parseBgp4Entry(int AFI) throws Exception { /*//from ww w . ja v a2s.c o m * TODO: this doesn't work as expected yet */ if (Debug.compileDebug) { Debug.debug("in parseBgp4Entry\n"); Debug.dump(record); } int addrSize = (AFI == MRTConstants.AFI_IPv4) ? 4 : 16; int view = RecordAccess.getU16(record, 0); int status = RecordAccess.getU16(record, 2); long rtime = RecordAccess.getU32(record, 4); int af = RecordAccess.getU16(record, 8); int safi = RecordAccess.getU8(record, 10); int nhl = RecordAccess.getU8(record, 11); if (Debug.compileDebug) { Debug.debug("int view = %d\n", view); Debug.debug("int status = %d\n", status); Debug.debug("long rtime = %d\n", rtime); Debug.debug("int af = %d\n", af); Debug.debug("int safi = %d\n", safi); Debug.debug("int nhl = %d\n", nhl); } int offset = 12; InetAddress nextHop = InetAddress.getByAddress(RecordAccess.getBytes(record, offset, addrSize)); offset += addrSize; Nlri prefix = new Nlri(record, offset, AFI); offset += prefix.getOffset(); Attributes attrs = new Attributes(record, record.length - offset, offset); ASPath aspath = attrs.getASPath(); AS neighborAS = null; if (aspath != null) neighborAS = aspath.get(0); return new TableDumpv2(view, 1, prefix, rtime, nextHop, neighborAS, attrs); }
From source file:com.google.android.apps.tvremote.DeviceFinder.java
private InetAddress getBroadcastAddress() throws IOException { WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); DhcpInfo dhcp = wifi.getDhcpInfo();//from w w w.jav a2 s . c o m int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask; byte[] quads = new byte[4]; for (int k = 0; k < 4; k++) { quads[k] = (byte) ((broadcast >> k * 8) & 0xFF); } return InetAddress.getByAddress(quads); }
From source file:com.mobilyzer.util.PhoneUtils.java
public String getWifiIpAddress() { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); if (wifiInfo != null) { int ip = wifiInfo.getIpAddress(); if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { ip = Integer.reverseBytes(ip); }//from w w w. j a va2 s .c om byte[] bytes = BigInteger.valueOf(ip).toByteArray(); String address; try { address = InetAddress.getByAddress(bytes).getHostAddress(); return address; } catch (UnknownHostException e) { e.printStackTrace(); } } return null; }
From source file:org.apache.cassandra.db.clock.IncrementCounterContextTest.java
@Test public void testCleanNodeCounts() throws UnknownHostException { byte[] bytes = new byte[HEADER_LENGTH + (4 * stepLength)]; icc.writeElementAtStepOffset(bytes, 0, FBUtilities.toByteArray(1), 1L); icc.writeElementAtStepOffset(bytes, 1, FBUtilities.toByteArray(2), 2L); icc.writeElementAtStepOffset(bytes, 2, FBUtilities.toByteArray(4), 3L); icc.writeElementAtStepOffset(bytes, 3, FBUtilities.toByteArray(8), 4L); assert 4 == FBUtilities.byteArrayToInt(bytes, HEADER_LENGTH + 2 * stepLength); assert 3L == FBUtilities.byteArrayToLong(bytes, HEADER_LENGTH + 2 * stepLength + idLength); bytes = icc.cleanNodeCounts(bytes, InetAddress.getByAddress(FBUtilities.toByteArray(4))); // node: 0.0.0.4 should be removed assert (HEADER_LENGTH + (3 * stepLength)) == bytes.length; // other nodes should be unaffected assert 1 == FBUtilities.byteArrayToInt(bytes, HEADER_LENGTH + 0 * stepLength); assert 1L == FBUtilities.byteArrayToLong(bytes, HEADER_LENGTH + 0 * stepLength + idLength); assert 2 == FBUtilities.byteArrayToInt(bytes, HEADER_LENGTH + 1 * stepLength); assert 2L == FBUtilities.byteArrayToLong(bytes, HEADER_LENGTH + 1 * stepLength + idLength); assert 8 == FBUtilities.byteArrayToInt(bytes, HEADER_LENGTH + 2 * stepLength); assert 4L == FBUtilities.byteArrayToLong(bytes, HEADER_LENGTH + 2 * stepLength + idLength); }
From source file:slash.navigation.mapview.browser.BrowserMapView.java
private ServerSocket createCallbackListenerServerSocket() { try {/* ww w .ja v a2 s .c om*/ ServerSocket serverSocket = new ServerSocket(0, 0, InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 })); serverSocket.setSoTimeout(1000); int port = serverSocket.getLocalPort(); log.info("Map listens on port " + port + " for callbacks"); setCallbackListenerPort(port); return serverSocket; } catch (IOException e) { log.severe("Cannot open callback listener socket: " + e); return null; } }