Fix warnings reported in toaster
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / messages / NetconfMessageFactory.java
index 526708ab580238e34ba26a1c70a1d89289571ca6..6f86cc37f92bcb6ba93c352e6475e72935df2730 100644 (file)
@@ -20,7 +20,6 @@ import org.opendaylight.controller.netconf.util.xml.XmlUtil;
 import org.opendaylight.protocol.framework.DeserializerException;
 import org.opendaylight.protocol.framework.DocumentedException;
 import org.opendaylight.protocol.framework.ProtocolMessageFactory;
-import org.opendaylight.protocol.util.ByteArray;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Comment;
@@ -29,7 +28,7 @@ import org.xml.sax.SAXException;
 
 import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
-import com.google.common.collect.Lists;
+import com.google.common.collect.ImmutableList;
 
 /**
  * NetconfMessageFactory for (de)serializing DOM documents.
@@ -37,6 +36,10 @@ import com.google.common.collect.Lists;
 public final class NetconfMessageFactory implements ProtocolMessageFactory<NetconfMessage> {
 
     private static final Logger logger = LoggerFactory.getLogger(NetconfMessageFactory.class);
+    private static final List<byte[]> POSSIBLE_STARTS = ImmutableList.of(
+        "[".getBytes(Charsets.UTF_8), "\r\n[".getBytes(Charsets.UTF_8), "\n[".getBytes(Charsets.UTF_8));
+    private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
+        "]\n".getBytes(Charsets.UTF_8), "]\r\n".getBytes(Charsets.UTF_8));
 
     private final Optional<String> clientId;
 
@@ -73,9 +76,34 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
         return message;
     }
 
+    private static int findByteSequence(final byte[] bytes, final byte[] sequence) {
+        if (bytes.length < sequence.length) {
+            throw new IllegalArgumentException("Sequence to be found is longer than the given byte array.");
+        }
+        if (bytes.length == sequence.length) {
+            if (Arrays.equals(bytes, sequence)) {
+                return 0;
+            } else {
+                return -1;
+            }
+        }
+        int j = 0;
+        for (int i = 0; i < bytes.length; i++) {
+            if (bytes[i] == sequence[j]) {
+                j++;
+                if (j == sequence.length) {
+                    return i - j + 1;
+                }
+            } else {
+                j = 0;
+            }
+        }
+        return -1;
+    }
+
     private int getAdditionalHeaderEndIndex(byte[] bytes) {
-        for (String possibleEnd : Lists.newArrayList("]\n", "]\r\n")) {
-            int idx = ByteArray.findByteSequence(bytes, possibleEnd.getBytes(Charsets.UTF_8));
+        for (byte[] possibleEnd : POSSIBLE_ENDS) {
+            int idx = findByteSequence(bytes, possibleEnd);
 
             if (idx != -1) {
                 return idx;
@@ -86,11 +114,10 @@ public final class NetconfMessageFactory implements ProtocolMessageFactory<Netco
     }
 
     private boolean startsWithAdditionalHeader(byte[] bytes) {
-        List<String> possibleStarts = Lists.newArrayList("[", "\r\n[", "\n[");
-        for (String possibleStart : possibleStarts) {
+        for (byte[] possibleStart : POSSIBLE_STARTS) {
             int i = 0;
-            for (byte b : possibleStart.getBytes(Charsets.UTF_8)) {
-                if(bytes[i]!=b)
+            for (byte b : possibleStart) {
+                if(bytes[i] != b)
                     break;
 
                 return true;