*/
public static byte[] readBytes(final ByteBuf buffer, final int length) {
Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
- "Buffer cannot be read for %s bytes.", length);
+ "Buffer cannot be read for %s bytes.", length);
final byte[] result = new byte[length];
buffer.readBytes(result);
return result;
*/
public static byte[] getBytes(final ByteBuf buffer, final int length) {
Preconditions.checkArgument(buffer != null && buffer.readableBytes() >= length,
- "Buffer cannot be read for %s bytes.", length);
+ "Buffer cannot be read for %s bytes.", length);
final byte[] result = new byte[length];
buffer.getBytes(buffer.readerIndex(), result);
return result;
*/
public static byte[] subByte(final byte[] bytes, final int startIndex, final int length) {
if (bytes.length == 0 || length < 0 || length > bytes.length || startIndex < 0 || startIndex > bytes.length
- || startIndex + length > bytes.length) {
+ || startIndex + length > bytes.length) {
throw new IllegalArgumentException("Cannot create subByte, invalid arguments: Length: " + length + " startIndex: " + startIndex);
}
final byte[] res = new byte[length];
if (file.length() > Integer.MAX_VALUE) {
throw new IOException("Too large file to load in byte array.");
}
-
- final FileInputStream fin = new FileInputStream(file);
final byte[] byteArray = new byte[(int) file.length()];
-
- while (offset < byteArray.length && (numRead = fin.read(byteArray, offset, byteArray.length - offset)) >= 0) {
- offset += numRead;
- }
-
- if (fin != null) {
+ try (final FileInputStream fin = new FileInputStream(file)) {
+ while (offset < byteArray.length && (numRead = fin.read(byteArray, offset, byteArray.length - offset)) >= 0) {
+ offset += numRead;
+ }
fin.close();
}
-
return byteArray;
}
* @return copied value aligned to right
*/
public static byte copyBitsRange(final byte src, final int fromBit, final int length) {
- if (fromBit < 0 | fromBit > Byte.SIZE - 1 | length < 1 | length > Byte.SIZE) {
+ if (fromBit < 0 || fromBit > Byte.SIZE - 1 || length < 1 || length > Byte.SIZE) {
throw new IllegalArgumentException("fromBit or toBit is out of range.");
}
if (fromBit + length > Byte.SIZE) {
* @return Hexadecimal string representation of given byte array
*/
public static String bytesToHexString(final byte[] array, final int bytesOnLine, final String byteSeparator, final int wordCount,
- final String wordSeparator) {
+ final String wordSeparator) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < array.length; i++) {
sb.append(Hex.encodeHexString(new byte[] { array[i] }));
*/
public final class ByteBufWriteUtil {
- protected static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
+ public static final int SHORT_BYTES_LENGTH = Short.SIZE / Byte.SIZE;
- protected static final int MEDIUM_BYTES_LENGTH = 3;
+ public static final int MEDIUM_BYTES_LENGTH = 3;
- protected static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
+ public static final int INT_BYTES_LENGTH = Integer.SIZE / Byte.SIZE;
- protected static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
+ public static final int LONG_BYTES_LENGTH = Long.SIZE / Byte.SIZE;
- protected static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
+ public static final int FLOAT32_BYTES_LENGTH = INT_BYTES_LENGTH;
- protected static final int ONE_BYTE_LENGTH = 1;
+ public static final int ONE_BYTE_LENGTH = 1;
- protected static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
+ public static final int IPV4_PREFIX_BYTE_LENGTH = Ipv4Util.IP4_LENGTH + 1;
- protected static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
+ public static final int IPV6_PREFIX_BYTE_LENGTH = Ipv6Util.IPV6_LENGTH + 1;
private ByteBufWriteUtil() {
}
import com.google.common.net.InetAddresses;
import com.google.common.primitives.Bytes;
import com.google.common.primitives.UnsignedBytes;
-
import io.netty.buffer.ByteBuf;
-
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
* @return
*/
public static int getPrefixLengthBytes(final String prefix) {
- int bits = Ipv4Util.getPrefixLength(prefix);
+ final int bits = Ipv4Util.getPrefixLength(prefix);
if (bits % 8 != 0) {
return (bits / 8) + 1;
}
* @param ipv4Prefix Ipv4Prefix to be converted
* @return byte array
*/
- public static byte[] bytesForPrefixByPrefixLength(Ipv4Prefix ipv4Prefix) {
+ public static byte[] bytesForPrefixByPrefixLength(final Ipv4Prefix ipv4Prefix) {
return ByteArray.subByte(bytesForPrefix(ipv4Prefix), 0,
- getPrefixLengthBytes(ipv4Prefix.getValue()));
+ getPrefixLengthBytes(ipv4Prefix.getValue()));
}
/**
*/
public static int getPrefixLength(final String prefixValue) {
final int sep = prefixValue.indexOf('/');
- return Integer.valueOf(prefixValue.substring(sep + 1, prefixValue.length()));
+ return Integer.parseInt(prefixValue.substring(sep + 1, prefixValue.length()));
}
}
// next chars are final length, ending with '.'
final int lengthIdx = idx + LENGTH.length();
final int messageIdx = content.indexOf('.', lengthIdx);
- final int length = Integer.valueOf(content.substring(lengthIdx, messageIdx));
+ final int length = Integer.parseInt(content.substring(lengthIdx, messageIdx));
final int messageEndIdx = messageIdx + (length * 2) + 1; // dot
// Assert that message is longer than minimum 4(header.length == 4)
// If length in PCEP message would be 0, loop would never end
Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
- + MINIMAL_LENGTH);
+ + MINIMAL_LENGTH);
final String hexMessage = content.substring(messageIdx + 1, messageEndIdx); // dot
byte[] message = null;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
-
import org.junit.Test;
public class PCEPHexDumpParserTest {
public static final String hexDumpFileName = "pcep-hex.txt";
- private final int expectedSize = 6;
+ private static final int expectedSize = 6;
@Test
public void testParsing() throws Exception {
final List<byte[]> result = PCEPHexDumpParser.parseMessages(getClass().getClassLoader().getResourceAsStream(
- PCEPHexDumpParserTest.hexDumpFileName));
+ PCEPHexDumpParserTest.hexDumpFileName));
assertEquals(this.expectedSize, result.size());
final List<byte[]> result1 = PCEPHexDumpParser.parseMessages(new File(getClass().getClassLoader().getResource(
- PCEPHexDumpParserTest.hexDumpFileName).toURI()));
+ PCEPHexDumpParserTest.hexDumpFileName).toURI()));
assertEquals(this.expectedSize, result1.size());
}