Class FlexBase64.Decoder

  • Enclosing class:
    FlexBase64

    public static final class FlexBase64.Decoder
    extends Object
    Controls the decoding process.
    • Method Detail

      • decode

        public void decode​(ByteBuffer source,
                           ByteBuffer target)
                    throws IOException
        Decodes one Base64 byte buffer into another. This method will return and save state if the target does not have the required capacity. Subsequent calls with a new target will resume reading where it last left off (the source buffer's position). Similarly not all of the source data need be available, this method can be repetitively called as data is made available.

        The decoder will skip white space, but will error if it detects corruption.

        Parameters:
        source - the byte buffer to read encoded data from
        target - the byte buffer to write decoded data to
        Throws:
        IOException - if the encoded data is corrupted
      • getLastInputPosition

        public int getLastInputPosition()
        Gets the last position where decoding left off in the last byte array that was used for reading. If the target for decoded content does not have the necessary capacity, this method should be used to determine where to start from on subsequent decode calls.
        Returns:
        the last known read position
      • decode

        public int decode​(String source,
                          int sourcePos,
                          int sourceLimit,
                          byte[] target,
                          int targetPos,
                          int targetLimit)
                   throws IOException
        Decodes one Base64 byte array into another byte array. If the source limit is hit, this method will return and save the current state, such that future calls can resume the decoding process. Likewise, if the target does not have the capacity, this method will also return and save state for subsequent calls to this method.

        When multiple calls are made, getLastInputPosition() should be used to determine what value should be set for sourcePos. Likewise, the returned target position should be used as the targetPos in a subsequent call.

        The decoder will skip white space, but will error if it detects corruption.

        Parameters:
        source - a Base64 encoded string to decode data from
        sourcePos - the position in the source array to start decoding from
        sourceLimit - the position in the source array to halt decoding when hit (exclusive)
        target - the byte buffer to write decoded data to
        targetPos - the position in the target byte array to begin writing at
        targetLimit - the position in the target byte array to halt writing (exclusive)
        Returns:
        the position in the target array immediately following the last byte written
        Throws:
        IOException - if the encoded data is corrupted
      • decode

        public int decode​(String source,
                          byte[] target)
                   throws IOException
        Decodes a Base64 encoded string into the passed byte array. This method will return and save state if the target does not have the required capacity. Subsequent calls with a new target will resume reading where it last left off (the source buffer's position). Similarly not all of the source data need be available, this method can be repetitively called as data is made available.

        Since this method variant assumes a position of 0 and a limit of the item length, repeated calls will need fresh source and target values. decode(String, int, int, byte[], int, int) would be a better fit if you need reuse

        The decoder will skip white space, but will error if it detects corruption.

        Parameters:
        source - a base64 encoded string to decode from
        target - a byte array to write to
        Returns:
        output position following the last written byte
        Throws:
        IOException - if the base64 content is malformed
      • decode

        public int decode​(byte[] source,
                          int sourcePos,
                          int sourceLimit,
                          byte[] target,
                          int targetPos,
                          int targetLimit)
                   throws IOException
        Decodes one Base64 byte array into another byte array. If the source limit is hit, this method will return and save the current state, such that future calls can resume the decoding process. Likewise, if the target does not have the capacity, this method will also return and save state for subsequent calls to this method.

        When multiple calls are made, getLastInputPosition() should be used to determine what value should be set for sourcePos. Likewise, the returned target position should be used as the targetPos in a subsequent call.

        The decoder will skip white space, but will error if it detects corruption.

        
          Decoder decoder = FlexBase64.createDecoder();
          byte[] outBuffer = new byte[10];
          byte[] bytes = "aGVsbG8=".getBytes("US-ASCII");
          // Decode only 2 bytes
          int outPosition = decoder.decode(bytes, 0, 8, outBuffer, 5, 7);
          // Resume where we left off and get the rest
          outPosition = decoder.decode(bytes, decoder.getLastInputPosition(), 8, outBuffer, outPosition, 10);
          // Prints "10 : Hello"
          System.out.println(outPosition + " : " + new String(outBuffer, 0, 5, outPosition - 5));
         
        Parameters:
        source - the byte array to read encoded data from
        sourcePos - the position in the source array to start decoding from
        sourceLimit - the position in the source array to halt decoding when hit (exclusive)
        target - the byte buffer to write decoded data to
        targetPos - the position in the target byte array to begin writing at
        targetLimit - the position in the target byte array to halt writing (exclusive)
        Returns:
        the position in the target array immediately following the last byte written
        Throws:
        IOException - if the encoded data is corrupted