Skip to content

Commit 4157867

Browse files
committed
added JBBPUtils.traceData auxiliary methods
1 parent 43b01ab commit 4157867

File tree

2 files changed

+246
-48
lines changed

2 files changed

+246
-48
lines changed

jbbp/src/main/java/com/igormaznitsa/jbbp/utils/JBBPUtils.java

Lines changed: 137 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.io.ByteArrayOutputStream;
2424
import java.io.Closeable;
2525
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.io.PrintStream;
2628
import java.nio.ByteBuffer;
2729
import java.nio.charset.Charset;
2830
import java.util.ArrayList;
@@ -109,7 +111,8 @@ public static byte[] packInt(final int value) {
109111
} else if ((value & 0xFFFF0000) == 0) {
110112
return new byte[] {(byte) 0x80, (byte) (value >>> 8), (byte) value};
111113
} else {
112-
return new byte[] {(byte) 0x81, (byte) (value >>> 24), (byte) (value >>> 16), (byte) (value >>> 8), (byte) value};
114+
return new byte[] {(byte) 0x81, (byte) (value >>> 24), (byte) (value >>> 16),
115+
(byte) (value >>> 8), (byte) value};
113116
}
114117
}
115118

@@ -159,7 +162,8 @@ public static int unpackInt(final byte[] array, final JBBPIntCounter position) {
159162
final int result;
160163
switch (code) {
161164
case 0x80: {
162-
result = ((array[position.getAndIncrement()] & 0xFF) << 8) | (array[position.getAndIncrement()] & 0xFF);
165+
result = ((array[position.getAndIncrement()] & 0xFF) << 8) |
166+
(array[position.getAndIncrement()] & 0xFF);
163167
}
164168
break;
165169
case 0x81: {
@@ -170,7 +174,8 @@ public static int unpackInt(final byte[] array, final JBBPIntCounter position) {
170174
}
171175
break;
172176
default:
173-
throw new IllegalArgumentException("Unsupported packed integer prefix [0x" + Integer.toHexString(code).toUpperCase(Locale.ENGLISH) + ']');
177+
throw new IllegalArgumentException("Unsupported packed integer prefix [0x"
178+
+ Integer.toHexString(code).toUpperCase(Locale.ENGLISH) + ']');
174179
}
175180
return result;
176181
}
@@ -215,7 +220,9 @@ public static String array2oct(final byte[] array) {
215220
* @param radix the base for conversion
216221
* @return the string representation of the byte array
217222
*/
218-
public static String byteArray2String(final byte[] array, final String prefix, final String delimiter, final boolean brackets, final int radix) {
223+
public static String byteArray2String(final byte[] array, final String prefix,
224+
final String delimiter, final boolean brackets,
225+
final int radix) {
219226
if (array == null) {
220227
return null;
221228
}
@@ -312,7 +319,8 @@ public static String bin2str(final byte[] values, final boolean separateBytes) {
312319
* @param separateBytes if true then bytes will be separated by spaces
313320
* @return the string representation of the array
314321
*/
315-
public static String bin2str(final byte[] values, final JBBPBitOrder bitOrder, final boolean separateBytes) {
322+
public static String bin2str(final byte[] values, final JBBPBitOrder bitOrder,
323+
final boolean separateBytes) {
316324
if (values == null) {
317325
return null;
318326
}
@@ -489,7 +497,8 @@ public static void assertNotNull(final Object object, final String message) {
489497
* @return a string with human readable hexadecimal number representation
490498
*/
491499
public static String int2msg(final int number) {
492-
return number + " (0x" + Long.toHexString((long) number & 0xFFFFFFFFL).toUpperCase(Locale.ENGLISH) + ')';
500+
return number + " (0x"
501+
+ Long.toHexString((long) number & 0xFFFFFFFFL).toUpperCase(Locale.ENGLISH) + ')';
493502
}
494503

495504
/**
@@ -586,7 +595,8 @@ public static byte[] reverseArray(final byte[] nullableArrayToBeInverted) {
586595
* the provided buffer is null or has not enough size
587596
* @since 1.1
588597
*/
589-
public static byte[] splitInteger(final int value, final boolean valueInLittleEndian, final byte[] buffer) {
598+
public static byte[] splitInteger(final int value, final boolean valueInLittleEndian,
599+
final byte[] buffer) {
590600
final byte[] result;
591601
if (buffer == null || buffer.length < 4) {
592602
result = new byte[4];
@@ -620,7 +630,8 @@ public static byte[] splitInteger(final int value, final boolean valueInLittleEn
620630
* the provided buffer is null or has not enough size
621631
* @since 1.1
622632
*/
623-
public static byte[] splitLong(final long value, final boolean valueInLittleEndian, final byte[] buffer) {
633+
public static byte[] splitLong(final long value, final boolean valueInLittleEndian,
634+
final byte[] buffer) {
624635
final byte[] result;
625636
if (buffer == null || buffer.length < 8) {
626637
result = new byte[8];
@@ -783,7 +794,8 @@ public static String ulong2str(final long ulongValue, final int radix, final cha
783794
if (ulongValue > 0) {
784795
result = Long.toString(ulongValue, radix).toUpperCase(Locale.ENGLISH);
785796
} else {
786-
final char[] buffer = charBuffer == null || charBuffer.length < 64 ? new char[64] : charBuffer;
797+
final char[] buffer =
798+
charBuffer == null || charBuffer.length < 64 ? new char[64] : charBuffer;
787799
int pos = buffer.length;
788800
long topPart = ulongValue >>> 32;
789801
long bottomPart = (ulongValue & 0xFFFFFFFFL) + ((topPart % radix) << 32);
@@ -812,7 +824,8 @@ public static String ulong2str(final long ulongValue, final int radix, final cha
812824
* text has equals or greater length.
813825
* @since 1.1
814826
*/
815-
public static String ensureMinTextLength(final String text, final int neededLen, final char ch, final int mode) {
827+
public static String ensureMinTextLength(final String text, final int neededLen, final char ch,
828+
final int mode) {
816829
final int number = neededLen - text.length();
817830
if (number <= 0) {
818831
return text;
@@ -972,8 +985,7 @@ public static int makeMask(final int value) {
972985
int msk = 1;
973986
do {
974987
msk <<= 1;
975-
}
976-
while (msk <= value);
988+
} while (msk <= value);
977989
return msk - 1;
978990
}
979991

@@ -995,4 +1007,117 @@ public static boolean equals(final Object o1, final Object o2) {
9951007
return o1.equals(o2);
9961008
}
9971009

1010+
public static String toHexString(final long value, final int charsNum) {
1011+
String result = Long.toHexString(value).toUpperCase(Locale.ENGLISH);
1012+
if (charsNum >= result.length()) {
1013+
final StringBuilder buffer = new StringBuilder(charsNum);
1014+
for (int i = 0; i < charsNum - result.length(); i++) {
1015+
buffer.append('0');
1016+
}
1017+
buffer.append(result);
1018+
result = buffer.toString();
1019+
}
1020+
return result;
1021+
}
1022+
1023+
/**
1024+
* Trace an input stream into a print writer.
1025+
*
1026+
* @param inStream input stream to be traced, must not be null
1027+
* @param out destination print stream, must not be null
1028+
* @throws IOException thrown if transport error
1029+
* @see #traceData(InputStream, int, String, String, String, String, char, boolean, PrintStream)
1030+
* @since 2.0.3
1031+
*/
1032+
public static void traceData(final InputStream inStream, final PrintStream out)
1033+
throws IOException {
1034+
traceData(
1035+
inStream,
1036+
4,
1037+
8,
1038+
" ",
1039+
" ",
1040+
" | ",
1041+
" ",
1042+
'.',
1043+
true,
1044+
out
1045+
);
1046+
}
1047+
1048+
/**
1049+
* Trace an input stream into a print writer.
1050+
*
1051+
* @param inStream an input stream to be traced, must not be null
1052+
* @param valuesPerColumn number of value in one shown column
1053+
* @param columnsNumber number of eight byte columns
1054+
* @param afterAddressDelimiter string to be written after address section, must not be null
1055+
* @param interValueDelimiter string to be written after each value, must not be null
1056+
* @param interColumnDelimiter string to be written to show column, must not be null
1057+
* @param delimiterBeforeChars string to be written before chars section, must not be null
1058+
* @param nonPrintableChar char to be used for non-printable chars in chars section
1059+
* @param printAsChars true if char section is required, false otherwise
1060+
* @param out destination writer, must not be null
1061+
* @throws IOException thrown if any transport error
1062+
* @since 2.0.3
1063+
*/
1064+
public static void traceData(final InputStream inStream,
1065+
int valuesPerColumn,
1066+
final int columnsNumber,
1067+
final String afterAddressDelimiter,
1068+
final String interValueDelimiter,
1069+
final String interColumnDelimiter,
1070+
final String delimiterBeforeChars,
1071+
final char nonPrintableChar,
1072+
final boolean printAsChars,
1073+
final PrintStream out)
1074+
throws IOException {
1075+
long address = 0L;
1076+
valuesPerColumn = valuesPerColumn <= 0 ? 1 : valuesPerColumn;
1077+
final int bytesPerLine = columnsNumber <= 0 ? 8 : columnsNumber * valuesPerColumn;
1078+
1079+
final StringBuilder charBuffer = printAsChars ? new StringBuilder(bytesPerLine) : null;
1080+
1081+
int lineByteCounter = 0;
1082+
1083+
boolean ending = false;
1084+
1085+
while (!Thread.currentThread().isInterrupted()) {
1086+
final int nextData;
1087+
if (ending) {
1088+
nextData = -1;
1089+
} else {
1090+
nextData = inStream.read();
1091+
ending = nextData < 0;
1092+
}
1093+
1094+
if (lineByteCounter == 0) {
1095+
out.print(toHexString(address, 8));
1096+
out.print(afterAddressDelimiter);
1097+
}
1098+
if (charBuffer != null) {
1099+
charBuffer.append(nextData > 0x1F && nextData < 0xFF ? (char) nextData : nonPrintableChar);
1100+
}
1101+
out.print(nextData < 0 ? "--" : toHexString(nextData, 2));
1102+
lineByteCounter++;
1103+
if (lineByteCounter == bytesPerLine) {
1104+
if (charBuffer != null) {
1105+
out.print(delimiterBeforeChars);
1106+
out.print(charBuffer.toString());
1107+
charBuffer.setLength(0);
1108+
}
1109+
lineByteCounter = 0;
1110+
address += bytesPerLine;
1111+
out.println();
1112+
if (ending) {
1113+
break;
1114+
}
1115+
} else if (lineByteCounter % valuesPerColumn == 0) {
1116+
out.print(interColumnDelimiter);
1117+
} else {
1118+
out.print(interValueDelimiter);
1119+
}
1120+
}
1121+
}
1122+
9981123
}

0 commit comments

Comments
 (0)