public static final int IP4_LENGTH = 4;
+ /**
+ * Converts byte array to Inet4Address.
+ *
+ * @param bytes to be converted
+ * @return InetAddress instance
+ * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
+ */
private static InetAddress getAddress(final byte[] bytes) {
try {
return Inet4Address.getByAddress(bytes);
}
}
+ /**
+ * Converts byte array to Ipv4Address.
+ *
+ * @param bytes to be converted to Ipv4Address
+ * @return Ipv4Address
+ */
public static Ipv4Address addressForBytes(final byte[] bytes) {
return new Ipv4Address(InetAddresses.toAddrString(getAddress(bytes)));
}
+ /**
+ * Converts Ipv4Address to byte array.
+ *
+ * @param address Ipv4Address to be converted
+ * @return byte array
+ */
public static byte[] bytesForAddress(final Ipv4Address address) {
final InetAddress a = InetAddresses.forString(address.getValue());
Preconditions.checkArgument(a instanceof Inet4Address);
return a.getAddress();
}
+ /**
+ * Converts Ipv4Prefix to byte array.
+ *
+ * @param prefix Ipv4Prefix to be converted
+ * @return byte array
+ */
public static byte[] bytesForPrefix(final Ipv4Prefix prefix) {
final String p = prefix.getValue();
final int sep = p.indexOf('/');
return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
}
+ /**
+ * Creates an Ipv4Prefix object from given byte array.
+ *
+ * @param bytes IPv4 address
+ * @param length prefix length
+ * @return Ipv4Prefix object
+ */
public static Ipv4Prefix prefixForBytes(final byte[] bytes, final int length) {
Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
final byte[] tmp = Arrays.copyOfRange(bytes, 0, IP4_LENGTH);
return new Ipv4Prefix(InetAddresses.toAddrString(a) + '/' + length);
}
+ /**
+ * Creates a list of Ipv4 Prefixes from given byte array.
+ *
+ * @param bytes to be converted to List of Ipv4Prefixes.
+ * @return List<Ipv4Prefix>
+ */
public static List<Ipv4Prefix> prefixListForBytes(final byte[] bytes) {
if (bytes.length == 0) {
return Collections.emptyList();
}
-
final List<Ipv4Prefix> list = Lists.newArrayList();
int byteOffset = 0;
while (byteOffset < bytes.length) {
return list;
}
+ /**
+ * Obtains prefix length from given prefix.
+ *
+ * @param prefix
+ * @return prefix length
+ */
public static int getPrefixLength(final IpPrefix prefix) {
String p = "";
if (prefix.getIpv4Prefix() != null) {
public static final int IPV6_LENGTH = 16;
+ /**
+ * Converts byte array to Inet6Address.
+ *
+ * @param bytes to be converted
+ * @return InetAddress instance
+ * @throws IllegalArgumentException if {@link UnknownHostException} is thrown.
+ */
private static InetAddress getAddress(final byte[] bytes) {
try {
return Inet6Address.getByAddress(bytes);
}
}
+ /**
+ * Converts byte array to Ipv6Address.
+ *
+ * @param bytes to be converted to Ipv6Address
+ * @return Ipv6Address
+ */
public static Ipv6Address addressForBytes(final byte[] bytes) {
return new Ipv6Address(InetAddresses.toAddrString(getAddress(bytes)));
}
+ /**
+ * Converts Ipv6Address to byte array.
+ *
+ * @param address Ipv6Address to be converted
+ * @return byte array
+ */
public static byte[] bytesForAddress(final Ipv6Address address) {
final InetAddress a = InetAddresses.forString(address.getValue());
Preconditions.checkArgument(a instanceof Inet6Address);
return a.getAddress();
}
+ /**
+ * Converts Ipv6Prefix to byte array.
+ *
+ * @param prefix Ipv6Prefix to be converted
+ * @return byte array
+ */
public static byte[] bytesForPrefix(final Ipv6Prefix prefix) {
final String p = prefix.getValue();
final int sep = p.indexOf('/');
return Bytes.concat(bytes, new byte[] { Byte.valueOf(p.substring(sep + 1, p.length())) });
}
+ /**
+ * Creates an Ipv6Prefix object from given byte array.
+ *
+ * @param bytes IPv6 address
+ * @param length prefix length
+ * @return Ipv6Prefix object
+ */
public static Ipv6Prefix prefixForBytes(final byte[] bytes, final int length) {
Preconditions.checkArgument(length <= bytes.length * Byte.SIZE);
final byte[] tmp = Arrays.copyOfRange(bytes, 0, IPV6_LENGTH);
return new Ipv6Prefix(InetAddresses.toAddrString(a) + '/' + length);
}
+ /**
+ * Creates a list of Ipv6 Prefixes from given byte array.
+ *
+ * @param bytes to be converted to List of Ipv6Prefixes.
+ * @return List<Ipv6Prefix>
+ */
public static List<Ipv6Prefix> prefixListForBytes(final byte[] bytes) {
if (bytes.length == 0) {
return Collections.emptyList();
import com.google.common.primitives.UnsignedInteger;
/**
- *
* Util class for methods working with byte array.
- *
*/
public final class ByteArray {
private ByteArray() {
return bytes;
}
+ /**
+ * Trims zeros from the beginning of the byte array.
+ *
+ * @param bytes
+ * @return byte array without leading zeros.
+ */
public static byte[] trim(final byte[] bytes) {
int i = bytes.length - 1;
while (i >= 0 && bytes[i] == 0) {
return Arrays.copyOf(bytes, i + 1);
}
+ /**
+ * Converts given byte array to unsigned Integer.
+ *
+ * @param bytes byte array to be converted to unsigned Integer.
+ * @return uint
+ */
public static UnsignedInteger bytesToUint32(final byte[] bytes) {
Preconditions.checkArgument(bytes.length == Integer.SIZE / Byte.SIZE);
return UnsignedInteger.fromIntBits(bytesToInt(bytes));
}
+ /**
+ * Converts uint to byte array.
+ *
+ * @param uint to be converted to byte array
+ * @return byte array
+ */
public static byte[] uint32ToBytes(final UnsignedInteger uint) {
return intToBytes(uint.intValue());
}
+ /**
+ * Converts uint as long to byte array.
+ *
+ * @param uint to be converted to byte array
+ * @return byte array
+ */
public static byte[] uint32ToBytes(final long uint) {
return uint32ToBytes(UnsignedInteger.valueOf(uint));
}
import java.util.Iterator;
import java.util.Set;
+@Deprecated
public class RemoveOnlySet<E> implements Set<E> {
private final Set<E> set;
@Override
public void clear() {
- set.clear();
+ this.set.clear();
}
@Override
public boolean contains(final Object o) {
- return set.contains(o);
+ return this.set.contains(o);
}
@Override
public boolean containsAll(final Collection<?> c) {
- return set.containsAll(c);
+ return this.set.containsAll(c);
}
@Override
public boolean isEmpty() {
- return set.isEmpty();
+ return this.set.isEmpty();
}
@Override
public Iterator<E> iterator() {
- return set.iterator();
+ return this.set.iterator();
}
@Override
public boolean remove(final Object o) {
- return set.remove(o);
+ return this.set.remove(o);
}
@Override
public boolean removeAll(final Collection<?> c) {
- return set.removeAll(c);
+ return this.set.removeAll(c);
}
@Override
public boolean retainAll(final Collection<?> c) {
- return set.retainAll(c);
+ return this.set.retainAll(c);
}
@Override
public int size() {
- return set.size();
+ return this.set.size();
}
@Override
public Object[] toArray() {
- return set.toArray();
+ return this.set.toArray();
}
@Override
public <T> T[] toArray(final T[] a) {
- return set.toArray(a);
+ return this.set.toArray(a);
}
}
*/
package org.opendaylight.protocol.util;
+/**
+ * Util class for storing various util values as constants.
+ */
public final class Values {
private Values() {
}
+ /**
+ * Maximum unsigned Short value.
+ */
public static final int UNSIGNED_SHORT_MAX_VALUE = 65535;
+ /**
+ * Maximum unsigned Byte value.
+ */
public static final int UNSIGNED_BYTE_MAX_VALUE = 255;
+ /**
+ * Maximum nsigned Byte value in hex.
+ */
public static final int BYTE_MAX_VALUE_BYTES = 0xFF;
}