summaryrefslogtreecommitdiffstats
path: root/bcprov/src/main/java/org/bouncycastle/crypto/tls/test/LoggingDatagramTransport.java
blob: 41816193c08312bad470c563ac163136c917c213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package org.bouncycastle.crypto.tls.test;

import java.io.IOException;
import java.io.PrintStream;

import org.bouncycastle.crypto.tls.DatagramTransport;

public class LoggingDatagramTransport
    implements DatagramTransport
{

    private static final String HEX_CHARS = "0123456789ABCDEF";

    private final DatagramTransport transport;
    private final PrintStream output;
    private final long launchTimestamp;

    public LoggingDatagramTransport(DatagramTransport transport, PrintStream output)
    {
        this.transport = transport;
        this.output = output;
        this.launchTimestamp = System.currentTimeMillis();
    }

    public int getReceiveLimit()
        throws IOException
    {
        return transport.getReceiveLimit();
    }

    public int getSendLimit()
        throws IOException
    {
        return transport.getSendLimit();
    }

    public int receive(byte[] buf, int off, int len, int waitMillis)
        throws IOException
    {
        int length = transport.receive(buf, off, len, waitMillis);
        if (length >= 0)
        {
            dumpDatagram("Received", buf, off, length);
        }
        return length;
    }

    public void send(byte[] buf, int off, int len)
        throws IOException
    {
        dumpDatagram("Sending", buf, off, len);
        transport.send(buf, off, len);
    }

    public void close()
        throws IOException
    {
    }

    private void dumpDatagram(String verb, byte[] buf, int off, int len)
        throws IOException
    {
        long timestamp = System.currentTimeMillis() - launchTimestamp;
        StringBuffer sb = new StringBuffer("(+" + timestamp + "ms) " + verb + " " + len + " byte datagram:");
        for (int pos = 0; pos < len; ++pos)
        {
            if (pos % 16 == 0)
            {
                sb.append(System.getProperty("line.separator"));
                sb.append("    ");
            }
            else if (pos % 16 == 8)
            {
                sb.append('-');
            }
            else
            {
                sb.append(' ');
            }
            int val = buf[off + pos] & 0xFF;
            sb.append(HEX_CHARS.charAt(val >> 4));
            sb.append(HEX_CHARS.charAt(val & 0xF));
        }
        dump(sb.toString());
    }

    private synchronized void dump(String s)
    {
        output.println(s);
    }
}