java.net.Inet4Address 代码实例
以下是展示如何使用java.net.Inet4Address的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。
实例 1
package io.hydrosphere.mist.utils import java.net.{Inet4Address, InetAddress, NetworkInterface} import scala.collection.JavaConverters._ object NetUtils { def findLocalInetAddress(): InetAddress = { val address = InetAddress.getLocalHost if (address.isLoopbackAddress) { val activeNetworkIFs = NetworkInterface.getNetworkInterfaces.asScala.toList for (ni <- activeNetworkIFs) { val addresses = ni.getInetAddresses.asScala.toList .filterNot(addr => addr.isLinkLocalAddress || addr.isLoopbackAddress) if (addresses.nonEmpty) { val addr = addresses.find(_.isInstanceOf[Inet4Address]).getOrElse(addresses.head) val strippedAddress = InetAddress.getByAddress(addr.getAddress) return strippedAddress } } } address } }
实例 2
package com.bwsw.sj.examples.sflow.module.process.udf import java.net.{Inet4Address, Inet6Address, InetAddress} import com.bwsw.sj.common.DAL.repository.ConnectionRepository import com.bwsw.sj.common.utils.ConfigSettingsUtils import com.maxmind.geoip.LookupService import org.slf4j.LoggerFactory object GeoIp { private val logger = LoggerFactory.getLogger(this.getClass) private val fileStorage = ConnectionRepository.getFileStorage private lazy val ipv4AsNumLookup = getAsLookupServiceIpv4 private lazy val ipv6AsNumLookup = getAsLookupServiceIpv6 def resolveAs(ip: String): Int = { try { InetAddress.getByName(ip) match { case _: Inet4Address => if (ipv4AsNumLookup.getID(ip) != 0) ipv4AsNumLookup.getOrg(ip).split(" ")(0).substring(2).toInt else 0 case _: Inet6Address => if (ipv6AsNumLookup.getID(ip) != 0) ipv6AsNumLookup.getOrgV6(ip).split(" ")(0).substring(2).toInt else 0 } } catch { case _: java.net.UnknownHostException => throw new Exception( s"""resolveAs error: "$ip" isn't correct ip address""") case _: com.maxmind.geoip.InvalidDatabaseException => logger.error( s"""resolveAs error: "$ip" com.maxmind.geoip.InvalidDatabaseException""") 0 } } private def getAsLookupServiceIpv4 = { val geoIpFileName = ConfigSettingsUtils.getGeoIpAsNumFileName() createLookupService(geoIpFileName) } private def getAsLookupServiceIpv6 = { val geoIpFileName = ConfigSettingsUtils.getGeoIpAsNumv6FileName() createLookupService(geoIpFileName) } private def createLookupService(filename: String) = { val databaseFile = fileStorage.get(filename, filename) new LookupService(databaseFile) } }
实例 3
package refined.guava.net import java.net.{Inet4Address, Inet6Address} import com.google.common.net.{InetAddresses, InternetDomainName, MediaType} import eu.timepit.refined.api.Validate import refined.guava.net.net.{DNS, IP, IPv4, IPv6, Media} object net extends NetValidate { case class IP() case class IPv4() case class IPv6() case class DNS() case class Media() } private[net] trait NetValidate { implicit def ipValidate: Validate.Plain[String, IP] = Validate.fromPartial(InetAddresses.forString, "IP Address", IP()) implicit def ipv4Validate: Validate.Plain[String, IPv4] = Validate.fromPredicate( str => InetAddresses.forString(str).isInstanceOf[Inet4Address], str => s"$str is not an IPv4 Address", IPv4()) implicit def ipv6Validate: Validate.Plain[String, IPv6] = Validate.fromPredicate( str => InetAddresses.forString(str).isInstanceOf[Inet6Address], str => s"$str is not an IPv6 Address", IPv6()) implicit def dnsValidate: Validate.Plain[String, DNS] = Validate.fromPartial(InternetDomainName.from, "DNS name", DNS()) implicit def mediaValidate: Validate.Plain[String, Media] = Validate.fromPartial(MediaType.parse, "MediaType", Media()) }