Class StiBase64EncoderUtil

java.lang.Object
com.stimulsoft.lib.base64.StiBase64EncoderUtil

public class StiBase64EncoderUtil extends Object
Class encodes the bytes read from the InputStream to a Base64 encoded string and writes to output stream.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    24-bit buffer to translate 3 octets into 4 sextets.
    protected int
    The number of valid bytes in the data buffer.
    protected static final int
    Number of sextets encoded from buffer.
    protected static final int
    Input buffer size.
    protected static final byte[]
    Array to convert sextet byte values into base64 characters.
    protected static final int
    Mask buffer to or with first octet.
    protected static final int
    Mask buffer to or with second octet.
    protected static final int
    Third octet in buffer.
    protected static final int
    Mask buffer to or with third octet.
    protected static final int
    Mask an octet.
    protected int
    Number of octets in buffer.
    protected byte[]
    The buffer where data is stored between writes to output stream.
    protected static final byte
    Byte used to pad output.
    protected static final int
    Mask a sextet.
    protected static final int
    Number of bits to shift for one octet.
    protected static final int
    Number of bits to shift for one sextet.
    protected static final int
    Number of bits to shift for two octets.
    protected static final int
    Number of bits to shift for two sextets.
    protected static final int
    Number of bits to shift for three sextets.
    protected final OutputStream
    Stream for encoded characters.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a Base64Encoder waiting on calls to its translate() method.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    encode(byte[] data)
    Encode given byte array into an encoded byte array.
    static byte[]
    encode(byte[] data, int begin, int end)
    Encode given byte array into an encoded byte array.
    static void
    Encode bytes from input stream and write encoded bytes to output stream.
    static String
    encode(String data)
     
    protected void
    Encode 4 sextets from buffer.
    protected void
    Encode the remaining sextets from buffer.
    void
    Writes all encoded characters.
    translate(byte[] bytes)
    Translate all bytes of given array by appending each to octet buffer.
    translate(byte[] bytes, int begin, int end)
    Translate bytes of given array by appending each to octet buffer.
    protected void
    Write buffer to output stream.

    Methods inherited from class java.lang.Object

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

    • FOUR_SEXTETS

      protected static final int FOUR_SEXTETS
      Number of sextets encoded from buffer.
      See Also:
    • INPUT_BUFFER_SIZE

      protected static final int INPUT_BUFFER_SIZE
      Input buffer size.
      See Also:
    • SEXTET_MASK

      protected static final int SEXTET_MASK
      Mask a sextet.
      See Also:
    • SHIFT_1_SEXTET

      protected static final int SHIFT_1_SEXTET
      Number of bits to shift for one sextet.
      See Also:
    • SHIFT_2_SEXTET

      protected static final int SHIFT_2_SEXTET
      Number of bits to shift for two sextets.
      See Also:
    • SHIFT_3_SEXTET

      protected static final int SHIFT_3_SEXTET
      Number of bits to shift for three sextets.
      See Also:
    • PAD

      protected static final byte PAD
      Byte used to pad output.
      See Also:
    • MAP

      protected static final byte[] MAP
      Array to convert sextet byte values into base64 characters.
    • OCTET_3

      protected static final int OCTET_3
      Third octet in buffer.
      See Also:
    • OCTET_1_MASK

      protected static final int OCTET_1_MASK
      Mask buffer to or with first octet.
      See Also:
    • OCTET_2_MASK

      protected static final int OCTET_2_MASK
      Mask buffer to or with second octet.
      See Also:
    • OCTET_3_MASK

      protected static final int OCTET_3_MASK
      Mask buffer to or with third octet.
      See Also:
    • OCTET_MASK

      protected static final int OCTET_MASK
      Mask an octet.
      See Also:
    • SHIFT_1_OCTET

      protected static final int SHIFT_1_OCTET
      Number of bits to shift for one octet.
      See Also:
    • SHIFT_2_OCTET

      protected static final int SHIFT_2_OCTET
      Number of bits to shift for two octets.
      See Also:
    • buffer

      protected int buffer
      24-bit buffer to translate 3 octets into 4 sextets.
    • octets

      protected int octets
      Number of octets in buffer.
    • stream

      protected final OutputStream stream
      Stream for encoded characters.
    • outputBuffer

      protected byte[] outputBuffer
      The buffer where data is stored between writes to output stream.
    • count

      protected int count
      The number of valid bytes in the data buffer.
  • Constructor Details

    • StiBase64EncoderUtil

      public StiBase64EncoderUtil(OutputStream stream)
      Construct a Base64Encoder waiting on calls to its translate() method. Base64Encoder must be flushed to write internally buffered data to output stream.
      Parameters:
      stream - stream to write encoded bytes to
  • Method Details

    • encode

      public static void encode(InputStream input, OutputStream out) throws IOException
      Encode bytes from input stream and write encoded bytes to output stream.
      Parameters:
      input - input stream
      out - output stream
      Throws:
      IOException - if an I/O error occurs
    • encode

      public static byte[] encode(byte[] data)
      Encode given byte array into an encoded byte array.
      Parameters:
      data - the byte array to be encoded
      Returns:
      Base64 encoded characters as byte array
    • encode

      public static String encode(String data)
    • encode

      public static byte[] encode(byte[] data, int begin, int end)
      Encode given byte array into an encoded byte array.
      Parameters:
      data - the byte array to be encoded
      begin - index of the first byte to start from
      end - index after the last byte
      Returns:
      Base64 encoded characters as byte array
    • translate

      protected StiBase64EncoderUtil translate(byte[] bytes) throws IOException
      Translate all bytes of given array by appending each to octet buffer. If buffer contains 3 octets its content will be encoded to 4 sextet byte values which are converted to a base64 character each.
      Parameters:
      bytes - the byte array to be encoded
      Returns:
      Base64Encoder
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • translate

      protected StiBase64EncoderUtil translate(byte[] bytes, int begin, int end) throws IOException
      Translate bytes of given array by appending each to octet buffer. If buffer contains 3 octets its content will be encoded to 4 sextet byte values which are converted to a base64 character each.
      Parameters:
      bytes - the byte array to be encoded
      begin - index of the first byte to start from
      end - index after the last byte
      Returns:
      Base64Encoder
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • encodeBuffer

      protected void encodeBuffer() throws IOException
      Encode 4 sextets from buffer.
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • encodeBufferWithPadding

      protected void encodeBufferWithPadding() throws IOException
      Encode the remaining sextets from buffer.
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • writeBuffer

      protected void writeBuffer() throws IOException
      Write buffer to output stream.
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    • flush

      public void flush() throws IOException
      Writes all encoded characters.
      Throws:
      IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.