You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2023/01/13 18:50:34 UTC
[isis] 01/02: ISIS-3304: adds some byte IO algorithms (basically for JUnit testing)
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
commit 3aee62ab6f58eecb3d0ee281d557f3fea7b9b1ff
Author: Andi Huber <ah...@apache.org>
AuthorDate: Fri Jan 13 19:49:12 2023 +0100
ISIS-3304: adds some byte IO algorithms (basically for JUnit testing)
---
.../causeway/commons/internal/base/_Bytes.java | 69 +++++++++++++++++++++-
1 file changed, 68 insertions(+), 1 deletion(-)
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
index 65cf4b01e4..4a88b91c1c 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
@@ -23,10 +23,13 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.function.UnaryOperator;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
import org.springframework.lang.Nullable;
import lombok.NonNull;
+import lombok.val;
/**
* <h1>- internal use only -</h1>
@@ -96,6 +99,71 @@ public final class _Bytes {
}
}
+ // -- ARRAY TO STREAM AND VICE VERSA
+
+ /**
+ * Converts given byte array into a stream of int values,
+ * while the element wise type conversion is preserving sign.
+ * @apiNote The Java byte type is signed.
+ * @see #ofIntStream(IntStream)
+ */
+ public static IntStream streamAsInts(final @Nullable byte[] bytes) {
+ if(bytes==null
+ || bytes.length==0) {
+ return IntStream.empty();
+ }
+ return IntStream.range(0, bytes.length)
+ .map(index->(int)bytes[index]);
+ }
+
+ /**
+ * Converts given {@link IntStream} into a byte array,
+ * while the element wise type conversion is preserving sign,
+ * but ignoring overflow.
+ * @apiNote The Java byte type is signed.
+ * @implNote certainly not the most sufficient algorithm, as we resort to boxing and temporary list creation
+ * @see #streamAsInts(byte[])
+ */
+ public static byte[] ofIntStream(final @Nullable IntStream intStream) {
+ if(intStream==null) {
+ return new byte[0];
+ }
+ val listOfInts = intStream
+ .boxed()
+ .collect(Collectors.toList());
+ val bytes = new byte[listOfInts.size()];
+ IntStream.range(0, listOfInts.size())
+ .forEach(index->bytes[index]=(byte)(int)listOfInts.get(index));
+ return bytes;
+ }
+
+ // -- TO AND FROM HEX DUMP
+
+ /**
+ * Converts given byte array into a space separated list of 2 character fixed length hex numbers.
+ * @apiNote future extension code support pretty printing, as for now the resulting string is a single line
+ * @see #ofHexDump(String)
+ */
+ public static String hexDump(final @Nullable byte[] bytes) {
+ if(bytes==null) {
+ return "";
+ }
+ return _Bytes.streamAsInts(bytes).mapToObj(Integer::toHexString).collect(Collectors.joining(" "));
+ }
+
+ /**
+ * Converts given space separated list of 2 character fixed length hex numbers into a byte array.
+ * @see #hexDump(byte[])
+ */
+ public static byte[] ofHexDump(final @Nullable String hexDump) {
+ if(hexDump==null) {
+ return new byte[0];
+ }
+ final IntStream intStream = _Strings.splitThenStream(hexDump, " ")
+ .mapToInt(hex->Integer.parseUnsignedInt(hex, 16));
+ return ofIntStream(intStream);
+ }
+
// -- PREPEND/APPEND
/**
@@ -272,7 +340,6 @@ public final class _Bytes {
.andThen(bytes->decodeBase64(Base64.getDecoder(), bytes))
.andThen(_Bytes::decompress);
-
// --