1 /*
2  * Copyright (C) 2017, 2018 XStream Committers.
3  * All rights reserved.
4  *
5  * The software in this package is published under the terms of the BSD
6  * style license a copy of which has been included with this distribution in
7  * the LICENSE.txt file.
8  *
9  * Created on 12. August 2017 by Joerg Schaible
10  */

11 package com.thoughtworks.xstream.core.util;
12
13 import java.util.Base64;
14
15 import com.thoughtworks.xstream.core.StringCodec;
16
17
18 /**
19  * Base64 codec implementation based on java.util.Base64.
20  *
21  * @author Jörg Schaible
22  * @since 1.4.11
23  */

24 public class Base64JavaUtilCodec implements StringCodec {
25     final private Base64.Decoder decoder;
26     final private Base64.Encoder encoder;
27
28     /**
29      * Constructs a Base64JavaUtilCodec.
30      * <p>
31      * The implementation will use a basic encoder and a MIME decoder by default.
32      * </p>
33      *
34      * @since 1.4.11
35      */

36     public Base64JavaUtilCodec() {
37         this(Base64.getEncoder(), Base64.getMimeDecoder());
38     }
39
40     /**
41      * Constructs a Base64JavaUtilCodec with provided encoder and decoder.
42      *
43      * @param encoder the encoder instance
44      * @param decoder the decoder instance
45      * @since 1.4.11
46      */

47     public Base64JavaUtilCodec(final Base64.Encoder encoder, final Base64.Decoder decoder) {
48         this.encoder = encoder;
49         this.decoder = decoder;
50     }
51
52     @Override
53     public byte[] decode(final String base64) {
54         return decoder.decode(base64);
55     }
56
57     @Override
58     public String encode(final byte[] data) {
59         return encoder.encodeToString(data);
60     }
61 }
62