Example usage for java.net InetAddress getByAddress

List of usage examples for java.net InetAddress getByAddress

Introduction

In this page you can find the example usage for java.net InetAddress getByAddress.

Prototype

public static InetAddress getByAddress(byte[] addr) throws UnknownHostException 

Source Link

Document

Returns an InetAddress object given the raw IP address .

Usage

From source file:Main.java

private static InetAddress getBroadcastAddress(DhcpInfo mDhcpInfo) throws UnknownHostException {
    int broadcast = (mDhcpInfo.ipAddress & mDhcpInfo.netmask) | ~mDhcpInfo.netmask;
    byte[] quads = new byte[4];
    try {//from  www . j a  v a 2s.  c  om
        for (int k = 0; k < 4; k++)
            quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return InetAddress.getByAddress(quads);
}

From source file:org.namelessrom.devicecontrol.net.NetworkInfo.java

public static String getWifiIp() {
    final WifiManager wifiManager = (WifiManager) Application.get().getSystemService(Context.WIFI_SERVICE);
    int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
    if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
        ipAddress = Integer.reverseBytes(ipAddress);
    }/*from  www  .j a  v  a2s . co m*/

    final byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();

    String ipAddressString;
    try {
        ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
    } catch (UnknownHostException ex) {
        ipAddressString = "0.0.0.0";
    }

    return ipAddressString;
}

From source file:com.aqnote.app.wifianalyzer.wifi.model.WiFiUtils.java

public static String convertIpAddress(int ipAddress) {
    try {//from   w  ww. j ava 2 s.co  m
        byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
        ArrayUtils.reverse(bytes);
        return InetAddress.getByAddress(bytes).getHostAddress();
    } catch (Exception e) {
        return StringUtils.EMPTY;
    }
}

From source file:eu.operando.operandoapp.wifi.model.WiFiUtils.java

public static String convertIpAddress(int ipAddress) {
    byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray();
    ArrayUtils.reverse(bytes);// w  w  w.  ja va2 s.c om
    try {
        return InetAddress.getByAddress(bytes).getHostAddress();
    } catch (UnknownHostException e) {
        return StringUtils.EMPTY;
    }
}

From source file:org.apache.tajo.util.GeoIPUtil.java

/** Converts binary(byte array) to country code.
 *  In case of IPv4, it is 4 bytes length.
 *
 *  @param host ip address as byte array type by network byte order(big endian)
 *//*from w  w w. j a va 2s .c  o m*/
public static String getCountryCode(byte[] host) {
    InetAddress addr;

    try {
        addr = InetAddress.getByAddress(host);
    } catch (UnknownHostException e) {
        LOG.error("Unknown host: " + IPconvertUtil.bytes2ipstr(host));
        return "";
    }

    return lookup.getCountry(addr).getCode();
}

From source file:org.anarres.simplexml.serializers.spring.SimpleXmlSerializersConfigurationTest.java

@Test
public void testPersister() throws Exception {
    Persister persister = factory.newPersister();
    LOG.info("Serializer is " + persister);
    PersisterTestUtils.testSerialization(persister, new int[] { 1, 2, 3, 4, 5 });
    PersisterTestUtils.testSerialization(persister, InetAddress.getByAddress(new byte[] { 1, 2, 3, 4 }));

    Bean bean = new Bean();
    bean.aAddress = InetAddress.getByAddress(new byte[] { 3, 4, 5, 6 });
    bean.eAddress = InetAddress.getByAddress(new byte[] { 3, 4, 5, 7 });
    bean.aUri = URI.create("mailto:nobody@localhost");
    bean.eUri = URI.create("mailto:somebody@localhost");
    PersisterTestUtils.testSerialization(persister, bean);
}

From source file:com.edmunds.etm.loadbalancer.impl.OrderedInet4Address.java

public Inet4Address toInet4Address() {
    try {/*from   w ww.j  a v  a2s .  c om*/
        return (Inet4Address) InetAddress.getByAddress(address);
    } catch (UnknownHostException e) {
        // Never Happen!
        throw new RuntimeException(e);
    }
}

From source file:com.ionicsdk.discovery.Discovery.java

InetAddress getBroadcastAddress() throws Exception {
    WifiManager wifi = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();/*from   w ww.j a  v  a 2s.  c  o  m*/
    // handle null somehow

    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);
    InetAddress addr = InetAddress.getByAddress(quads);

    Log.d(TAG, "Got broadcast addr:" + addr);
    return addr;
}

From source file:nl.minvenj.pef.util.TestUtil.java

/**
 * Convert an array of bytes representing an IPv4/6 address to a dotted decimal or colonized format respectively.
 *
 * @param ipBytes the bytes containing the IP data
 * @return a string represenation of IPv4/6
 *///  w ww . j a v a2  s  .  c  o m
public static String ipStringFromBytes(final byte[] ipBytes) {
    try {
        return InetAddress.getByAddress(ipBytes).getHostAddress();
    } catch (final UnknownHostException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:nl.sidn.pcap.PcapReaderUtil.java

public static String convertAddress(byte[] data, int offset, int size) {
    byte[] addr = new byte[size];
    System.arraycopy(data, offset, addr, 0, addr.length);
    try {/*  w  ww .j  a v  a 2  s  . c o  m*/
        return InetAddress.getByAddress(addr).getHostAddress();
    } catch (UnknownHostException e) {
        LOG.error("Ivalid host address: ", e);
        return null;
    }
}