Java Code Examples for com.google.common.net.InetAddresses#toAddrString()

The following examples show how to use com.google.common.net.InetAddresses#toAddrString() . 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: Helperfunctions.java    From open-rmbt with Apache License 2.0 6 votes vote down vote up
public static String anonymizeIp(final InetAddress inetAddress)
{
    try
    {
        final byte[] address = inetAddress.getAddress();
        address[address.length - 1] = 0;
        if (address.length > 4) // ipv6
        {
            for (int i = 6; i < address.length; i++)
                address[i] = 0;
        }
        
        String result = InetAddresses.toAddrString(InetAddress.getByAddress(address));
        if (address.length == 4)
            result = result.replaceFirst(".0$", "");
        return result;
    }
    catch (final Exception e)
    {
        e.printStackTrace();
        return null;
    }
}
 
Example 2
Source File: IpAddressType.java    From presto with Apache License 2.0 5 votes vote down vote up
@Override
public Object getObjectValue(ConnectorSession session, Block block, int position)
{
    if (block.isNull(position)) {
        return null;
    }
    try {
        return InetAddresses.toAddrString(InetAddress.getByAddress(getSlice(block, position).getBytes()));
    }
    catch (UnknownHostException e) {
        throw new IllegalArgumentException();
    }
}
 
Example 3
Source File: AbstractTraceLog.java    From summerframework with Apache License 2.0 5 votes vote down vote up
private static String getLocalhost() {
    InetAddress localInetAddress = null;
    try {
        localInetAddress = InetAddress.getLocalHost();
    } catch (UnknownHostException e) {

    }

    if (localInetAddress == null) {
        return "127.0.0.1";
    }

    return InetAddresses.toAddrString(localInetAddress);
}
 
Example 4
Source File: AddressUtils.java    From dhcp4j with Apache License 2.0 5 votes vote down vote up
/** Like {@link InetAddresses#toAddrString(InetAddress)} but accepts nulls. */
@CheckForNull
public static String toAddrString(@CheckForNull InetAddress address) {
    if (address == null)
        return null;
    return InetAddresses.toAddrString(address);
}
 
Example 5
Source File: AclMaintainer.java    From vespa with Apache License 2.0 5 votes vote down vote up
private void applyRedirect(NodeAgentContext context, InetAddress address) {
    IPVersion ipVersion = IPVersion.get(address);
    // Necessary to avoid the routing packets destined for the node's own public IP address
    // via the bridge, which is illegal.
    String redirectRule = "-A OUTPUT -d " + InetAddresses.toAddrString(address) + ipVersion.singleHostCidr() + " -j REDIRECT";
    editLogOnError(context, ipVersion, "nat", NatTableLineEditor.from(redirectRule));
}
 
Example 6
Source File: PCCTunnelManagerImpl.java    From bgpcep with Eclipse Public License 1.0 5 votes vote down vote up
public PCCTunnelManagerImpl(final int lspsCount, final InetAddress address, final int redelegationTimeout,
                            final int stateTimeout, final Timer timer, final Optional<TimerHandler> timerHandler) {
    Preconditions.checkArgument(lspsCount >= 0);
    this.redelegationTimeout = redelegationTimeout;
    this.stateTimeout = stateTimeout;
    this.plspIDsCounter = new AtomicLong(lspsCount);
    this.address = InetAddresses.toAddrString(requireNonNull(address));
    this.timer = requireNonNull(timer);
    this.timerHandler = timerHandler;
    this.lspsCount = lspsCount;
}
 
Example 7
Source File: AtriumIpAddress.java    From atrium-odl with Apache License 2.0 5 votes vote down vote up
@Override
/*
 * (non-Javadoc)
 * The string representation of the IP address: "x.x.x.x" for IPv4
 * addresses, or ':' separated string for IPv6 addresses.
 *
 * @see java.lang.Object#toString()
 */
