01: package net.sourceforge.jtds.tools;
02:
03: import java.io.*;
04:
05: public class PacketLogger {
06: PrintStream out;
07:
08: static String hexstring = "0123456789ABCDEF";
09:
10: static String hex(byte b) {
11: int ln = (int) (b & 0xf);
12: int hn = (int) ((b & 0xf0) >> 4);
13: return "" + hexstring.charAt(hn) + hexstring.charAt(ln);
14: }
15:
16: static String hex(short b) {
17: byte lb = (byte) (b & 0x00ff);
18: byte hb = (byte) ((b & 0xff00) >> 8);
19: return hex(hb) + hex(lb);
20: }
21:
22: public PacketLogger(String filename) throws IOException {
23: out = new PrintStream(new FileOutputStream(new File(filename)));
24: }
25:
26: public void log(byte[] packet) {
27: short pos = 0;
28: while (pos < packet.length) {
29: out.print(hex(pos) + ": ");
30: short startpos = pos;
31: pos += 16;
32: if (pos > packet.length)
33: pos = (short) packet.length;
34: for (short i = startpos; i < pos; i++) {
35: out.print(hex(packet[i]) + " ");
36: }
37: for (short i = pos; i < startpos + 16; i++)
38: out.print(" ");
39: out.print(" ");
40: for (short i = startpos; i < startpos + 16; i++) {
41: if (i >= pos)
42: out.print(" ");
43: else if (packet[i] < 32)
44: out.print(".");
45: else
46: out.print((char) packet[i]);
47: }
48: out.println("");
49: }
50: out.println("");
51: }
52: }
|