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