Class Base64Encoder

java.lang.Object
com.thoughtworks.xstream.core.util.Base64Encoder
All Implemented Interfaces:
StringCodec

public class Base64Encoder extends 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.

  • Field Details

    • SIXTY_FOUR_CHARS

      private static final char[] SIXTY_FOUR_CHARS
    • REVERSE_MAPPING

      private static final int[] REVERSE_MAPPING
    • lineBreaks

      private final boolean lineBreaks
  • Constructor Details

    • 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 Details

    • encode

      public 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(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(Reader input) throws IOException
      Throws:
      IOException