All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nervousync.beans.network.NetworkInfo Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.nervousync.beans.network;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;

import jakarta.annotation.Nonnull;
import org.nervousync.exceptions.beans.network.NetworkInfoException;
import org.nervousync.utils.IPUtils;

/**
 * 

System NetworkInterface information define

*

系统网卡信息定义

* * @author Steven Wee [email protected] * @version $Revision: 1.1.3 $ $Date: Apr 06, 2020 11:53:10 $ */ public final class NetworkInfo implements Serializable { /** * Serial version UID * 序列化UID */ private static final long serialVersionUID = -8060054814830700945L; /** * Current network interface is virtual interface * 当前网络接口是虚拟接口 */ private final boolean virtual; /** * Display name of current network interface * 当前网络接口的显示名称 */ private final String displayName; /** * MAC address of current network interface * 当前网络接口的物理地址 */ private String macAddress = ""; /** * IP address list of current network interface * 当前网络接口绑定的IP地址列表 */ private final List ipAddressList = new ArrayList<>(); /** *

Constructor for NetworkInfo

* Read network interface information from java.net.NetworkInterface instance *

NetworkInfo的构造函数

* 从java.net.NetworkInterface对象实例中读取网络接口相关信息 * * @param networkInterface Instance of java.net.NetworkInterface * java.net.NetworkInterface对象实例 * * @throws NetworkInfoException * If the value of NetworkInterface is null or an I/O error occurs * 当参数networkInterface为空或捕获I/O异常 */ public NetworkInfo(final NetworkInterface networkInterface) throws NetworkInfoException { if (networkInterface == null) { throw new NetworkInfoException(0x0000001A0001L, "Null_Network_Interface_Error"); } try { if (networkInterface.isUp() && !networkInterface.isVirtual()) { byte[] macAddress = networkInterface.getHardwareAddress(); if (macAddress != null && macAddress.length > 0) { StringBuilder stringBuilder = new StringBuilder(); for (byte mac : macAddress) { stringBuilder.append(":"); String address = Integer.toHexString(mac & 0xFF); if (address.length() == 1) { address = "0" + address; } stringBuilder.append(address.toUpperCase()); } this.macAddress = stringBuilder.substring(1); } } } catch (SocketException e) { throw new NetworkInfoException(0x0000001A0002L, "Retrieve_Network_Interface_Error", e); } this.virtual = networkInterface.isVirtual(); this.displayName = networkInterface.getDisplayName(); Enumeration enumeration = networkInterface.getInetAddresses(); while (enumeration.hasMoreElements()) { Optional.ofNullable(enumeration.nextElement()) .map(IPAddressInfo::new) .ifPresent(this.ipAddressList::add); } } /** *

Getter method for field virtual

*

虚拟接口状态的Getter方法

* * @return Virtual status * 虚拟接口状态 */ public boolean isVirtual() { return virtual; } /** *

Getter method for display name

*

显示名称的Getter方法

* * @return Display name of current network interface * 当前网络接口的显示名称 */ public String getDisplayName() { return displayName; } /** *

Getter method for MAC address

*

网卡物理地址的Getter方法

* * @return MAC address of current network interface * 当前网络接口的物理地址 */ public String getMacAddress() { return macAddress; } /** *

Getter method for IP address list

*

网卡IP地址列表的Getter方法

* * @return IP address list of current network interface * 当前网络接口绑定的IP地址列表 */ public List getIpAddressList() { return ipAddressList; } /** *

Getter method for IPv4 address list

*

网卡IPv4地址列表的Getter方法

* * @return IPv4 address list of current network interface * 当前网络接口绑定的IPv4地址列表 */ public List getIPv4AddressList() { List addressList = new ArrayList<>(); for (IPAddressInfo ipAddressInfo : this.ipAddressList) { if (IPUtils.isIPv4Address(ipAddressInfo.getIpAddress())) { addressList.add(ipAddressInfo); } } return addressList; } /** *

Getter method for IPv6 address list

*

网卡IPv6地址列表的Getter方法

* * @return IPv6 address list of current network interface * 当前网络接口绑定的IPv6地址列表 */ public List getIPv6AddressList() { List addressList = new ArrayList<>(); for (IPAddressInfo ipAddressInfo : this.ipAddressList) { if (IPUtils.isIPv6Address(ipAddressInfo.getIpAddress())) { addressList.add(ipAddressInfo); } } return addressList; } /** *

IP address information define

*

IP地址信息定义

* * @author Steven Wee [email protected] * @version $Revision: 1.0.0 $ $Date: Jul 2, 2018 09:22:28 $ */ public static final class IPAddressInfo implements Serializable { /** * Serial version UID * 序列化UID */ private static final long serialVersionUID = -2882813548945783456L; /** * IP address string, supported IPv4 and IPv6 * IP地址字符串,支持IPv4和IPv6 */ private final String ipAddress; /** * SiteLocal address flag * 私网地址标识 */ private final boolean local; /** * Loop address flag * 回环地址标识 */ private final boolean loop; /** * LinkLocal address flag * 链路地址标识 */ private final boolean linkLocal; /** *

Constructor for IPAddressInfo

* Read IP address information from java.net.InetAddress instance *

IPAddressInfo的构造函数

* 从java.net.InetAddress对象实例中读取IP地址相关信息 * * @param inetAddress Instance of java.net.InetAddress * java.net.InetAddress对象实例 */ public IPAddressInfo(@Nonnull final InetAddress inetAddress) { String ipAddress = inetAddress.getHostAddress(); if (ipAddress.indexOf("%") > 0) { this.ipAddress = ipAddress.substring(0, ipAddress.indexOf("%")); } else { this.ipAddress = ipAddress; } this.local = inetAddress.isSiteLocalAddress(); this.loop = inetAddress.isLoopbackAddress(); this.linkLocal = inetAddress.isLinkLocalAddress(); } /** *

Getter method for IP address string

*

IP地址字符串的Getter方法

* * @return IP address string, supported IPv4 and IPv6 * IP地址字符串,支持IPv4和IPv6 */ public String getIpAddress() { return ipAddress; } /** *

Getter method for site local flag

*

私网地址标识的Getter方法

* * @return SiteLocal address flag * 私网地址标识 */ public boolean isLocal() { return local; } /** *

Getter method for loop address flag

*

回环地址标识的Getter方法

* * @return Loop address flag * 回环地址标识 */ public boolean isLoop() { return loop; } /** *

Getter method for link local flag

*

链路地址标识的Getter方法

* * @return LinkLocal address flag * 链路地址标识 */ public boolean isLinkLocal() { return linkLocal; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy