Skip to content

Commit

Permalink
Merge pull request #16 from daniel-beck/master
Browse files Browse the repository at this point in the history
More readable diagnostic for JENKINS-8856
  • Loading branch information
jglick committed Aug 9, 2013
2 parents a567306 + 2a6c74d commit 8b50957
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/main/java/hudson/remoting/HexDump.java
Expand Up @@ -11,19 +11,35 @@ public static String toHex(byte[] buf) {
}
public static String toHex(byte[] buf, int start, int len) {
StringBuilder r = new StringBuilder(len*2);
boolean inText = false;
for (int i=0; i<len; i++) {
if (i > 0) {
r.append(' ');
}
byte b = buf[start+i];
if (b >= 0x20 && b <= 0x7e) {
r.append('\'').append((char) b).append('\'');
if (!inText) {
inText = true;
r.append('\'');
}
r.append((char) b);
} else {
if (inText) {
r.append("' ");
inText = false;
}
r.append("0x");
r.append(CODE.charAt((b>>4)&15));
r.append(CODE.charAt(b&15));
if (i < len - 1) {
if (b == 10) {
r.append('\n');
} else {
r.append(' ');
}
}
}
}
if (inText) {
r.append('\'');
}
return r.toString();
}
}
5 changes: 5 additions & 0 deletions src/test/java/hudson/remoting/HexDumpTest.java
Expand Up @@ -8,5 +8,10 @@
public class HexDumpTest extends TestCase {
public void test1() {
assertEquals("0x00 0x01 0xff 'A'", HexDump.toHex(new byte[] {0, 1, -1, 65}));
assertEquals("0x00 0x01 0xff 'ABC'", HexDump.toHex(new byte[] {0, 1, -1, 65, 66, 67}));
assertEquals("'AAAA' 0x00", HexDump.toHex(new byte[] {65, 65, 65, 65, 0}));
}
public void testMultiline() {
assertEquals("'A A' 0x0a\n' AA'", HexDump.toHex(new byte[] {65, 32, 65, 10, 32, 65, 65}));
}
}

0 comments on commit 8b50957

Please sign in to comment.