com.stimulsoft.lib.base64
Class StiBase64EncoderUtil

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

public class StiBase64EncoderUtil
extends java.lang.Object

Class encodes the bytes read from the InputStream to a Base64 encoded string and writes to output stream.


Field Summary
protected  int buffer
          24-bit buffer to translate 3 octets into 4 sextets.
protected  int count
          The number of valid bytes in the data buffer.
protected static int FOUR_SEXTETS
          Number of sextets encoded from buffer.
protected static int INPUT_BUFFER_SIZE
          Input buffer size.
protected static byte[] MAP
          Array to convert sextet byte values into base64 characters.
protected static int OCTET_1_MASK
          Mask buffer to or with first octet.
protected static int OCTET_2_MASK
          Mask buffer to or with second octet.
protected static int OCTET_3
          Third octet in buffer.
protected static int OCTET_3_MASK
          Mask buffer to or with third octet.
protected static int OCTET_MASK
          Mask an octet.
protected  int octets
          Number of octets in buffer.
protected  byte[] outputBuffer
          The buffer where data is stored between writes to output stream.
protected static byte PAD
          Byte used to pad output.
protected static int SEXTET_MASK
          Mask a sextet.
protected static int SHIFT_1_OCTET
          Number of bits to shift for one octet.
protected static int SHIFT_1_SEXTET
          Number of bits to shift for one sextet.
protected static int SHIFT_2_OCTET
          Number of bits to shift for two octets.
protected static int SHIFT_2_SEXTET
          Number of bits to shift for two sextets.
protected static int SHIFT_3_SEXTET
          Number of bits to shift for three sextets.
protected  java.io.OutputStream stream
          Stream for encoded characters.
 
Constructor Summary
StiBase64EncoderUtil(java.io.OutputStream stream)
          Construct a Base64Encoder waiting on calls to its translate() method.
 
Method Summary
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(java.io.InputStream input, java.io.OutputStream out)
          Encode bytes from input stream and write encoded bytes to output stream.
static java.lang.String encode(java.lang.String data)
           
protected  void encodeBuffer()
          Encode 4 sextets from buffer.
protected  void encodeBufferWithPadding()
          Encode the remaining sextets from buffer.
 void flush()
          Writes all encoded characters.
protected  StiBase64EncoderUtil translate(byte[] bytes)
          Translate all bytes of given array by appending each to octet buffer.
protected  StiBase64EncoderUtil translate(byte[] bytes, int begin, int end)
          Translate bytes of given array by appending each to octet buffer.
protected  void writeBuffer()
          Write buffer to output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FOUR_SEXTETS

protected static final int FOUR_SEXTETS
Number of sextets encoded from buffer.

See Also:
Constant Field Values

INPUT_BUFFER_SIZE

protected static final int INPUT_BUFFER_SIZE
Input buffer size.

See Also:
Constant Field Values

SEXTET_MASK

protected static final int SEXTET_MASK
Mask a sextet.

See Also:
Constant Field Values

SHIFT_1_SEXTET

protected static final int SHIFT_1_SEXTET
Number of bits to shift for one sextet.

See Also:
Constant Field Values

SHIFT_2_SEXTET

protected static final int SHIFT_2_SEXTET
Number of bits to shift for two sextets.

See Also:
Constant Field Values

SHIFT_3_SEXTET

protected static final int SHIFT_3_SEXTET
Number of bits to shift for three sextets.

See Also:
Constant Field Values

PAD

protected static final byte PAD
Byte used to pad output.

See Also:
Constant Field Values

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:
Constant Field Values

OCTET_1_MASK

protected static final int OCTET_1_MASK
Mask buffer to or with first octet.

See Also:
Constant Field Values

OCTET_2_MASK

protected static final int OCTET_2_MASK
Mask buffer to or with second octet.

See Also:
Constant Field Values

OCTET_3_MASK

protected static final int OCTET_3_MASK
Mask buffer to or with third octet.

See Also:
Constant Field Values

OCTET_MASK

protected static final int OCTET_MASK
Mask an octet.

See Also:
Constant Field Values

SHIFT_1_OCTET

protected static final int SHIFT_1_OCTET
Number of bits to shift for one octet.

See Also:
Constant Field Values

SHIFT_2_OCTET

protected static final int SHIFT_2_OCTET
Number of bits to shift for two octets.

See Also:
Constant Field Values

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 java.io.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 Detail

StiBase64EncoderUtil

public StiBase64EncoderUtil(java.io.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 Detail

encode

public static void encode(java.io.InputStream input,
                          java.io.OutputStream out)
                   throws java.io.IOException
Encode bytes from input stream and write encoded bytes to output stream.

Parameters:
input - input stream
out - output stream
Throws:
java.io.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 java.lang.String encode(java.lang.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 java.io.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:
java.io.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 java.io.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:
java.io.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 java.io.IOException
Encode 4 sextets from buffer.

Throws:
java.io.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 java.io.IOException
Encode the remaining sextets from buffer.

Throws:
java.io.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 java.io.IOException
Write buffer to output stream.

Throws:
java.io.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 java.io.IOException
Writes all encoded characters.

Throws:
java.io.IOException - if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.


Copyright © 2019 Stimulsoft. All Rights Reserved.