Fixed various findbugs issues in util. 59/9359/7
authorDana Kutenicsova <dkutenic@cisco.com>
Sun, 27 Jul 2014 18:08:53 +0000 (20:08 +0200)
committerRobert Varga <rovarga@cisco.com>
Tue, 29 Jul 2014 10:05:13 +0000 (10:05 +0000)
Change-Id: I5e31d46ed509596284764cb7a1eba63399789dff
Signed-off-by: Dana Kutenicsova <dkutenic@cisco.com>
util/src/main/java/org/opendaylight/protocol/util/ByteArray.java
util/src/main/java/org/opendaylight/protocol/util/ByteBufWriteUtil.java
util/src/main/java/org/opendaylight/protocol/util/Ipv4Util.java
util/src/main/java/org/opendaylight/protocol/util/PCEPHexDumpParser.java
util/src/test/java/org/opendaylight/protocol/util/PCEPHexDumpParserTest.java

index 38f59021c56b9a44e6a1e646252b1368f69dd44e..bd58afae1eb59e88b7c4df7951fb7370a0b44d84 100644 (file)
@@ -37,7 +37,7 @@ public final class ByteArray {
      */
     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;
@@ -64,7 +64,7 @@ public final class ByteArray {
      */
     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;
@@ -92,7 +92,7 @@ public final class ByteArray {
      */
     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];
@@ -244,18 +244,13 @@ public final class ByteArray {
         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;
     }
 
@@ -316,7 +311,7 @@ public final class 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) {
@@ -434,7 +429,7 @@ public final class ByteArray {
      * @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] }));
index 2d029ee8a80b50c1c89c90453b8f30a07aca3c69..b7a336872a8acfd830c36245a6fb5e05f9bc6764 100644 (file)
@@ -23,21 +23,21 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ieee754.
  */
 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() {
     }
index a9b60407033971aec8a82d31bc8b3092501ead38..c3df434deb45ebf430c5a7a6749d62304136f575 100644 (file)
@@ -12,16 +12,13 @@ import com.google.common.collect.Lists;
 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;
@@ -90,7 +87,7 @@ public final class Ipv4Util {
      * @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;
         }
@@ -103,9 +100,9 @@ public final class Ipv4Util {
      * @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()));
     }
 
     /**
@@ -200,6 +197,6 @@ public final class Ipv4Util {
      */
     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()));
     }
 }
index fe22f9a36e4852d4492a7bea8edad20b8db0fdd1..24560af310f95ab3d309027adc101fd20670f4a0 100644 (file)
@@ -60,13 +60,13 @@ public final class PCEPHexDumpParser {
             // 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;
index 2c8f2e47ecb66683ca6de11deed12505dfa934dc..7e0084a20692edabc56e861779101d95e646098f 100644 (file)
@@ -15,21 +15,20 @@ import static org.junit.matchers.JUnitMatchers.containsString;
 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());
     }