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-2025 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10 * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
11 * </p>
12 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13 * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
14 */
15 public class Base64Dumper extends Dumper<Base64Dumper>
16 {
17 /**
18 * Construct a new Base64Dumper.
19 * @param addressOffset address of the first byte that will be appended
20 */
21 public Base64Dumper(final int addressOffset)
22 {
23 super(addressOffset);
24 addDecoder(new HexAddressDecoder(16));
25 addDecoder(new FixedString(" "));
26 addDecoder(new CharDecoder(16, 4));
27 addDecoder(new FixedString(": "));
28 addDecoder(new Base64Decoder(16, 3));
29 addDecoder(new FixedString("\n"));
30 }
31
32 /**
33 * Construct a new Base64Dumper with initial address offset 0.
34 */
35 public Base64Dumper()
36 {
37 this(0);
38 }
39
40 /**
41 * Create a HexDumper object; use it to dump an array of bytes and return the dump as a String.
42 * @param addressOffset address of the first byte
43 * @param bytes the bytes to hex-dump
44 * @return the hexadecimal and character dump of the base64 decoded <code>bytes</code>
45 */
46 public static String base64Dumper(final int addressOffset, final byte[] bytes)
47 {
48 ByteArrayOutputStream baos = new ByteArrayOutputStream();
49 try
50 {
51 new Base64Dumper(addressOffset).setOutputStream(baos).append(bytes).flush();
52 return baos.toString("UTF-8");
53 }
54 catch (IOException exception)
55 {
56 // Cannot happen because ByteOutputStream.write(byte[]) cannot fail and UTF-8 exists
57 return "";
58 }
59 }
60
61 /**
62 * Create a HexDumper object with addressOffset 0; use it to dump an array of bytes and return the dump as a String.
63 * @param bytes the bytes to hex-dump
64 * @return the hexadecimal and character dump of the base64 decoded <code>bytes</code>
65 */
66 public static String base64Dumper(final byte[] bytes)
67 {
68 return base64Dumper(0, bytes);
69 }
70
71 @Override
72 public String toString()
73 {
74 return "Base64Dumper [super=" + super.toString() + "]";
75 }
76
77 }