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="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>. 16 * <p> 17 * $LastChangedDate: 2019-06-07 01:33:02 +0200 (Mon, 7 Jun 2019) $, @version $Revision: 1401 $, by $Author: pknoppers $, initial 18 * version Jun 27, 2019 <br> 19 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 20 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 21 */ 22 public class SerialDataDumper extends Dumper<SerialDataDumper> 23 { 24 /** 25 * Construct a new SerialDataDumper. 26 * @param endianUtil EndianUtil; used to decode multi-byte values 27 * @param addressOffset int; address of the first byte that will be processed 28 */ 29 public SerialDataDumper(final EndianUtil endianUtil, final int addressOffset) 30 { 31 super(addressOffset); 32 addDecoder(new HexAddressDecoder(16)); 33 addDecoder(new FixedString(": ")); 34 addDecoder(new HexDecoder(16, 8)); 35 addDecoder(new FixedString(" ")); 36 addDecoder(new SerialDataDecoder(endianUtil)); 37 addDecoder(new FixedString("\n")); 38 } 39 40 /** 41 * Construct a new SerialDataDumper. 42 * @param endianUtil EndianUtil; used to decode multi-byte values 43 */ 44 public SerialDataDumper(final EndianUtil endianUtil) 45 { 46 this(endianUtil, 0); 47 } 48 49 /** 50 * Create a SerialDataDumper object; use it to dump an array of bytes and return the dump as a String. 51 * @param endianUtil EndianUtil; used to decode multi-byte values 52 * @param addressOffset int; address of the first byte 53 * @param bytes byte[]; the bytes to hex-dump 54 * @return String; the hexadecimal and character dump of the <code>bytes</code> 55 */ 56 public static String serialDataDumper(final EndianUtil endianUtil, final int addressOffset, final byte[] bytes) 57 { 58 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 59 try 60 { 61 new SerialDataDumper(endianUtil, addressOffset).setOutputStream(baos).append(bytes).flush(); 62 } 63 catch (IOException exception) 64 { 65 // Cannot happen because ByteOutputStream.write(byte[]) cannot fail 66 } 67 return baos.toString(); 68 } 69 70 /** 71 * Create a SerialDataDumper object with addressOffset 0; use it to dump an array of bytes and return the dump as a String. 72 * @param endianUtil EndianUtil; used to decode multi-byte values 73 * @param bytes byte[]; the bytes to hex-dump 74 * @return String; the hexadecimal and character dump of the <code>bytes</code> 75 */ 76 public static String serialDataDumper(final EndianUtil endianUtil, final byte[] bytes) 77 { 78 return serialDataDumper(endianUtil, 0, bytes); 79 } 80 81 /** {@inheritDoc} */ 82 @Override 83 public String toString() 84 { 85 return "SerialDataDumper [super=" + super.toString() + "]"; 86 } 87 88 }