# Class FileCacheImageOutputStream

java.lang.Object
javax.imageio.stream.ImageInputStreamImpl
javax.imageio.stream.ImageOutputStreamImpl
javax.imageio.stream.FileCacheImageOutputStream

All Implemented Interfaces:
  `Closeable, DataInput, DataOutput, AutoCloseable, ImageInputStream, ImageOutputStream`

public class FileCacheImageOutputStream extends ImageOutputStreamImpl
An implementation of `ImageOutputStream` that writes its output to a regular `OutputStream`. A file is used to cache data until it is flushed to the output stream.

- ## Field Summary

  ### Fields declared in class ImageInputStreamImpl

  `bitOffset, byteOrder, flushedPos, streamPos`
  Modifier and Type
  Field
  Description
  `protected int`
  `bitOffset`
  The current bit offset within the stream.
  `protected ByteOrder`
  `byteOrder`
  The byte order of the stream as an instance of the enumeration class `java.nio.ByteOrder`, where `ByteOrder.BIG_ENDIAN` indicates network byte order and `ByteOrder.LITTLE_ENDIAN` indicates the reverse order.
  `protected long`
  `flushedPos`
  The position prior to which data may be discarded.
  `protected long`
  `streamPos`
  The current read position within the stream.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `FileCacheImageOutputStream(OutputStream stream, File cacheDir)`
  Constructs a `FileCacheImageOutputStream` that will write to a given `outputStream`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  Closes this `FileCacheImageOutputStream`.
  `void`
  `flushBefore(long pos)`
  Flushes all data prior to the given position to the underlying destination, such as an `OutputStream` or `File`.
  `boolean`
  `isCached()`
  Returns `true` since this `ImageOutputStream` caches data in order to allow seeking backwards.
  `boolean`
  `isCachedFile()`
  Returns `true` since this `ImageOutputStream` maintains a file cache.
  `boolean`
  `isCachedMemory()`
  Returns `false` since this `ImageOutputStream` does not maintain a main memory cache.
  `int`
  `read()`
  Reads a single byte from the stream and returns it as an `int` between 0 and 255.
  `int`
  `read(byte[] b, int off, int len)`
  Reads up to `len` bytes from the stream, and stores them into `b` starting at index `off`.
  `void`
  `seek(long pos)`
  Sets the current stream position and resets the bit offset to 0.
  `void`
  `write(byte[] b, int off, int len)`
  Writes a sequence of bytes to the stream at the current position.
  `void`
  `write(int b)`
  Writes a single byte to the stream at the current position.

  ### Methods declared in class ImageOutputStreamImpl

  `flushBits, write, writeBit, writeBits, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeChars, writeDouble, writeDoubles, writeFloat, writeFloats, writeInt, writeInts, writeLong, writeLongs, writeShort, writeShorts, writeUTF`
  Modifier and Type
  Method
  Description
  `protected final void`
  `flushBits()`
  If the bit offset is non-zero, forces the remaining bits in the current byte to 0 and advances the stream position by one.
  `void`
  `write(byte[] b)`
  Writes a sequence of bytes to the stream at the current position.
  `void`
  `writeBit(int bit)`
  Writes a single bit, given by the least significant bit of the argument, to the stream at the current bit offset within the current byte position.
  `void`
  `writeBits(long bits, int numBits)`
  Writes a sequence of bits, given by the `numBits` least significant bits of the `bits` argument in left-to-right order, to the stream at the current bit offset within the current byte position.
  `void`
  `writeBoolean(boolean v)`
  Writes a `boolean` value to the stream.
  `void`
  `writeByte(int v)`
  Writes the 8 low-order bits of `v` to the stream.
  `void`
  `writeBytes(String s)`
  Writes a string to the output stream.
  `void`
  `writeChar(int v)`
  This method is a synonym for `writeShort`.
  `void`
  `writeChars(char[] c, int off, int len)`
  Writes a sequence of chars to the stream at the current position.
  `void`
  `writeChars(String s)`
  Writes a string to the output stream.
  `void`
  `writeDouble(double v)`
  Writes a `double` value, which is comprised of four bytes, to the output stream.
  `void`
  `writeDoubles(double[] d, int off, int len)`
  Writes a sequence of doubles to the stream at the current position.
  `void`
  `writeFloat(float v)`
  Writes a `float` value, which is comprised of four bytes, to the output stream.
  `void`
  `writeFloats(float[] f, int off, int len)`
  Writes a sequence of floats to the stream at the current position.
  `void`
  `writeInt(int v)`
  Writes the 32 bits of `v` to the stream.
  `void`
  `writeInts(int[] i, int off, int len)`
  Writes a sequence of ints to the stream at the current position.
  `void`
  `writeLong(long v)`
  Writes the 64 bits of `v` to the stream.
  `void`
  `writeLongs(long[] l, int off, int len)`
  Writes a sequence of longs to the stream at the current position.
  `void`
  `writeShort(int v)`
  Writes the 16 low-order bits of `v` to the stream.
  `void`
  `writeShorts(short[] s, int off, int len)`
  Writes a sequence of shorts to the stream at the current position.
  `void`
  `writeUTF(String s)`
  Writes two bytes of length information to the output stream in network byte order, followed by the modified UTF-8 representation of every character in the string `s`.

  ### Methods declared in class ImageInputStreamImpl

  `checkClosed, flush, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, length, mark, read, readBit, readBits, readBoolean, readByte, readBytes, readChar, readDouble, readFloat, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedShort, readUTF, reset, setBitOffset, setByteOrder, skipBytes, skipBytes`
  Modifier and Type
  Method
  Description
  `protected final void`
  `checkClosed()`
  Throws an `IOException` if the stream has been closed.
  `void`
  `flush()`
  Discards the initial position of the stream prior to the current stream position.
  `int`
  `getBitOffset()`
  Returns the current bit offset, as an integer between 0 and 7, inclusive.
  `ByteOrder`
  `getByteOrder()`
  Returns the byte order with which data values will be read from this stream as an instance of the `java.nio.ByteOrder` enumeration.
  `long`
  `getFlushedPosition()`
  Returns the earliest position in the stream to which seeking may be performed.
  `long`
  `getStreamPosition()`
  Returns the current byte position of the stream.
  `long`
  `length()`
  Returns `-1L` to indicate that the stream has unknown length.
  `void`
  `mark()`
  Pushes the current stream position onto a stack of marked positions.
  `int`
  `read(byte[] b)`
  A convenience method that calls `read(b, 0, b.length)`.
  `int`
  `readBit()`
  Reads a single bit from the stream and returns it as an `int` with the value `0` or `1`.
  `long`
  `readBits(int numBits)`
  Reads a bitstring from the stream and returns it as a `long`, with the first bit read becoming the most significant bit of the output.
  `boolean`
  `readBoolean()`
  Reads a byte from the stream and returns a `boolean` value of `true` if it is nonzero, `false` if it is zero.
  `byte`
  `readByte()`
  Reads a byte from the stream and returns it as a `byte` value.
  `void`
  `readBytes(IIOByteBuffer buf, int len)`
  Reads up to `len` bytes from the stream, and modifies the supplied `IIOByteBuffer` to indicate the byte array, offset, and length where the data may be found.
  `char`
  `readChar()`
  Equivalent to `readUnsignedShort`, except that the result is returned using the `char` datatype.
  `double`
  `readDouble()`
  Reads 8 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as a `double`.
  `float`
  `readFloat()`
  Reads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as a `float`.
  `void`
  `readFully(byte[] b)`
  Reads `b.length` bytes from the stream, and stores them into `b` starting at index `0`.
  `void`
  `readFully(byte[] b, int off, int len)`
  Reads `len` bytes from the stream, and stores them into `b` starting at index `off`.
  `void`
  `readFully(char[] c, int off, int len)`
  Reads `len` chars (unsigned 16-bit integers) from the stream according to the current byte order, and stores them into `c` starting at index `off`.
  `void`
  `readFully(double[] d, int off, int len)`
  Reads `len` doubles (64-bit IEEE double-precision floats) from the stream according to the current byte order, and stores them into `d` starting at index `off`.
  `void`
  `readFully(float[] f, int off, int len)`
  Reads `len` floats (32-bit IEEE single-precision floats) from the stream according to the current byte order, and stores them into `f` starting at index `off`.
  `void`
  `readFully(int[] i, int off, int len)`
  Reads `len` ints (signed 32-bit integers) from the stream according to the current byte order, and stores them into `i` starting at index `off`.
  `void`
  `readFully(long[] l, int off, int len)`
  Reads `len` longs (signed 64-bit integers) from the stream according to the current byte order, and stores them into `l` starting at index `off`.
  `void`
  `readFully(short[] s, int off, int len)`
  Reads `len` shorts (signed 16-bit integers) from the stream according to the current byte order, and stores them into `s` starting at index `off`.
  `int`
  `readInt()`
  Reads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as an `int`.
  `String`
  `readLine()`
  Reads the next line of text from the input stream.
  `long`
  `readLong()`
  Reads 8 bytes from the stream, and (conceptually) concatenates them according to the current byte order and returns the result as a `long`.
  `short`
  `readShort()`
  Reads two bytes from the stream, and (conceptually) concatenates them according to the current byte order, and returns the result as a `short` value.
  `int`
  `readUnsignedByte()`
  Reads a byte from the stream, and (conceptually) converts it to an int, masks it with `0xff` in order to strip off any sign-extension bits, and returns it as a `byte` value.
  `long`
  `readUnsignedInt()`
  Reads 4 bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the result to a long, masks it with `0xffffffffL` in order to strip off any sign-extension bits, and returns the result as an unsigned `long` value.
  `int`
  `readUnsignedShort()`
  Reads two bytes from the stream, and (conceptually) concatenates them according to the current byte order, converts the resulting value to an `int`, masks it with `0xffff` in order to strip off any sign-extension buts, and returns the result as an unsigned `int` value.
  `String`
  `readUTF()`
  Reads in a string that has been encoded using a modified UTF-8 format.
  `void`
  `reset()`
  Resets the current stream byte and bit positions from the stack of marked positions.
  `void`
  `setBitOffset(int bitOffset)`
  Sets the bit offset to an integer between 0 and 7, inclusive.
  `void`
  `setByteOrder(ByteOrder byteOrder)`
  Sets the desired byte order for future reads of data values from this stream.
  `int`
  `skipBytes(int n)`
  Advances the current stream position by calling `seek(getStreamPosition() + n)`.
  `long`
  `skipBytes(long n)`
  Advances the current stream position by calling `seek(getStreamPosition() + n)`.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Constructor Details

  - ### FileCacheImageOutputStream

    public FileCacheImageOutputStream(OutputStream stream, File cacheDir) throws IOException
    Constructs a `FileCacheImageOutputStream` that will write to a given `outputStream`.

    A temporary file is used as a cache. If `cacheDir` is non-`null` and is a directory, the file will be created there. If it is `null`, the system-dependent default temporary-file directory will be used (see the documentation for `File.createTempFile` for details).

    Parameters:
      `stream` - an `OutputStream` to write to.
      `cacheDir` - a `File` indicating where the cache file should be created, or `null` to use the system directory.
    Throws:
      `IllegalArgumentException` - if `stream` is `null`.
      `IllegalArgumentException` - if `cacheDir` is non-`null` but is not a directory.
      `IOException` - if a cache file cannot be created.

