org.djutils.serialization.util.SerialDataDumper Maven / Gradle / Ivy
Show all versions of djutils-serialization Show documentation
package org.djutils.serialization.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.djutils.decoderdumper.Dumper;
import org.djutils.decoderdumper.FixedString;
import org.djutils.decoderdumper.HexAddressDecoder;
import org.djutils.decoderdumper.HexDecoder;
import org.djutils.serialization.EndianUtil;
import org.djutils.serialization.SerialDataDecoder;
/**
* Dumper for serialized data.
*
* Copyright (c) 2019-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See DJUTILS License.
*
* version Jun 27, 2019
* @author Alexander Verbraeck
* @author Peter Knoppers
*/
public class SerialDataDumper extends Dumper
{
/**
* Construct a new SerialDataDumper.
* @param endianUtil EndianUtil; used to decode multi-byte values
* @param addressOffset int; address of the first byte that will be processed
*/
public SerialDataDumper(final EndianUtil endianUtil, final int addressOffset)
{
super(addressOffset);
addDecoder(new HexAddressDecoder(16));
addDecoder(new FixedString(": "));
addDecoder(new HexDecoder(16, 8));
addDecoder(new FixedString(" "));
addDecoder(new SerialDataDecoder(endianUtil));
addDecoder(new FixedString("\n"));
}
/**
* Construct a new SerialDataDumper.
* @param endianUtil EndianUtil; used to decode multi-byte values
*/
public SerialDataDumper(final EndianUtil endianUtil)
{
this(endianUtil, 0);
}
/**
* Create a SerialDataDumper object; use it to dump an array of bytes and return the dump as a String.
* @param endianUtil EndianUtil; used to decode multi-byte values
* @param addressOffset int; address of the first byte
* @param bytes byte[]; the bytes to hex-dump
* @return String; the hexadecimal and character dump of the bytes
*/
public static String serialDataDumper(final EndianUtil endianUtil, final int addressOffset, final byte[] bytes)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
new SerialDataDumper(endianUtil, addressOffset).setOutputStream(baos).append(bytes).flush();
}
catch (IOException exception)
{
// Cannot happen because ByteOutputStream.write(byte[]) cannot fail
}
return baos.toString();
}
/**
* Create a SerialDataDumper object with addressOffset 0; use it to dump an array of bytes and return the dump as a String.
* @param endianUtil EndianUtil; used to decode multi-byte values
* @param bytes byte[]; the bytes to hex-dump
* @return String; the hexadecimal and character dump of the bytes
*/
public static String serialDataDumper(final EndianUtil endianUtil, final byte[] bytes)
{
return serialDataDumper(endianUtil, 0, bytes);
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "SerialDataDumper [super=" + super.toString() + "]";
}
}