# Class BufferedBase64OutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
uk.ac.starlink.util.BufferedBase64OutputStream

All Implemented Interfaces:
  `Closeable, Flushable, AutoCloseable`

public class BufferedBase64OutputStream extends FilterOutputStream
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 FilterOutputStream

`out`

## Constructor Summary

Constructors
Constructor
Description
`BufferedBase64OutputStream(OutputStream out)`
Constructor with default characteristics.
`BufferedBase64OutputStream(OutputStream out, int quadsPerLine, byte[] lineEnd, int linesPerBuf)`
Custom constructor.

## Method Summary

Modifier and Type
Method
Description
`void`
`close()`
Calls `endBase64()` if required before closing.
`void`
`endBase64()`
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`
`getOutputBufferSize()`
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 OutputStream

`nullOutputStream`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### BufferedBase64OutputStream

public BufferedBase64OutputStream(OutputStream out)
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 buffered
  `quadsPerLine` - number of 4-byte groups per output line
  `lineEnd` - sequence of bytes to be written after each output line
  `linesPerBuf` - number of lines buffered before an actual write to the underlying stream is performed

## Method Details

### write

public void write(int b) throws IOException

Overrides:
  `write` in class `FilterOutputStream`
Throws:
  `IOException`

### write

public void write(byte[] b, int off, int len) throws IOException

Overrides:
  `write` in class `FilterOutputStream`
Throws:
  `IOException`

### write

public void write(byte[] b) throws IOException

Overrides:
  `write` in class `FilterOutputStream`
Throws:
  `IOException`

### endBase64

public void endBase64() throws IOException
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 by `close()` 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

public void flush() throws IOException
This calls flush on the underlying stream, but does not flush this stream itself. The `endBase64()` method must be called to do that.

Specified by:
  `flush` in interface `Flushable`
Overrides:
  `flush` in class `FilterOutputStream`
Throws:
  `IOException`

### close

public void close() throws IOException
Calls `endBase64()` if required before closing.

Specified by:
  `close` in interface `AutoCloseable`
Specified by:
  `close` in interface `Closeable`
Overrides:
  `close` in class `FilterOutputStream`
Throws:
  `IOException`