本文整理汇总了Java中com.google.common.net.InetAddresses.toAddrString方法的典型用法代码示例。如果您正苦于以下问题:Java InetAddresses.toAddrString方法的具体用法?Java InetAddresses.toAddrString怎么用?Java InetAddresses.toAddrString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.net.InetAddresses
的用法示例。
在下文中一共展示了InetAddresses.toAddrString方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: anonymizeIp
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
/**
* Anonymize an IP address
* @param inetAddress the IP address to be anonymized
* @param replaceLastOctetWith the String which shall replace the last octet in IPv4
* @return
*/
public static String anonymizeIp(final InetAddress inetAddress, String replaceLastOctetWith) {
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$", replaceLastOctetWith);
return result;
}
catch (final Exception e)
{
e.printStackTrace();
return null;
}
}
示例2: getBroadcastAddressFromCidr
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
public static String getBroadcastAddressFromCidr(String cidr) {
String[] ipaddressValues = cidr.split("/");
int address = InetAddresses.coerceToInteger(InetAddresses.forString(ipaddressValues[0]));
int cidrPart = Integer.parseInt(ipaddressValues[1]);
int netmask = 0;
for (int j = 0; j < cidrPart; ++j) {
netmask |= 1 << 31 - j;
}
int network = address & netmask;
int broadcast = network | ~netmask;
return InetAddresses.toAddrString(InetAddresses.fromInteger(broadcast));
}
示例3: marshal
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
@Override
public AddressShim marshal(InetAddress inetAddress) {
AddressShim shim = new AddressShim();
shim.ipAddress = InetAddresses.toAddrString(inetAddress);
shim.ipVersion = (inetAddress instanceof Inet6Address) ? "v6" : "v4";
return shim;
}
示例4: toString
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
@Override
public String toString() {
return InetAddresses.toAddrString(addr.getAddress()) + ':' +
addr.getPort();
}
示例5: toString
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
@Override
public String toString() {
return InetAddresses.toAddrString(address);
}
示例6: toString
import com.google.common.net.InetAddresses; //导入方法依赖的package包/类
/**
* 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);
}