Package de.uplanet.util.base64
Class Base64OutputStream
java.lang.Object
java.io.OutputStream
de.uplanet.util.base64.Base64OutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
Base64 encoding as described in
RFC 2045 section 6.8.
This stream-based encoding will outperform
| 0 A | 17 R | 34 i | 51 z |
| 1 B | 18 S | 35 j | 52 0 |
| 2 C | 19 T | 36 k | 53 1 |
| 3 D | 20 U | 37 l | 54 2 |
| 4 E | 21 V | 38 m | 55 3 |
| 5 F | 22 W | 39 n | 56 4 |
| 6 G | 23 X | 40 o | 57 5 |
| 7 H | 24 Y | 41 p | 58 6 |
| 8 I | 25 Z | 42 q | 59 7 |
| 9 J | 26 a | 43 r | 60 8 |
| 10 K | 27 b | 44 s | 61 9 |
| 11 L | 28 c | 45 t | 62 + |
| 12 M | 29 d | 46 u | 63 / |
| 13 N | 30 e | 47 v | |
| 14 O | 31 f | 48 w | (pad) = |
| 15 P | 32 g | 49 x | |
| 16 Q | 33 h | 50 y |
This stream-based encoding will outperform
Base64Encoder
when writing data sizes beyond about 400 bytes to a StringWriter provided
that the output buffer is properly preallocated using the outputSizeFromInputSize(int) method.
In these cases use the following pattern:
byte[] l_data;
// ...
StringWriter l_sw = new StringWriter(Base64OutputStream.outputSizeFromInputSize(l_data.length));
Base64OutputStream l_out = new Base64OutputStream(l_sw);
l_out.write(l_data);
l_out.close();
String l_strEncoded = l_sw.toString();
// ...
StringWriter l_sw = new StringWriter(Base64OutputStream.outputSizeFromInputSize(l_data.length));
Base64OutputStream l_out = new Base64OutputStream(l_sw);
l_out.write(l_data);
l_out.close();
String l_strEncoded = l_sw.toString();
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBase64OutputStream(Writer p_out) Constructor for Base64OutputStream.Base64OutputStream(XMLStreamWriter p_xmlStreamWriter) Constructor for Base64OutputStream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidfinish()Finishes writing base64-encoded data to the output stream without closing the underlying stream.voidflush()Flush the stream.static intoutputSizeFromInputSize(int p_iSize) Calculates the size of the output for the given input size.voidwrite(byte[] p_buf) Writes an array of bytes to the base64-encoded output stream.voidwrite(byte[] p_buf, int p_iOffs, int p_iLen) Writes an array of bytes to the base64-encoded output stream.voidwrite(int p_byte) Writes a single byte to the base64-encoded output stream.Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
Base64OutputStream
Constructor for Base64OutputStream. Creates a new base64 output stream to write data to the specified underlying output stream with a default 512-byte buffer size.- Parameters:
p_out- The underlying stream.
-
Base64OutputStream
Constructor for Base64OutputStream. Creates a new base64 output stream to write data to the specifiedXMLStreamWriterwith a default 512-byte buffer size.- Parameters:
p_xmlStreamWriter- Thep_xmlStreamWriterto write to.
-
-
Method Details
-
outputSizeFromInputSize
public static int outputSizeFromInputSize(int p_iSize) Calculates the size of the output for the given input size. Note: values obtained for chunks of data are not additive.- Parameters:
p_iSize- The input size in bytes.- Returns:
- The output size in characters.
-
write
Writes a single byte to the base64-encoded output stream.- Specified by:
writein classOutputStream- Parameters:
p_byte- The byte to be written.- Throws:
IOException- If an I/O error occurs.
-
write
Writes an array of bytes to the base64-encoded output stream.- Overrides:
writein classOutputStream- Parameters:
p_buf- The data to be written.- Throws:
IOException- If an I/O error occurs.
-
write
Writes an array of bytes to the base64-encoded output stream.- Overrides:
writein classOutputStream- Parameters:
p_buf- The data to be written.p_iOffs- The start offset of the data.p_iLen- The length of the data.- Throws:
IOException- If an I/O error occurs.
-
finish
Finishes writing base64-encoded data to the output stream without closing the underlying stream.- Throws:
IOException- If an I/O error occurs.
-
flush
Flush the stream. Note: at most two bytes may not be flushed unlessfinish()is applied.- Specified by:
flushin interfaceFlushable- Overrides:
flushin classOutputStream- Throws:
IOException- If an I/O error occurs.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputStream- Throws:
IOException- If an I/O error occurs.
-