|
18 | 18 | package org.jsl.collider; |
19 | 19 |
|
20 | 20 | import java.nio.ByteBuffer; |
21 | | -import java.util.Arrays; |
22 | 21 |
|
23 | 22 | public class Util |
24 | 23 | { |
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'}; |
30 | 25 |
|
31 | | - public static String formatDelay( long startTime, long endTime ) |
| 26 | + public static String formatDelay(long startTime, long endTime) |
32 | 27 | { |
33 | 28 | final long delay = ((endTime - startTime) / 1000); |
34 | 29 | if (delay > 0) |
35 | | - return String.format( "%d.%06d", delay/1000000, delay%1000000 ); |
| 30 | + return String.format("%d.%06d", delay/1000000, delay%1000000); |
36 | 31 | return "0.0"; |
37 | 32 | } |
38 | 33 |
|
39 | | - public static String hexDump( ByteBuffer bb ) |
| 34 | + public static void hexDump(ByteBuffer byteBuffer, StringBuilder sb, int maxLines) |
40 | 35 | { |
41 | | - int pos = bb.position(); |
42 | | - int limit = bb.limit(); |
| 36 | + int pos = byteBuffer.position(); |
| 37 | + int limit = byteBuffer.limit(); |
43 | 38 | if (pos == limit) |
44 | | - return "<empty>"; |
| 39 | + { |
| 40 | + sb.append("<empty>"); |
| 41 | + return; |
| 42 | + } |
45 | 43 |
|
46 | | - if (limit > 0xFFFF) |
47 | | - limit = 0xFFFF; |
| 44 | + int c = ((limit - pos) / 16); |
| 45 | + if (c > maxLines) |
| 46 | + limit = (pos + (maxLines * 16)); |
48 | 47 |
|
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) |
65 | 56 | { |
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++) |
75 | 81 | { |
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); |
100 | 85 | } |
101 | 86 |
|
102 | | - sb.append( buf ); |
| 87 | + sb.append('\n'); |
| 88 | + pos += n; |
103 | 89 | } |
| 90 | + } |
104 | 91 |
|
| 92 | + public static String hexDump(ByteBuffer byteBuffer, int maxLines) |
| 93 | + { |
| 94 | + final StringBuilder sb = new StringBuilder(); |
| 95 | + hexDump(byteBuffer, sb, maxLines); |
105 | 96 | return sb.toString(); |
106 | 97 | } |
107 | 98 |
|
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) |
109 | 105 | { |
110 | | - return hexDump( bb.getNioByteBuffer() ); |
| 106 | + return hexDump(bb.getNioByteBuffer()); |
111 | 107 | } |
112 | 108 | } |
0 commit comments