From 945044127ca30db37ac6f2fdb9c8945800489063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20R=C3=BChl?= Date: Fri, 14 Jan 2022 19:34:05 +0100 Subject: [PATCH] feat(spi): Added highlight function to hex to highlight byte positions --- .../spi/generation/io/MyDefaultBitInput.java | 17 +++++++++++++++-- .../apache/plc4x/java/spi/utils/hex/Hex.java | 11 ++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/io/MyDefaultBitInput.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/io/MyDefaultBitInput.java index 352418c68b5..7b0e2728a87 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/io/MyDefaultBitInput.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/generation/io/MyDefaultBitInput.java @@ -20,6 +20,9 @@ import com.github.jinahya.bit.io.ArrayByteInput; import com.github.jinahya.bit.io.DefaultBitInput; +import org.apache.plc4x.java.spi.utils.hex.Hex; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -28,6 +31,8 @@ */ public class MyDefaultBitInput extends DefaultBitInput { + private static final Logger LOGGER = LoggerFactory.getLogger(MyDefaultBitInput.class); + public MyDefaultBitInput(ArrayByteInput delegate) { super(delegate); } @@ -38,11 +43,19 @@ public long getPos() { } public void reset(int pos) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Before\n{}", Hex.dump(delegate.getSource(), Hex.DefaultWidth, delegate.getIndex())); + } try { - align(1); + long align = align(1); + LOGGER.debug("aligned {}", align); } catch (IOException e) { - e.printStackTrace(); + throw new RuntimeException(e); } delegate.setIndex(pos); + LOGGER.debug("set to index {}", pos); + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("After\n{}", Hex.dump(delegate.getSource(), Hex.DefaultWidth, pos)); + } } } diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/utils/hex/Hex.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/utils/hex/Hex.java index 63b639489bb..04e14a6fe5a 100644 --- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/utils/hex/Hex.java +++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/utils/hex/Hex.java @@ -29,6 +29,8 @@ import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; public class Hex { @@ -75,10 +77,11 @@ public static String dump(byte[] data) { } // DumpFixedWidth dumps hex as hex string. Min width of string returned is 18 up to supplied charWidth - public static String dump(byte[] data, int desiredCharWidth) { + public static String dump(byte[] data, int desiredCharWidth, int... highlights) { if (data == null || data.length < 1) { return ""; } + Set highlightsSet = Arrays.stream(highlights).boxed().collect(Collectors.toSet()); // We copy the array to avoid mutations data = Arrays.copyOf(data, data.length); StringBuilder hexString = new StringBuilder(); @@ -91,7 +94,13 @@ public static String dump(byte[] data, int desiredCharWidth) { for (int columnIndex = 0; columnIndex < maxBytesPerRow; columnIndex++) { int absoluteIndex = byteIndex + columnIndex; if (absoluteIndex < data.length) { + if(highlightsSet.contains(absoluteIndex)) { + hexString.append("\033[0;31m"); + } hexString.append(String.format("%02x ", data[absoluteIndex])); + if(highlightsSet.contains(absoluteIndex)) { + hexString.append("\033[0m"); + } } else { // align with empty byte representation hexString.append(StringUtils.repeat(" ", byteWidth));