java.net.Inet4Address 代码实例

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())

} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值