Skip to content

Commit 44b81d0

Browse files
committed
hexDump to the StringBuilder instead of return String.
1 parent 27a25e8 commit 44b81d0

File tree

1 file changed

+62
-66
lines changed

1 file changed

+62
-66
lines changed

src/main/java/org/jsl/collider/Util.java

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,95 +18,91 @@
1818
package org.jsl.collider;
1919

2020
import java.nio.ByteBuffer;
21-
import java.util.Arrays;
2221

2322
public class Util
2423
{
25-
private static final char [] HD =
26-
{
27-
'0', '1', '2', '3', '4', '5', '6', '7',
28-
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
29-
};
24+
private static final char [] HD = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
3025

31-
public static String formatDelay( long startTime, long endTime )
26+
public static String formatDelay(long startTime, long endTime)
3227
{
3328
final long delay = ((endTime - startTime) / 1000);
3429
if (delay > 0)
35-
return String.format( "%d.%06d", delay/1000000, delay%1000000 );
30+
return String.format("%d.%06d", delay/1000000, delay%1000000);
3631
return "0.0";
3732
}
3833

39-
public static String hexDump( ByteBuffer bb )
34+
public static void hexDump(ByteBuffer byteBuffer, StringBuilder sb, int maxLines)
4035
{
41-
int pos = bb.position();
42-
int limit = bb.limit();
36+
int pos = byteBuffer.position();
37+
int limit = byteBuffer.limit();
4338
if (pos == limit)
44-
return "<empty>";
39+
{
40+
sb.append("<empty>");
41+
return;
42+
}
4543

46-
if (limit > 0xFFFF)
47-
limit = 0xFFFF;
44+
int c = ((limit - pos) / 16);
45+
if (c > maxLines)
46+
limit = (pos + (maxLines * 16));
4847

49-
final StringBuilder sb = new StringBuilder();
50-
/* "0000: 00 01 02 03 04 05 06 07-08 09 0A 0B 0C 0D 0E 0F | ................" */
51-
sb.append( " 0 1 2 3 4 5 6 7 8 9 A B C D E F [" );
52-
sb.append( pos );
53-
sb.append( ", " );
54-
sb.append( limit );
55-
sb.append( "]\n" );
56-
57-
final char [] buf = new char[73];
58-
Arrays.fill( buf, ' ' );
59-
buf[4] = ':';
60-
buf[29] = '-';
61-
buf[54] = '|';
62-
buf[72] = '\n';
63-
64-
loop: while (pos < limit)
48+
/* "0000: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................" */
49+
sb.append(" 0 1 2 3 4 5 6 7 8 9 A B C D E F [");
50+
sb.append(pos);
51+
sb.append(", ");
52+
sb.append(limit);
53+
sb.append("]\n");
54+
55+
while (pos < limit)
6556
{
66-
final int pp = (pos - bb.position());
67-
buf[0] = HD[(pp >> 12) & 0xF];
68-
buf[1] = HD[(pp >> 8) & 0xF];
69-
buf[2] = HD[(pp >> 4) & 0xF];
70-
buf[3] = HD[pp & 0xF];
71-
72-
int hp = 6;
73-
int cp = 56;
74-
for (int idx=0;;)
57+
int p = (pos - byteBuffer.position());
58+
sb.append(HD[(p >> 12) & 0x0F]);
59+
sb.append(HD[(p >> 8) & 0x0F]);
60+
sb.append(HD[(p >> 4) & 0x0F]);
61+
sb.append(HD[p & 0x0F]);
62+
sb.append(": ");
63+
64+
int n = Math.min((limit - pos), 16);
65+
p = pos;
66+
for (c=n; c>0; c--, p++)
67+
{
68+
final int v = (((int)byteBuffer.get(p)) & 0xFF);
69+
sb.append(HD[(v >> 4)]);
70+
sb.append(HD[v & 0x0F]);
71+
sb.append(' ');
72+
}
73+
74+
for (c=(16-n); c>0; c--)
75+
sb.append(" ");
76+
77+
sb.append("| ");
78+
79+
p = pos;
80+
for (c=n; c>0; c--, p++)
7581
{
76-
final int v = bb.get( pos );
77-
buf[hp++] = HD[(v >> 4) & 0xF];
78-
buf[hp] = HD[v & 0xF];
79-
hp+= 2;
80-
81-
buf[cp++] = ((v >= 32) && (v < 128)) ? (char)v : '.';
82-
83-
pos++;
84-
85-
if (++idx == 16)
86-
break;
87-
88-
if (pos == limit)
89-
{
90-
for (; idx<16; idx++)
91-
{
92-
buf[hp++] = ' ';
93-
buf[hp] = ' ';
94-
hp += 2;
95-
buf[cp++] = ' ';
96-
}
97-
sb.append( buf );
98-
break loop;
99-
}
82+
final int v = byteBuffer.get(p);
83+
final char vc = ((v >= 32) ? (char)v : '.');
84+
sb.append(vc);
10085
}
10186

102-
sb.append( buf );
87+
sb.append('\n');
88+
pos += n;
10389
}
90+
}
10491

92+
public static String hexDump(ByteBuffer byteBuffer, int maxLines)
93+
{
94+
final StringBuilder sb = new StringBuilder();
95+
hexDump(byteBuffer, sb, maxLines);
10596
return sb.toString();
10697
}
10798

108-
public static String hexDump( RetainableByteBuffer bb )
99+
public static String hexDump(ByteBuffer byteBuffer)
100+
{
101+
return hexDump(byteBuffer, 10);
102+
}
103+
104+
public static String hexDump(RetainableByteBuffer bb)
109105
{
110-
return hexDump( bb.getNioByteBuffer() );
106+
return hexDump(bb.getNioByteBuffer());
111107
}
112108
}

0 commit comments

Comments
 (0)