1 package org.djutils.decoderdumper;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5
6 /**
7 * Decode base64 encoded data and dump it in hexadecimal format and (insofar possible) as characters.
8 * <p>
9 * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
11 * <p>
12 * @version $Revision$, $LastChangedDate$, by $Author$, initial version Jan 7, 2019 <br>
13 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
15 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
16 */
17 public class Base64Dumper extends Dumper<Base64Dumper>
18 {
19 /**
20 * Construct a new Base64Dumper.
21 * @param addressOffset int; address of the first byte that will be appended
22 */
23 public Base64Dumper(final int addressOffset)
24 {
25 super(addressOffset);
26 addDecoder(new HexAddressDecoder(16));
27 addDecoder(new FixedString(" "));
28 addDecoder(new CharDecoder(16, 4));
29 addDecoder(new FixedString(": "));
30 addDecoder(new Base64Decoder(16, 3));
31 addDecoder(new FixedString("\n"));
32 }
33
34 /**
35 * Construct a new Base64Dumper with initial address offset 0.
36 */
37 public Base64Dumper()
38 {
39 this(0);
40 }
41
42 /**
43 * Create a HexDumper object; use it to dump an array of bytes and return the dump as a String.
44 * @param addressOffset int; address of the first byte
45 * @param bytes byte[]; the bytes to hex-dump
46 * @return String; the hexadecimal and character dump of the base64 decoded <code>bytes</code>
47 */
48 public static String base64Dumper(final int addressOffset, final byte[] bytes)
49 {
50 ByteArrayOutputStream baos = new ByteArrayOutputStream();
51 try
52 {
53 new Base64Dumper(addressOffset).setOutputStream(baos).append(bytes).flush();
54 }
55 catch (IOException exception)
56 {
57 // Cannot happen because ByteOutputStream.write(byte[]) cannot fail
58 }
59 return baos.toString();
60 }
61
62 /**
63 * Create a HexDumper object with addressOffset 0; use it to dump an array of bytes and return the dump as a String.
64 * @param bytes byte[]; the bytes to hex-dump
65 * @return String; the hexadecimal and character dump of the base64 decoded <code>bytes</code>
66 */
67 public static String base64Dumper(final byte[] bytes)
68 {
69 return base64Dumper(0, bytes);
70 }
71
72 /** {@inheritDoc} */
73 @Override
74 public String toString()
75 {
76 return "Base64Dumper [super=" + super.toString() + "]";
77 }
78
79 }