You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/12/03 17:59:36 UTC

[plc4x] branch develop updated: plc4j-protocol-ads: fix encodeBigInteger in LittleEndianEncoder (#286)

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

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new bda80dc  plc4j-protocol-ads: fix encodeBigInteger in LittleEndianEncoder (#286)
bda80dc is described below

commit bda80dcd5aec1f2ec7e2f7e4d47be7fcafedd456
Author: Richard Meister <ri...@gmail.com>
AuthorDate: Fri Dec 3 18:59:32 2021 +0100

    plc4j-protocol-ads: fix encodeBigInteger in LittleEndianEncoder (#286)
---
 .../java/ads/protocol/util/LittleEndianEncoder.java    | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
index 16dd845..8a21372 100644
--- a/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
+++ b/plc4j/protocols/ads/src/main/java/org/apache/plc4x/java/ads/protocol/util/LittleEndianEncoder.java
@@ -187,20 +187,16 @@ public class LittleEndianEncoder {
     }
 
     private static Stream<byte[]> encodeBigInteger(AdsDataType adsDataType, Stream<BigInteger> bigIntegerStream) {
-        // TODO: add boundchecks and add optional extension
         return bigIntegerStream
+            .peek(value -> {
+                if (value.longValue() < adsDataType.getLowerBound().longValue() || value.longValue() > adsDataType.getUpperBound().longValue())
+                    throw new PlcRuntimeException(value + " not within bounds of " + adsDataType);
+            })
             .map(bigIntValue -> {
                 byte[] bytes = bigIntValue.toByteArray();
-                if (bytes.length > 1 && bytes[0] == 0x0) {
-                    byte[] subArray = Arrays.copyOf(ArrayUtils.subarray(bytes, 1, bytes.length), adsDataType.getTargetByteSize());
-                    ArrayUtils.reverse(subArray);
-                    return subArray;
-                } else {
-                    ArrayUtils.reverse(Arrays.copyOf(bytes, adsDataType.getTargetByteSize()));
-                    return bytes;
-                }
-            })
-            .map(bytes -> Arrays.copyOf(bytes, adsDataType.getTargetByteSize()));
+                ArrayUtils.reverse(bytes); /* reverse first, so we don't truncate the wrong end */
+                return Arrays.copyOf(bytes, adsDataType.getTargetByteSize());
+            });
     }
 
     private static Stream<byte[]> encodeLocalTime(AdsDataType adsDataType, Stream<LocalTime> localTimeStream) {