本文整理汇总了Scala中java.net.Inet4Address类的典型用法代码示例。如果您正苦于以下问题:Scala Inet4Address类的具体用法?Scala Inet4Address怎么用?Scala Inet4Address使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Inet4Address类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: IPAddressSpec
//设置package包名称以及导入依赖的类
package edu.uw.at.iroberts.wirefugue.pcap
import org.scalatest.{Matchers, WordSpecLike}
class IPAddressSpec extends WordSpecLike with Matchers {
val ipString: String = "192.168.42.196"
val ipBytes: Seq[Byte] = Seq(0xc0, 0xa8, 0x2a, 0xc4).map(_.toByte)
"an IPAddress" should {
"be constructable from String and Seq[Byte]" in {
IPAddress(ipString) shouldEqual IPAddress(ipBytes)
}
"produce a string representation" in {
val a = IPAddress(ipBytes)
a.toString shouldEqual ipString
}
"produce a byte sequence representation" in {
val a = IPAddress(ipString)
a.bytes shouldEqual ipBytes
}
"provide an equivalent java.net.Inet4Address" in {
import java.net.{InetAddress, Inet4Address}
val a = IPAddress(ipString)
a.inet4Address shouldEqual InetAddress.getByAddress(ipBytes.toArray).asInstanceOf[Inet4Address]
}
"reject invalid sequences upon construction" in {
assertThrows[IllegalArgumentException] {
IPAddress(Seq[Byte](0x10, 0x20, 0x30, 0x40, 0x50))
}
}
"reject invalid strings upon construction" in {
assertThrows[IllegalArgumentException] {
IPAddress("192.168.0.256")
}
assertThrows[IllegalArgumentException] {
IPAddress("10.0.0.0.1")
}
assertThrows[IllegalArgumentException] {
IPAddress("192.168.2")
}
}
}
}
示例2: Subnet
//设置package包名称以及导入依赖的类
package com.github.shadowsocks.acl
import java.net.{Inet4Address, Inet6Address, InetAddress}
import com.github.shadowsocks.utils.Utils
@throws[IllegalArgumentException]
class Subnet(val address: InetAddress, val prefixSize: Int) extends Comparable[Subnet] {
if (prefixSize < 0) throw new IllegalArgumentException
address match {
case _: Inet4Address => if (prefixSize > 32) throw new IllegalArgumentException
case _: Inet6Address => if (prefixSize > 128) throw new IllegalArgumentException
}
override def toString: String = if (address match {
case _: Inet4Address => prefixSize == 32
case _: Inet6Address => prefixSize == 128
}) address.getHostAddress else address.getHostAddress + '/' + prefixSize
override def compareTo(that: Subnet): Int = {
val addrThis = address.getAddress
val addrThat = that.address.getAddress
var result = addrThis lengthCompare addrThat.length // IPv4 address goes first
if (result != 0) return result
for ((x, y) <- addrThis zip addrThat) {
result = (x & 0xFF) compare (y & 0xFF) // undo sign extension of signed byte
if (result != 0) return result
}
prefixSize compare that.prefixSize
}
}
object Subnet {
@throws[IllegalArgumentException]
def fromString(value: String): Subnet = {
val parts = value.split("/")
if (!Utils.isNumeric(parts(0))) throw new IllegalArgumentException()
val addr = InetAddress.getByName(parts(0))
parts.length match {
case 1 => new Subnet(addr, addr match {
case _: Inet4Address => 32
case _: Inet6Address => 128
})
case 2 => new Subnet(addr, parts(1).toInt)
case _ => throw new IllegalArgumentException()
}
}
}
示例3: NetUtils
//设置package包名称以及导入依赖的类
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
}
}
示例4: GeoIp
//设置package包名称以及导入依赖的类
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)
}
}
示例5: net
//设置package包名称以及导入依赖的类
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())
}
示例6: ConsulSettings
//设置package包名称以及导入依赖的类
package io.blumenplace.monitor
import java.net.Inet4Address
import akka.actor.{Actor, Props}
import com.typesafe.scalalogging.LazyLogging
// import consul.Consul
class ConsulSettings(address: Inet4Address, port: Int = 8500) extends Actor with LazyLogging {
def receive = {
case _ =>
logger.error("Unexpected message has been received")
}
}
object ConsulSettings {
case class ReadSettings()
def props() = Props(classOf[ConsulSettings])
}