public String toString() {
    // FIXME InetAddress is super slow
    switch (version) {
        case INET:
            return String.format("%d.%d.%d.%d", octets[0] & 0xff,
                                                octets[1] & 0xff,
                                                octets[2] & 0xff,
                                                octets[3] & 0xff);
        case INET6:
        default:
            InetAddress inetAddr = null;
            try {
                inetAddr = InetAddress.getByAddress(octets);
            } catch (UnknownHostException e) {
                // Should never happen
                checkState(false, "Internal error: Ip6Address.toString()");
                return "[Invalid IP Address]";
            }
            return InetAddresses.toAddrString(inetAddr);
    }
}
 
Example 8
Source File: AddressOption.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
protected String toStringData() throws DhcpException {
    return InetAddresses.toAddrString(getAddress());
}
 
Example 9
Source File: ChannelUtils.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
public static String inetNtoA(int ip) {
    return InetAddresses.toAddrString(InetAddresses.fromInteger(ip));
}
 
Example 10
Source File: AbstractMaskedAddress.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return InetAddresses.toAddrString(getAddress()) + "/" + getNetmask();
}
 
Example 11
Source File: AbstractMaskedAddress.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
public AbstractMaskedAddress(@Nonnull InetAddress address, @Nonnegative int netmask) {
    if (netmask > address.getAddress().length * Byte.SIZE)
        throw new IllegalArgumentException("Netmask too large: " + InetAddresses.toAddrString(address) + "/" + netmask);
    this.address = address;
    this.netmask = netmask;
}
 
Example 12
Source File: InetAddressRange.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return InetAddresses.toAddrString(getStart()) + "-" + InetAddresses.toAddrString(getEnd());
}
 
Example 13
Source File: AbstractMaskedAddress.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
public String toString() {
    return InetAddresses.toAddrString(getAddress()) + "/" + getNetmask();
}
 
Example 14
Source File: InetAddressSetConverter.java    From nomulus with Apache License 2.0 4 votes vote down vote up
@Override
String toString(InetAddress element) {
  return InetAddresses.toAddrString(element);
}
 
Example 15
Source File: Acl.java    From vespa with Apache License 2.0 4 votes vote down vote up
public String inetAddressString() {
    return InetAddresses.toAddrString(inetAddress);
}
 
Example 16
Source File: AbstractMaskedAddress.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
public AbstractMaskedAddress(@Nonnull InetAddress address, @Nonnegative int netmask) {
    if (netmask > address.getAddress().length * Byte.SIZE)
        throw new IllegalArgumentException("Netmask too large: " + InetAddresses.toAddrString(address) + "/" + netmask);
    this.address = address;
    this.netmask = netmask;
}
 
Example 17
Source File: AddressOption.java    From dhcp4j with Apache License 2.0 4 votes vote down vote up
@Override
protected String toStringData() throws DhcpException {
    return InetAddresses.toAddrString(getAddress());
}
 
Example 18
Source File: IPUtil.java    From j360-dubbo-app-all with Apache License 2.0 2 votes vote down vote up
/**
 * InetAddress转换为String.
 * 
 * InetAddress可以是IPV4或IPV6. 其中IPV4直接调用getHostAddress()
 * 
 * @see com.google.common.net.InetAddresses#toAddrString(InetAddress)
 */
public static String toString(InetAddress address) {
	return InetAddresses.toAddrString(address);
}
 
Example 19
Source File: IPUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * InetAddress转换为String.
 * 
 * InetAddress可以是IPV4或IPV6. 其中IPV4直接调用getHostAddress()
 * 
 * @see com.google.common.net.InetAddresses#toAddrString(InetAddress)
 */
public static String toIpString(InetAddress address) {
	return InetAddresses.toAddrString(address);
}
 
Example 20
Source File: IPUtil.java    From vjtools with Apache License 2.0 2 votes vote down vote up
/**
 * InetAddress转换为String.
 * 
 * InetAddress可以是IPV4或IPV6. 其中IPV4直接调用getHostAddress()
 * 
 * @see com.google.common.net.InetAddresses#toAddrString(InetAddress)
 */
public static String toIpString(InetAddress address) {
	return InetAddresses.toAddrString(address);
}