Class BCodec

  • All Implemented Interfaces:
    Decoder, Encoder, StringDecoder, StringEncoder

    public class BCodec
    extends java.lang.Object
    implements StringEncoder, StringDecoder
    Identical to the Base64 encoding defined by RFC 1521 and allows a character set to be specified.

    RFC 1522 describes techniques to allow the encoding of non-ASCII text in various portions of a RFC 822 [2] message header, in a manner which is unlikely to confuse existing message handling software.

    This class is immutable and thread-safe.

    Decoding is lenient by default: the Base64 payload can contain ignored characters, noncanonical padding or trailing bits, and data after padding. Different encoded words can therefore decode to the same text. To require a canonical Base64 payload, select CodecPolicy.STRICT:

     BCodec codec = new BCodec(StandardCharsets.UTF_8, CodecPolicy.STRICT);
     

    Strict decoding requires the standard Base64 alphabet, padding for partial blocks, and no whitespace within the payload. Invalid payloads cause a DecoderException. This validates the Base64 payload only; it does not establish a unique representation of the complete encoded word or message header, including its charset label. Applications comparing header values for security decisions must use a consistent representation, and signature verification must follow the signing protocol.

    Since:
    1.3
    See Also:
    MIME (Multipurpose Internet Mail Extensions) Part Two: Message Header Extensions for Non-ASCII Text
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.nio.charset.Charset charset
      The default Charset used for string decoding and encoding.
      protected static java.lang.String POSTFIX
      Prefix.
      protected static java.lang.String PREFIX
      Postfix.
      protected static char SEP
      Separator.
    • Constructor Summary

      Constructors 
      Constructor Description
      BCodec()
      Constructs a new instance.
      BCodec​(java.lang.String charsetName)
      Constructs a new instance for the selection of a default Charset.
      BCodec​(java.nio.charset.Charset charset)
      Constructs a new instance for the selection of a default Charset.
      BCodec​(java.nio.charset.Charset charset, CodecPolicy decodingPolicy)
      Constructs a new instance for the selection of a default Charset.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object decode​(java.lang.Object value)
      Decodes a Base64 object into its original form.
      java.lang.String decode​(java.lang.String value)
      Decodes a Base64 string into its original form.
      protected java.lang.String decodeText​(java.lang.String text)
      Applies an RFC 1522 compliant decoding scheme to the given string of text.
      protected byte[] doDecoding​(byte[] bytes)
      Decodes an array of bytes using the defined encoding scheme.
      protected byte[] doEncoding​(byte[] bytes)
      Encodes an array of bytes using the defined encoding scheme.
      java.lang.Object encode​(java.lang.Object value)
      Encodes an object into its Base64 form using the default Charset.
      java.lang.String encode​(java.lang.String strSource)
      Encodes a string into its Base64 form using the default Charset.
      java.lang.String encode​(java.lang.String strSource, java.lang.String sourceCharset)
      Encodes a string into its Base64 form using the specified Charset.
      java.lang.String encode​(java.lang.String strSource, java.nio.charset.Charset sourceCharset)
      Encodes a string into its Base64 form using the specified Charset.
      protected java.lang.String encodeText​(java.lang.String text, java.lang.String charsetName)
      Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
      protected java.lang.String encodeText​(java.lang.String text, java.nio.charset.Charset charset)
      Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.
      java.nio.charset.Charset getCharset()
      Gets the default Charset name used for string decoding and encoding.
      java.lang.String getDefaultCharset()
      Gets the default Charset name used for string decoding and encoding.
      protected java.lang.String getEncoding()
      Gets the codec name (referred to as encoding in the RFC 1522).
      boolean isStrictDecoding()
      Tests whether decoding requires a canonical Base64 payload.
      • Methods inherited from class java.lang.Object

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

      • charset

        protected final java.nio.charset.Charset charset
        The default Charset used for string decoding and encoding.
    • Constructor Detail

      • BCodec

        public BCodec()
        Constructs a new instance.
      • BCodec

        public BCodec​(java.nio.charset.Charset charset)
        Constructs a new instance for the selection of a default Charset.
        Parameters:
        charset - the default string Charset to use.
        Since:
        1.7
        See Also:
        Charset
      • BCodec

        public BCodec​(java.nio.charset.Charset charset,
                      CodecPolicy decodingPolicy)
        Constructs a new instance for the selection of a default Charset.

        Use CodecPolicy.STRICT to require canonical standard Base64 payloads. The other constructors use CodecPolicy.LENIENT. This policy applies to the Base64 payload, not the complete encoded word; see the class documentation.

        Parameters:
        charset - the default string Charset to use.
        decodingPolicy - The decoding policy.
        Since:
        1.15
        See Also:
        Charset
      • BCodec

        public BCodec​(java.lang.String charsetName)
        Constructs a new instance for the selection of a default Charset.
        Parameters:
        charsetName - the default Charset to use.
        Throws:
        java.nio.charset.UnsupportedCharsetException - Thrown if the named Charset is unavailable.
        Since:
        1.7 throws UnsupportedCharsetException if the named Charset is unavailable
        See Also:
        Charset
    • Method Detail

      • decode

        public java.lang.Object decode​(java.lang.Object value)
                                throws DecoderException
        Decodes a Base64 object into its original form. Escaped characters are converted back to their original representation.

        Uses the decoding policy selected at construction. The default is lenient and does not require a canonical Base64 payload. Use BCodec(Charset, CodecPolicy) with CodecPolicy.STRICT for canonical payload validation.

        Specified by:
        decode in interface Decoder
        Parameters:
        value - Base64 object to convert into its original form.
        Returns:
        original object.
        Throws:
        DecoderException - Thrown if the argument is not a String. Thrown if a failure condition is encountered during the decode process.
      • decode

        public java.lang.String decode​(java.lang.String value)
                                throws DecoderException
        Decodes a Base64 string into its original form. Escaped characters are converted back to their original representation.

        Uses the decoding policy selected at construction. The default is lenient and does not require a canonical Base64 payload. Use BCodec(Charset, CodecPolicy) with CodecPolicy.STRICT for canonical payload validation.

        Specified by:
        decode in interface StringDecoder
        Parameters:
        value - Base64 string to convert into its original form.
        Returns:
        original string.
        Throws:
        DecoderException - Thrown if a failure condition is encountered during the decoding process.
      • doDecoding

        protected byte[] doDecoding​(byte[] bytes)
                             throws DecoderException
        Decodes an array of bytes using the defined encoding scheme.
        Parameters:
        bytes - Data to be decoded.
        Returns:
        A byte array that contains decoded data.
        Throws:
        java.lang.IllegalArgumentException - Thrown when a problem is detected processing data.
        DecoderException - Thrown if a Decoder encounters a failure condition during the decode process.
      • doEncoding

        protected byte[] doEncoding​(byte[] bytes)
        Encodes an array of bytes using the defined encoding scheme.
        Parameters:
        bytes - Data to be encoded.
        Returns:
        A byte array containing the encoded data.
      • encode

        public java.lang.Object encode​(java.lang.Object value)
                                throws EncoderException
        Encodes an object into its Base64 form using the default Charset. Unsafe characters are escaped.
        Specified by:
        encode in interface Encoder
        Parameters:
        value - object to convert to Base64 form.
        Returns:
        Base64 object.
        Throws:
        EncoderException - Thrown if a failure condition is encountered during the encoding process.
      • encode

        public java.lang.String encode​(java.lang.String strSource)
                                throws EncoderException
        Encodes a string into its Base64 form using the default Charset. Unsafe characters are escaped.
        Specified by:
        encode in interface StringEncoder
        Parameters:
        strSource - string to convert to Base64 form.
        Returns:
        Base64 string.
        Throws:
        EncoderException - Thrown if a failure condition is encountered during the encoding process.
      • encode

        public java.lang.String encode​(java.lang.String strSource,
                                       java.nio.charset.Charset sourceCharset)
                                throws EncoderException
        Encodes a string into its Base64 form using the specified Charset. Unsafe characters are escaped.
        Parameters:
        strSource - string to convert to Base64 form.
        sourceCharset - the Charset for value.
        Returns:
        Base64 string.
        Throws:
        EncoderException - Thrown if a failure condition is encountered during the encoding process.
        Since:
        1.7
      • encode

        public java.lang.String encode​(java.lang.String strSource,
                                       java.lang.String sourceCharset)
                                throws EncoderException
        Encodes a string into its Base64 form using the specified Charset. Unsafe characters are escaped.
        Parameters:
        strSource - string to convert to Base64 form.
        sourceCharset - the Charset for value.
        Returns:
        Base64 string.
        Throws:
        EncoderException - Thrown if a failure condition is encountered during the encoding process.
      • getEncoding

        protected java.lang.String getEncoding()
        Gets the codec name (referred to as encoding in the RFC 1522).
        Returns:
        name of the codec.
      • isStrictDecoding

        public boolean isStrictDecoding()
        Tests whether decoding requires a canonical Base64 payload.

        Strict decoding raises DecoderException for a noncanonical Base64 payload, including invalid alphabet characters, padding, or trailing bits. The default is lenient. This policy does not establish a canonical representation of the complete encoded word.

        Returns:
        true if using strict decoding.
        Since:
        1.15
      • decodeText

        protected java.lang.String decodeText​(java.lang.String text)
                                       throws DecoderException,
                                              java.io.UnsupportedEncodingException
        Applies an RFC 1522 compliant decoding scheme to the given string of text.

        This method processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes doDecoding(byte[]) method of a concrete class to perform the specific decoding.

        Parameters:
        text - A string to decode.
        Returns:
        A new decoded String or null if the input is null.
        Throws:
        DecoderException - Thrown if there is an error condition during the decoding process.
        java.io.UnsupportedEncodingException - Thrown if charset specified in the "encoded-word" header is not supported.
      • encodeText

        protected java.lang.String encodeText​(java.lang.String text,
                                              java.nio.charset.Charset charset)
                                       throws EncoderException
        Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.

        This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes doEncoding(byte[]) method of a concrete class to perform the specific encoding.

        Parameters:
        text - A string to encode.
        charset - A charset to be used.
        Returns:
        RFC 1522 compliant "encoded-word".
        Throws:
        EncoderException - Thrown if there is an error condition during the encoding process.
        See Also:
        Charset
      • encodeText

        protected java.lang.String encodeText​(java.lang.String text,
                                              java.lang.String charsetName)
                                       throws EncoderException
        Applies an RFC 1522 compliant encoding scheme to the given string of text with the given charset.

        This method constructs the "encoded-word" header common to all the RFC 1522 codecs and then invokes doEncoding(byte[]) method of a concrete class to perform the specific encoding.

        Parameters:
        text - A string to encode.
        charsetName - The charset to use.
        Returns:
        RFC 1522 compliant "encoded-word".
        Throws:
        EncoderException - Thrown if there is an error condition during the encoding process.
        java.nio.charset.UnsupportedCharsetException - Thrown if charset is not available.
        See Also:
        Charset
      • getCharset

        public java.nio.charset.Charset getCharset()
        Gets the default Charset name used for string decoding and encoding.
        Returns:
        The default Charset name.
        Since:
        1.7
      • getDefaultCharset

        public java.lang.String getDefaultCharset()
        Gets the default Charset name used for string decoding and encoding.
        Returns:
        The default Charset name.