Class Base64InputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public final class Base64InputStream extends FilterInputStream

A Base64InputStream decodes Base64-encoded bytes read from the given InputStream.

The Base 64 decoding

  • The length of Base64-encoded data MUST be a multiple of 4 and MAY end by 1 or 2 padding characters (=).
  • Encoded data MAY be splitted in lines of 76 characters.
  • Valid Base64 characters are: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/.
  • Invalid Base64 characters are ignored but a warning message is displayed in the standard error output.

Buffer

This stream is buffered. That means that several bytes have already been read and decoded from the inner input stream. By default the buffer size is: 8192.

Warning ! To fill the buffer of a Base64InputStream with N bytes, (N/3)*4 bytes must be fetched from the inner input stream. Indeed, with the Base64 encoding, the number of encoded bytes is always greater than the number of the corresponding decoded bytes: 3 bytes will be encoded by 4 characters encoded on 6 bits (which allows an alphabet of 2^6=64 characters, hence base64). Consequently: for a buffer of 8192 bytes, 10924 bytes are needed.

However the number of bytes stored in the buffer may be less than the size given at the initialization. That is to say: the buffer can contain AT MOST 8192 decoded bytes. Indeed some bytes coming from the inner input stream may correspond to invalid Base64 characters. In this case, they are ignored and so they are not stored in the buffer.

Warning & Errors

A Base64InputStream writes a warning in the standard error output when:

  • invalid Base64 characters are encountered before the end of the encoded data

A Base64InputStream throws an exception when:

  • the inner stream ends whereas a group of 4 valid characters/bytes is not complete
  • valid Base64 characters are encountered after padding characters
  • more than 2 padding characters are encountered

Mark & Reset

The mark(int) and reset() methods are not supported in a Base64InputStream.

Since:
09/2011
Author:
Gregory Mantelet
See Also: