
br.com.objectos.net.SimpleConfiguredNetworkAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of net Show documentation
Show all versions of net Show documentation
Java8 utilities for networking stuff.
The newest version!
/*
* Copyright 2016 Objectos, Fábrica de Software LTDA.
*
* Licensed 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 br.com.objectos.net;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
/**
* @author [email protected] (Marcio Endo)
*/
class SimpleConfiguredNetworkAdapter implements ConfiguredNetworkAdapter {
private static final IpAddress BROADCAST = IpAddress.of(255, 255, 255, 255);
private static final HardwareAddress EMPTY = HardwareAddress.wrap(new byte[] {});
private final String name;
private final HardwareAddress hardwareAddress;
private final IpAddress ipAddress;
private final IpAddress broadcast;
private SimpleConfiguredNetworkAdapter(String name,
HardwareAddress hardwareAddress,
IpAddress ipAddress,
IpAddress broadcast) {
this.name = name;
this.hardwareAddress = hardwareAddress;
this.ipAddress = ipAddress;
this.broadcast = broadcast;
}
public static ConfiguredNetworkAdapter of(NetworkInterface iface, InterfaceAddress address) {
return new SimpleConfiguredNetworkAdapter(
iface.getName(),
hardwareAddress(iface),
IpAddress.of(address.getAddress()),
broadcast(address));
}
private static IpAddress broadcast(InterfaceAddress address) {
InetAddress broadcast = address.getBroadcast();
return broadcast != null ? IpAddress.of(broadcast) : BROADCAST;
}
private static HardwareAddress hardwareAddress(NetworkInterface iface) {
try {
return HardwareAddress.wrap(iface.getHardwareAddress());
} catch (SocketException e) {
return EMPTY;
}
}
@Override
public String name() {
return name;
}
@Override
public HardwareAddress hardwareAddress() {
return hardwareAddress;
}
@Override
public IpAddress ipAddress() {
return ipAddress;
}
@Override
public IpAddress broadcast() {
return broadcast;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy