Class Base64InputStream
- java.lang.Object
-
- java.io.InputStream
-
- java.io.FilterInputStream
-
- cds.savot.binary.Base64InputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public final class Base64InputStream extends java.io.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:
Base64
-
-
Constructor Summary
Constructors Constructor Description Base64InputStream(java.io.InputStream encodedStream)
Base64InputStream(java.io.InputStream encodedStream, int bufferSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
void
close()
void
mark(int readlimit)
boolean
markSupported()
int
read()
int
read(byte[] b)
int
read(byte[] b, int off, int len)
void
reset()
long
skip(long n)
-
-
-
Method Detail
-
available
public int available() throws java.io.IOException
- Overrides:
available
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException
- Overrides:
read
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b) throws java.io.IOException
- Overrides:
read
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException
- Overrides:
read
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException
- Overrides:
skip
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classjava.io.FilterInputStream
-
mark
public void mark(int readlimit)
- Overrides:
mark
in classjava.io.FilterInputStream
-
reset
public void reset() throws java.io.IOException
- Overrides:
reset
in classjava.io.FilterInputStream
- Throws:
java.io.IOException
-
-