Package uk.ac.starlink.util
Class BufferedBase64OutputStream
java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
uk.ac.starlink.util.BufferedBase64OutputStream
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
OutputStream that encodes to Base64 with buffering.
Considerably faster than unbuffered implementations.
Note that the endBase64()
method should be called
once at the end of writing to flush the input and ensure that
the output is ended correctly.
Closing the stream will call this if it has not been called already.
- Since:
- 31 Mar 2022
- Author:
- Mark Taylor
-
Field Summary
Fields inherited from class java.io.FilterOutputStream
out
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor with default characteristics.BufferedBase64OutputStream
(OutputStream out, int quadsPerLine, byte[] lineEnd, int linesPerBuf) Custom constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
CallsendBase64()
if required before closing.void
Flushes any data in the buffer and terminates the Base64 output correctly.void
flush()
This calls flush on the underlying stream, but does not flush this stream itself.int
Returns the size of the output buffer; output will be written to the underlying stream in chunks of this size.void
write
(byte[] b) void
write
(byte[] b, int off, int len) void
write
(int b) Methods inherited from class java.io.OutputStream
nullOutputStream
-
Constructor Details
-
BufferedBase64OutputStream
Constructor with default characteristics.- Parameters:
out
- underlying output stream, doesn't need to be buffered
-
BufferedBase64OutputStream
public BufferedBase64OutputStream(OutputStream out, int quadsPerLine, byte[] lineEnd, int linesPerBuf) Custom constructor.- Parameters:
out
- underlying output stream, doesn't need to be bufferedquadsPerLine
- number of 4-byte groups per output linelineEnd
- sequence of bytes to be written after each output linelinesPerBuf
- number of lines buffered before an actual write to the underlying stream is performed
-
-
Method Details
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
write
- Overrides:
write
in classFilterOutputStream
- Throws:
IOException
-
endBase64
Flushes any data in the buffer and terminates the Base64 output correctly. This method must be called, once, after all writes have been completed, otherwise the output will likely not be valid base64. This method will be called byclose()
if it has not been done already. The effect of further writes to this stream following a call to this method is undefined.- Throws:
IOException
-
getOutputBufferSize
public int getOutputBufferSize()Returns the size of the output buffer; output will be written to the underlying stream in chunks of this size.- Returns:
- output buffer length in bytes
-
flush
This calls flush on the underlying stream, but does not flush this stream itself. TheendBase64()
method must be called to do that.- Specified by:
flush
in interfaceFlushable
- Overrides:
flush
in classFilterOutputStream
- Throws:
IOException
-
close
CallsendBase64()
if required before closing.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classFilterOutputStream
- Throws:
IOException
-