You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2018/03/05 20:45:56 UTC

[incubator-plc4x] 02/02: Refactored LittleEndianEncoder to avoid that one D-Level SonarQube warning.

This is an automated email from the ASF dual-hosted git repository.

cdutz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-plc4x.git

commit a486574f3ea1b1a0de5d803631fe887d5b63f058
Author: Christofer Dutz <ch...@c-ware.de>
AuthorDate: Mon Mar 5 21:45:39 2018 +0100

    Refactored LittleEndianEncoder to avoid that one D-Level SonarQube warning.
---
 .../java/ads/netty/util/LittleEndianEncoder.java   | 31 +++++++++++++---------
 1 file changed, 18 insertions(+), 13 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianEncoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianEncoder.java
index 20c84af..329d60b 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianEncoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/netty/util/LittleEndianEncoder.java
@@ -20,6 +20,7 @@ package org.apache.plc4x.java.ads.netty.util;
 
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.plc4x.java.ads.api.commands.types.TimeStamp;
+import org.apache.plc4x.java.api.exceptions.PlcProtocolException;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -36,7 +37,7 @@ public class LittleEndianEncoder {
         // Utility class
     }
 
-    public static byte[] encodeData(Class<?> valueType, Object[] values) {
+    public static byte[] encodeData(Class<?> valueType, Object[] values) throws PlcProtocolException {
         if (values.length == 0) {
             return new byte[]{};
         }
@@ -56,21 +57,25 @@ public class LittleEndianEncoder {
         } else if (valueType == String.class) {
             result = encodeString(Arrays.stream(values).map(String.class::cast));
         } else {
-            throw new IllegalArgumentException("Unsupported type" + valueType);
+            throw new PlcProtocolException("Unsupported datatype " + valueType);
         }
 
         // TODO: maybe we can replace this by a smarter flatmap
-        return result.collect(
-            ByteArrayOutputStream::new,
-            (bos, byteValue) -> {
-                try {
-                    bos.write(byteValue);
-                } catch (IOException e) {
-                    throw new RuntimeException(e);
-                }
-            },
-            (a, b) -> {
-            }).toByteArray();
+        try {
+            return result.collect(
+                ByteArrayOutputStream::new,
+                (bos, byteValue) -> {
+                    try {
+                        bos.write(byteValue);
+                    } catch (IOException e) {
+                        throw new RuntimeException(e);
+                    }
+                },
+                (a, b) -> {
+                }).toByteArray();
+        } catch (RuntimeException e) {
+            throw new PlcProtocolException("Error encoding data", e);
+        }
     }
 
     public static Stream<byte[]> encodeString(Stream<String> stringStream) {

-- 
To stop receiving notification emails like this one, please contact
cdutz@apache.org.