Class Base64Encoder

  • All Implemented Interfaces:
    StringCodec

    public class Base64Encoder
    extends java.lang.Object
    implements StringCodec
    Encodes binary data to plain text as Base64.

    Despite there being a gazillion other Base64 implementations out there, this has been written as part of XStream as it forms a core part but is too trivial to warrant an extra dependency. Recent Java Runtimes (since Java 6) provide an own Base64 codec though.

    By default it will not insert line breaks to support Base64 values also as attribute values. However, the standard as described in RFC 1521, section 5.2 requires line breaks, allowing other Base64 tools to manipulate the data. You can configure the Base64Encoder to be RFC compliant.

    • Constructor Summary

      Constructors 
      Constructor Description
      Base64Encoder()
      Constructs a Base64Encoder.
      Base64Encoder​(boolean lineBreaks)
      Constructs a Base64Encoder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) int computeResultingStringSize​(byte[] input)  
      byte[] decode​(java.lang.String input)
      Decode the provided encoded string.
      java.lang.String encode​(byte[] input)
      Encode the provided data.
      private int mapCharToInt​(java.io.Reader input)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SIXTY_FOUR_CHARS

        private static final char[] SIXTY_FOUR_CHARS
      • REVERSE_MAPPING

        private static final int[] REVERSE_MAPPING
      • lineBreaks

        private final boolean lineBreaks
    • Constructor Detail

      • Base64Encoder

        public Base64Encoder()
        Constructs a Base64Encoder.

        The encoder will not insert any line breaks.

        Since:
        1.4.11
      • Base64Encoder

        public Base64Encoder​(boolean lineBreaks)
        Constructs a Base64Encoder.
        Parameters:
        lineBreaks - flag to insert line breaks
        Since:
        1.4.11
    • Method Detail

      • encode

        public java.lang.String encode​(byte[] input)
        Description copied from interface: StringCodec
        Encode the provided data.
        Specified by:
        encode in interface StringCodec
        Parameters:
        input - the data to encode
        Returns:
        the data encoded as string
      • computeResultingStringSize

        int computeResultingStringSize​(byte[] input)
      • decode

        public byte[] decode​(java.lang.String input)
        Description copied from interface: StringCodec
        Decode the provided encoded string.
        Specified by:
        decode in interface StringCodec
        Parameters:
        input - the encoded string
        Returns:
        the decoded data
      • mapCharToInt

        private int mapCharToInt​(java.io.Reader input)
                          throws java.io.IOException
        Throws:
        java.io.IOException