Package gw.util

Class Base64Util.Base64

java.lang.Object
gw.util.Base64Util.Base64
Enclosing class:
Base64Util

static class Base64Util.Base64 extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final byte[]
    This array is a lookup table that translates unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit positive integer equivalents.
    private byte[]
    Buffer for streaming.
    (package private) static final byte[]
    Chunk separator per RFC 2045 section 2.1.
    (package private) static final int
    Chunk size per RFC 2045 section 6.8.
    private int
    Variable tracks how many characters have been written to the current line.
    private final int
    Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
    private final int
    Convenience variable to help us determine when our buffer is going to run out of room and needs resizing.
    private boolean
    Boolean flag to indicate the EOF has been reached.
    private static final byte[]
    This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet" equivalents as specified in Table 1 of RFC 2045.
    private final int
    Line length for encoding.
    private final byte[]
    Line separator for encoding.
    private static final int
    Mask used to extract 6 bits, used when encoding
    private static final int
    Mask used to extract 8 bits, used in decoding base64 bytes
    private int
    Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding.
    private static final byte
    Byte used to pad output.
    private int
    Position where next character should be written in the buffer.
    private int
    Position where next character should be read from the buffer.
    private int
    Place holder for the 3 bytes we're dealing with for our base64 logic.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor: lineLength is 76, and the lineSeparator is CRLF when encoding, and all forms can be decoded.
    Base64(int lineLength)
    Consumer can use this constructor to choose a different lineLength when encoding (lineSeparator is still CRLF).
    Base64(int lineLength, byte[] lineSeparator)
    Consumer can use this constructor to choose a different lineLength and lineSeparator when encoding.
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) int
    Returns the amount of buffered data available for reading.
    private static boolean
    containsBase64Byte(byte[] arrayOctet)
     
    byte[]
    decode(byte[] pArray)
    Decodes a byte[] containing containing characters in the Base64 alphabet.
    (package private) void
    decode(byte[] in, int inPos, int inAvail)
    Decodes all of the provided data, starting at inPos, for inAvail bytes.
    decode(Object pObject)
    Decodes an Object using the base64 algorithm.
    static byte[]
    decodeBase64(byte[] base64Data)
    Decodes Base64 data into octets
    static BigInteger
    decodeInteger(byte[] pArray)
    Decode a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
    (package private) static byte[]
    discardNonBase64(byte[] data)
    Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any characters outside of the base64 alphabet are to be ignored in base64 encoded data."
    (package private) static byte[]
    discardWhitespace(byte[] data)
    Deprecated.
    This method is no longer needed
    byte[]
    encode(byte[] pArray)
    Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
    (package private) void
    encode(byte[] in, int inPos, int inAvail)
    Encodes all of the provided data, starting at inPos, for inAvail bytes.
    encode(Object pObject)
    Encodes an Object using the base64 algorithm.
    static byte[]
    encodeBase64(byte[] binaryData)
    Encodes binary data using the base64 algorithm but does not chunk the output.
    static byte[]
    encodeBase64(byte[] binaryData, boolean isChunked)
    Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
    static byte[]
    encodeBase64Chunked(byte[] binaryData)
    Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
    static byte[]
    Encode to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
    (package private) boolean
    Returns true if this Base64 object has buffered data for reading.
    static boolean
    isArrayByteBase64(byte[] arrayOctet)
    Tests a given byte array to see if it contains only valid characters within the Base64 alphabet.
    static boolean
    isBase64(byte octet)
    Returns whether or not the octet is in the base 64 alphabet.
    private static boolean
    isWhiteSpace(byte byteToCheck)
    Check if a byte value is whitespace or not.
    (package private) int
    readResults(byte[] b, int bPos, int bAvail)
    Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes.
    private void
    Doubles our buffer.
    (package private) void
    setInitialBuffer(byte[] out, int outPos, int outAvail)
    Small optimization where we try to buffer directly to the consumer's output array for one round (if consumer calls this method first!) instead of starting our own buffer.
    (package private) static byte[]
    Returns a byte-array representation of a BigInteger without sign bit.

    Methods inherited from class java.lang.Object

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

    • CHUNK_SIZE

      static final int CHUNK_SIZE
      Chunk size per RFC 2045 section 6.8.

      The 76 character limit does not count the trailing CRLF, but counts all other characters, including any equal signs.

      See Also:
    • CHUNK_SEPARATOR

      static final byte[] CHUNK_SEPARATOR
      Chunk separator per RFC 2045 section 2.1.
      See Also:
    • intToBase64

      private static final byte[] intToBase64
      This array is a lookup table that translates 6-bit positive integer index values into their "Base64 Alphabet" equivalents as specified in Table 1 of RFC 2045. Thanks to "commons" project in ws.apache.org for this code. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
    • PAD

      private static final byte PAD
      Byte used to pad output.
      See Also:
    • base64ToInt

      private static final byte[] base64ToInt
      This array is a lookup table that translates unicode characters drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) into their 6-bit positive integer equivalents. Characters that are not in the Base64 alphabet but fall within the bounds of the array are translated to -1. Thanks to "commons" project in ws.apache.org for this code. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/
    • MASK_6BITS

      private static final int MASK_6BITS
      Mask used to extract 6 bits, used when encoding
      See Also:
    • MASK_8BITS

      private static final int MASK_8BITS
      Mask used to extract 8 bits, used in decoding base64 bytes
      See Also:
    • lineLength

      private final int lineLength
      Line length for encoding. Not used when decoding. A value of zero or less implies no chunking of the base64 encoded data.
    • lineSeparator

      private final byte[] lineSeparator
      Line separator for encoding. Not used when decoding. Only used if lineLength > 0.
    • decodeSize

      private final int decodeSize
      Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. decodeSize = 3 + lineSeparator.length;
    • encodeSize

      private final int encodeSize
      Convenience variable to help us determine when our buffer is going to run out of room and needs resizing. encodeSize = 4 + lineSeparator.length;
    • buf

      private byte[] buf
      Buffer for streaming.
    • pos

      private int pos
      Position where next character should be written in the buffer.
    • readPos

      private int readPos
      Position where next character should be read from the buffer.
    • currentLinePos

      private int currentLinePos
      Variable tracks how many characters have been written to the current line. Only used when encoding. We use it to make sure each encoded line never goes beyond lineLength (if lineLength > 0).
    • modulus

      private int modulus
      Writes to the buffer only occur after every 3 reads when encoding, an every 4 reads when decoding. This variable helps track that.
    • eof

      private boolean eof
      Boolean flag to indicate the EOF has been reached. Once EOF has been reached, this Base64 object becomes useless, and must be thrown away.
    • x

      private int x
      Place holder for the 3 bytes we're dealing with for our base64 logic. Bitwise operations store and extract the base64 encoding or decoding from this variable.
  • Constructor Details

    • Base64

      public Base64()
      Default constructor: lineLength is 76, and the lineSeparator is CRLF when encoding, and all forms can be decoded.
    • Base64

      public Base64(int lineLength)

      Consumer can use this constructor to choose a different lineLength when encoding (lineSeparator is still CRLF). All forms of data can be decoded.

      Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

      Parameters:
      lineLength - each line of encoded data will be at most this long (rounded up to nearest multiple of 4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.
    • Base64

      public Base64(int lineLength, byte[] lineSeparator)

      Consumer can use this constructor to choose a different lineLength and lineSeparator when encoding. All forms of data can be decoded.

      Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

      Parameters:
      lineLength - Each line of encoded data will be at most this long (rounded up to nearest multiple of 4). Ignored when decoding. If <= 0, then output will not be divided into lines (chunks).
      lineSeparator - Each line of encoded data will end with this sequence of bytes. If lineLength <= 0, then the lineSeparator is not used.
      Throws:
      IllegalArgumentException - The provided lineSeparator included some base64 characters. That's not going to work!
  • Method Details

    • hasData

      boolean hasData()
      Returns true if this Base64 object has buffered data for reading.
      Returns:
      true if there is Base64 object still available for reading.
    • avail

      int avail()
      Returns the amount of buffered data available for reading.
      Returns:
      The amount of buffered data available for reading.
    • resizeBuf

      private void resizeBuf()
      Doubles our buffer.
    • readResults

      int readResults(byte[] b, int bPos, int bAvail)
      Extracts buffered data into the provided byte[] array, starting at position bPos, up to a maximum of bAvail bytes. Returns how many bytes were actually extracted.
      Parameters:
      b - byte[] array to extract the buffered data into.
      bPos - position in byte[] array to start extraction at.
      bAvail - amount of bytes we're allowed to extract. We may extract fewer (if fewer are available).
      Returns:
      The number of bytes successfully extracted into the provided byte[] array.
    • setInitialBuffer

      void setInitialBuffer(byte[] out, int outPos, int outAvail)
      Small optimization where we try to buffer directly to the consumer's output array for one round (if consumer calls this method first!) instead of starting our own buffer.
      Parameters:
      out - byte[] array to buffer directly to.
      outPos - Position to start buffering into.
      outAvail - Amount of bytes available for direct buffering.
    • encode

      void encode(byte[] in, int inPos, int inAvail)

      Encodes all of the provided data, starting at inPos, for inAvail bytes. Must be called at least twice: once with the data to encode, and once with inAvail set to "-1" to alert encoder that EOF has been reached, so flush last remaining bytes (if not multiple of 3).

      Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/

      Parameters:
      in - byte[] array of binary data to base64 encode.
      inPos - Position to start reading data from.
      inAvail - Amount of bytes available from input for encoding.
    • decode

      void decode(byte[] in, int inPos, int inAvail)

      Decodes all of the provided data, starting at inPos, for inAvail bytes. Should be called at least twice: once with the data to decode, and once with inAvail set to "-1" to alert decoder that EOF has been reached. The "-1" call is not necessary when decoding, but it doesn't hurt, either.

      Ignores all non-base64 characters. This is how chunked (e.g. 76 character) data is handled, since CR and LF are silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in, garbage-out philosophy: it will not check the provided data for validity.

      Thanks to "commons" project in ws.apache.org for the bitwise operations, and general approach. http://svn.apache.org/repos/asf/webservices/commons/trunk/modules/util/

      Parameters:
      in - byte[] array of ascii data to base64 decode.
      inPos - Position to start reading data from.
      inAvail - Amount of bytes available from input for encoding.
    • isBase64

      public static boolean isBase64(byte octet)
      Returns whether or not the octet is in the base 64 alphabet.
      Parameters:
      octet - The value to test
      Returns:
      true if the value is defined in the the base 64 alphabet, false otherwise.
    • isArrayByteBase64

      public static boolean isArrayByteBase64(byte[] arrayOctet)
      Tests a given byte array to see if it contains only valid characters within the Base64 alphabet. Currently the method treats whitespace as valid.
      Parameters:
      arrayOctet - byte array to test
      Returns:
      true if all bytes are valid characters in the Base64 alphabet or if the byte array is empty; false, otherwise
    • containsBase64Byte

      private static boolean containsBase64Byte(byte[] arrayOctet)
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData)
      Encodes binary data using the base64 algorithm but does not chunk the output.
      Parameters:
      binaryData - binary data to encode
      Returns:
      Base64 characters
    • encodeBase64Chunked

      public static byte[] encodeBase64Chunked(byte[] binaryData)
      Encodes binary data using the base64 algorithm and chunks the encoded output into 76 character blocks
      Parameters:
      binaryData - binary data to encode
      Returns:
      Base64 characters chunked in 76 character blocks
    • decode

      public Object decode(Object pObject)
      Decodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the Decoder interface, and will throw a DecoderException if the supplied object is not of type byte[].
      Parameters:
      pObject - Object to decode
      Returns:
      An object (of type byte[]) containing the binary data which corresponds to the byte[] supplied.
    • decode

      public byte[] decode(byte[] pArray)
      Decodes a byte[] containing containing characters in the Base64 alphabet.
      Parameters:
      pArray - A byte array containing Base64 character data
      Returns:
      a byte array containing binary data
    • encodeBase64

      public static byte[] encodeBase64(byte[] binaryData, boolean isChunked)
      Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
      Parameters:
      binaryData - Array containing binary data to encode.
      isChunked - if true this encoder will chunk the base64 output into 76 character blocks
      Returns:
      Base64-encoded data.
      Throws:
      IllegalArgumentException - Thrown when the input array needs an output array bigger than Integer.MAX_VALUE
    • decodeBase64

      public static byte[] decodeBase64(byte[] base64Data)
      Decodes Base64 data into octets
      Parameters:
      base64Data - Byte array containing Base64 data
      Returns:
      Array containing decoded data.
    • discardWhitespace

      static byte[] discardWhitespace(byte[] data)
      Deprecated.
      This method is no longer needed
      Discards any whitespace from a base-64 encoded block.
      Parameters:
      data - The base-64 encoded data to discard the whitespace from.
      Returns:
      The data, less whitespace (see RFC 2045).
    • isWhiteSpace

      private static boolean isWhiteSpace(byte byteToCheck)
      Check if a byte value is whitespace or not.
      Parameters:
      byteToCheck - the byte to check
      Returns:
      true if byte is whitespace, false otherwise
    • discardNonBase64

      static byte[] discardNonBase64(byte[] data)
      Discards any characters outside of the base64 alphabet, per the requirements on page 25 of RFC 2045 - "Any characters outside of the base64 alphabet are to be ignored in base64 encoded data."
      Parameters:
      data - The base-64 encoded data to groom
      Returns:
      The data, less non-base64 characters (see RFC 2045).
    • encode

      public Object encode(Object pObject)
      Encodes an Object using the base64 algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an EncoderException if the supplied object is not of type byte[].
      Parameters:
      pObject - Object to encode
      Returns:
      An object (of type byte[]) containing the base64 encoded data which corresponds to the byte[] supplied.
    • encode

      public byte[] encode(byte[] pArray)
      Encodes a byte[] containing binary data, into a byte[] containing characters in the Base64 alphabet.
      Parameters:
      pArray - a byte array containing binary data
      Returns:
      A byte array containing only Base64 character data
    • decodeInteger

      public static BigInteger decodeInteger(byte[] pArray)
      Decode a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
      Parameters:
      pArray - a byte array containing base64 character data
      Returns:
      A BigInteger
    • encodeInteger

      public static byte[] encodeInteger(BigInteger bigInt)
      Encode to a byte64-encoded integer according to crypto standards such as W3C's XML-Signature
      Parameters:
      bigInt - a BigInteger
      Returns:
      A byte array containing base64 character data
      Throws:
      NullPointerException - if null is passed in
    • toIntegerBytes

      static byte[] toIntegerBytes(BigInteger bigInt)
      Returns a byte-array representation of a BigInteger without sign bit.
      Parameters:
      bigInt - BigInteger to be converted
      Returns:
      a byte array representation of the BigInteger parameter