1 package org.djutils.serialization;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6 import org.djutils.decoderdumper.Dumper;
7 import org.djutils.decoderdumper.FixedString;
8 import org.djutils.decoderdumper.HexAddressDecoder;
9 import org.djutils.decoderdumper.HexDecoder;
10
11 /**
12 * Dumper for serialized data.
13 * <p>
14 * Copyright (c) 2019-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
15 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
16 * <p>
17 * version Jun 27, 2019 <br>
18 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
19 * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
20 */
21 public class SerialDataDumper extends Dumper<SerialDataDumper>
22 {
23 /**
24 * Construct a new SerialDataDumper.
25 * @param endianUtil EndianUtil; used to decode multi-byte values
26 * @param addressOffset int; address of the first byte that will be processed
27 */
28 public SerialDataDumper(final EndianUtil endianUtil, final int addressOffset)
29 {
30 super(addressOffset);
31 addDecoder(new HexAddressDecoder(16));
32 addDecoder(new FixedString(": "));
33 addDecoder(new HexDecoder(16, 8));
34 addDecoder(new FixedString(" "));
35 addDecoder(new SerialDataDecoder(endianUtil));
36 addDecoder(new FixedString("\n"));
37 }
38
39 /**
40 * Construct a new SerialDataDumper.
41 * @param endianUtil EndianUtil; used to decode multi-byte values
42 */
43 public SerialDataDumper(final EndianUtil endianUtil)
44 {
45 this(endianUtil, 0);
46 }
47
48 /**
49 * Create a SerialDataDumper object; use it to dump an array of bytes and return the dump as a String.
50 * @param endianUtil EndianUtil; used to decode multi-byte values
51 * @param addressOffset int; address of the first byte
52 * @param bytes byte[]; the bytes to hex-dump
53 * @return String; the hexadecimal and character dump of the <code>bytes</code>
54 */
55 public static String serialDataDumper(final EndianUtil endianUtil, final int addressOffset, final byte[] bytes)
56 {
57 ByteArrayOutputStream baos = new ByteArrayOutputStream();
58 try
59 {
60 new SerialDataDumper(endianUtil, addressOffset).setOutputStream(baos).append(bytes).flush();
61 }
62 catch (IOException exception)
63 {
64 // Cannot happen because ByteOutputStream.write(byte[]) cannot fail
65 }
66 return baos.toString();
67 }
68
69 /**
70 * Create a SerialDataDumper object with addressOffset 0; use it to dump an array of bytes and return the dump as a String.
71 * @param endianUtil EndianUtil; used to decode multi-byte values
72 * @param bytes byte[]; the bytes to hex-dump
73 * @return String; the hexadecimal and character dump of the <code>bytes</code>
74 */
75 public static String serialDataDumper(final EndianUtil endianUtil, final byte[] bytes)
76 {
77 return serialDataDumper(endianUtil, 0, bytes);
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public String toString()
83 {
84 return "SerialDataDumper [super=" + super.toString() + "]";
85 }
86
87 }