- ## Method Details

  - ### read

    public int read() throws IOException
    Description copied from class: `ImageInputStreamImpl`
    Reads a single byte from the stream and returns it as an `int` between 0 and 255. If EOF is reached, `-1` is returned.

    Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

    The bit offset within the stream must be reset to zero before the read occurs.

    Specified by:
      `read` in interface `ImageInputStream`
    Specified by:
      `read` in class `ImageInputStreamImpl`
    Returns:
      the value of the next byte in the stream, or `-1` if EOF is reached.
    Throws:
      `IOException` - if the stream has been closed.

  - ### read

    public int read(byte[] b, int off, int len) throws IOException
    Description copied from class: `ImageInputStreamImpl`
    Reads up to `len` bytes from the stream, and stores them into `b` starting at index `off`. If no bytes can be read because the end of the stream has been reached, `-1` is returned.

    The bit offset within the stream must be reset to zero before the read occurs.

    Subclasses must provide an implementation for this method. The subclass implementation should update the stream position before exiting.

    Specified by:
      `read` in interface `ImageInputStream`
    Specified by:
      `read` in class `ImageInputStreamImpl`
    Parameters:
      `b` - an array of bytes to be written to.
      `off` - the starting position within `b` to write to.
      `len` - the maximum number of bytes to read.
    Returns:
      the number of bytes actually read, or `-1` to indicate EOF.
    Throws:
      `IOException` - if an I/O error occurs.

  - ### write

    public void write(int b) throws IOException
    Description copied from interface: `ImageOutputStream`
    Writes a single byte to the stream at the current position. The 24 high-order bits of `b` are ignored.

    If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the `flushBits` method of `ImageOutputStreamImpl` to guarantee this.

    Parameters:
      `b` - an `int` whose lower 8 bits are to be written.
    Throws:
      `IOException` - if an I/O error occurs.

  - ### write

    public void write(byte[] b, int off, int len) throws IOException
    Description copied from interface: `ImageOutputStream`
    Writes a sequence of bytes to the stream at the current position. If `len` is 0, nothing is written. The byte `b[off]` is written first, then the byte `b[off + 1]`, and so on.

    If the bit offset within the stream is non-zero, the remainder of the current byte is padded with 0s and written out first. The bit offset will be 0 after the write. Implementers can use the `flushBits` method of `ImageOutputStreamImpl` to guarantee this.

    Parameters:
      `b` - an array of `byte`s to be written.
      `off` - the start offset in the data.
      `len` - the number of `byte`s to write.
    Throws:
      `IOException` - if an I/O error occurs.

  - ### seek

    public void seek(long pos) throws IOException
    Sets the current stream position and resets the bit offset to 0. It is legal to seek past the end of the file; an `EOFException` will be thrown only if a read is performed. The file length will not be increased until a write is performed.
    Parameters:
      `pos` - a `long` containing the desired file pointer position.
    Throws:
      `IndexOutOfBoundsException` - if `pos` is smaller than the flushed position.
      `IOException` - if any other I/O error occurs.

  - ### isCached

    public boolean isCached()
    Returns `true` since this `ImageOutputStream` caches data in order to allow seeking backwards.
    Specified by:
      `isCached` in interface `ImageInputStream`
    Overrides:
      `isCached` in class `ImageInputStreamImpl`
    Returns:
      `true`.
    See Also:
      - `isCachedMemory()`
      - `isCachedFile()`

  - ### isCachedFile

    public boolean isCachedFile()
    Returns `true` since this `ImageOutputStream` maintains a file cache.
    Specified by:
      `isCachedFile` in interface `ImageInputStream`
    Overrides:
      `isCachedFile` in class `ImageInputStreamImpl`
    Returns:
      `true`.
    See Also:
      - `isCached()`
      - `isCachedMemory()`

  - ### isCachedMemory

    public boolean isCachedMemory()
    Returns `false` since this `ImageOutputStream` does not maintain a main memory cache.
    Specified by:
      `isCachedMemory` in interface `ImageInputStream`
    Overrides:
      `isCachedMemory` in class `ImageInputStreamImpl`
    Returns:
      `false`.
    See Also:
      - `isCached()`
      - `isCachedFile()`

  - ### close

    public void close() throws IOException
    Closes this `FileCacheImageOutputStream`. All pending data is flushed to the output, and the cache file is closed and removed. The destination `OutputStream` is not closed.
    Throws:
      `IOException` - if an error occurs.

  - ### flushBefore

    public void flushBefore(long pos) throws IOException
    Flushes all data prior to the given position to the underlying destination, such as an `OutputStream` or `File`. Attempting to seek to the flushed portion of the stream will result in an `IndexOutOfBoundsException`.
    Parameters:
      `pos` - a `long` containing the length of the stream prefix that may be flushed to the destination.
    Throws:
      `IOException` - if an I/O error occurs.