io.undertow.util

## Class FlexBase64.Encoder

- java.lang.Object
- - io.undertow.util.FlexBase64.Encoder

Enclosing class:
  FlexBase64

    public static final class FlexBase64.Encoder
    extends Object

Controls the encoding process.

### Method Summary

All Methods Instance Methods Concrete Methods
`int` `complete(byte[] target, int pos)` Completes an encoding session by writing out the necessary padding.
`void` `complete(ByteBuffer target)` Completes an encoding session by writing out the necessary padding.
`int` `encode(byte[] source, int pos, int limit, byte[] target, int opos, int olimit)` Encodes bytes read from source and writes them in base64 format to target.
`void` `encode(ByteBuffer source, ByteBuffer target)` Encodes bytes read from source and writes them in base64 format to target.
`int` `getLastInputPosition()` Gets the last position where encoding left off in the last byte array that was used.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### encode

    public void encode(ByteBuffer source,
                       ByteBuffer target)

Encodes bytes read from source and writes them in base64 format to target. If the source limit is hit, this method will return and save the current state, such that future calls can resume the encoding process. In addition, if the target does not have the capacity to fit an entire quad of bytes, this method will also return and save state for subsequent calls to this method. Once all bytes have been encoded to the target, `complete(java.nio.ByteBuffer)` should be called to add the necessary padding characters.

Parameters:
  `source` - the byte buffer to read from
  `target` - the byte buffer to write to

#### encode

    public int encode(byte[] source,
                      int pos,
                      int limit,
                      byte[] target,
                      int opos,
                      int olimit)

Encodes bytes read from source and writes them in base64 format to target. If the source limit is hit, this method will return and save the current state, such that future calls can resume the encoding process. In addition, if the target does not have the capacity to fit an entire quad of bytes, this method will also return and save state for subsequent calls to this method. Once all bytes have been encoded to the target, `complete(byte[], int)` should be called to add the necessary padding characters. In order to determine the last read position, the `getLastInputPosition()` can be used.

Note that the limit values are not lengths, they are positions similar to `Buffer.limit()`. To calculate a length simply subtract position from limit.

      Encoder encoder = FlexBase64.createEncoder(false);
      byte[] outBuffer = new byte[10];
      // Encode "ell"
      int outPosition = encoder.encode("hello".getBytes("US-ASCII"), 1, 4, outBuffer, 5, 10);
      // Prints "9 : ZWxs"
      System.out.println(outPosition + " : " + new String(outBuffer, 0, 5, outPosition - 5));

Parameters:
  `source` - the byte array to read from
  `pos` - ths position in the byte array to start reading from
  `limit` - the position in the byte array that is after the end of the source data
  `target` - the byte array to write base64 bytes to
  `opos` - the position to start writing to the target array at
  `olimit` - the position in the target byte array that makes the end of the writable area (exclusive)
Returns:
  the position in the target array immediately following the last byte written

#### getLastInputPosition

    public int getLastInputPosition()

Gets the last position where encoding left off in the last byte array that was used. If the target for encoded content does not have the necessary capacity, this method should be used to determine where to start from on subsequent reads.

Returns:
  the last known read position

#### complete

    public int complete(byte[] target,
                        int pos)

Completes an encoding session by writing out the necessary padding. This is essential to complying with the Base64 format. This method will write at most 4 or 2 bytes starting at pos,depending on whether or not wrapping is enabled.

      Encoder encoder = FlexBase64.createEncoder(false);
      byte[] outBuffer = new byte[13];

      // Encodes "ello"
      int outPosition = encoder.encode("hello".getBytes("US-ASCII"), 0, 4, outBuffer, 5, 13);
      outPosition = encoder.complete(outBuffer, outPosition);

      // Prints "13 : aGVsbA=="
      System.out.println(outPosition + " : " + new String(outBuffer, 0, 5, outPosition - 5));

Parameters:
  `target` - the byte array to write to
  `pos` - the position to start writing at
Returns:
  the position after the last byte written

#### complete

    public void complete(ByteBuffer target)

Completes an encoding session by writing out the necessary padding. This is essential to complying with the Base64 format. This method will write at most 4 or 2 bytes, depending on whether or not wrapping is enabled.

Parameters:
  `target` - the byte buffer to write to