Use byte[].clone()
[bgpcep.git] / bgp / parser-api / src / main / java / org / opendaylight / protocol / bgp / parser / BGPDocumentedException.java
index a97c5d7679dc1ee29d27aa7bf2f8174feb3d7916..e0649bf11a69dd2a5c9a29f16ae1d66179974f65 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.protocol.bgp.parser;
 
 import com.google.common.base.Preconditions;
 import com.google.common.primitives.UnsignedBytes;
-import java.util.Arrays;
 import org.opendaylight.protocol.util.Values;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -24,6 +23,8 @@ public final class BGPDocumentedException extends Exception {
 
     private static final Logger LOG = LoggerFactory.getLogger(BGPDocumentedException.class);
 
+    private static final byte[] EMPTY = new byte[0];
+
     private final BGPError error;
 
     private final byte[] data;
@@ -80,7 +81,7 @@ public final class BGPDocumentedException extends Exception {
     public BGPDocumentedException(final String message, final BGPError error, final byte[] data, final Exception cause) {
         super(message, cause);
         this.error = error;
-        this.data = data == null ? null : Arrays.copyOf(data, data.length);
+        this.data = data == null || data.length == 0 ? null : data.clone();
         LOG.error("Error = {}", error, this);
     }
 
@@ -90,7 +91,7 @@ public final class BGPDocumentedException extends Exception {
      * @return documented error
      */
     public BGPError getError() {
-        return this.error;
+        return error;
     }
 
     /**
@@ -99,7 +100,7 @@ public final class BGPDocumentedException extends Exception {
      * @return byte array data
      */
     public byte[] getData() {
-        return (this.data != null) ? Arrays.copyOf(this.data, this.data.length) : new byte[0];
+        return data != null ? data.clone() : EMPTY;
     }
 
     public static BGPDocumentedException badMessageLength(final String message, final int length) {
@@ -108,6 +109,5 @@ public final class BGPDocumentedException extends Exception {
         return new BGPDocumentedException(message, BGPError.BAD_MSG_LENGTH, new byte[] {
             UnsignedBytes.checkedCast(length / (Values.UNSIGNED_BYTE_MAX_VALUE + 1)),
             UnsignedBytes.checkedCast(length % (Values.UNSIGNED_BYTE_MAX_VALUE + 1)) });
-
     }
 }