Package com.norconex.commons.lang.io

# Class CachedOutputStream

java.lang.Object
java.io.OutputStream
com.norconex.commons.lang.io.CachedOutputStream

All Implemented Interfaces:
  `CachedStream`, `Closeable`, `Flushable`, `AutoCloseable`

public class CachedOutputStream extends OutputStream implements CachedStream
`OutputStream` wrapper that caches the output so it can be retrieved once as a `CachedInputStream`. Invoking `getInputStream()` effectively `close()` this stream and it can no longer be written to. Obtaining an input stream before or instead of calling the close method will not delete the cache content, but rather pass the reference to it to the CachedInputStream.
To create new instances of `CachedOutputStream`, use the `CachedStreamFactory` class. Reusing the same factory will ensure all `CachedOutputStream` instances created share the same combined maximum memory. Invoking one of the `newOutputStream(...)` methods on this class have the same effect.
The internal cache stores written bytes into memory, up to to the specified maximum cache size. If content exceeds the cache limit, the cache transforms itself into a file-based cache of unlimited size. Default memory cache size is 128 KB.

Since:
  1.5
See Also:
  - `CachedStreamFactory`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  `void`
  `dispose()`
  Clear the cache attached to this output stream.
  `void`
  `flush()`
  `final Path`
  `getCacheDirectory()`
  Gets the cache directory where temporary cache files are created.
  `CachedInputStream`
  `getInputStream()`
  Return the cached content of the of this output stream as an input stream, before disposing it.
  `long`
  `getMemCacheSize()`
  Get the amount of bytes used for caching this stream.
  `CachedStreamFactory`
  `getStreamFactory()`
  `boolean`
  `isCacheEmpty()`
  Returns `true` if was nothing to cache (no writing was performed) or if the stream was closed.
  `CachedOutputStream`
  `newOuputStream()`
  `CachedOutputStream`
  `newOuputStream(OutputStream os)`
  `void`
  `write(byte[] b, int off, int len)`
  `void`
  `write(int b)`

  ### Methods inherited from class java.io.OutputStream

  `nullOutputStream, write`

  ### Methods inherited from class java.lang.Object

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

- ## Method Details

  - ### write

    public void write(int b) throws IOException
    Specified by:
      `write` in class `OutputStream`
    Throws:
      `IOException`

  - ### write

    public void write(byte[] b, int off, int len) throws IOException
    Overrides:
      `write` in class `OutputStream`
    Throws:
      `IOException`

  - ### getInputStream

    public CachedInputStream getInputStream() throws IOException
    Return the cached content of the of this output stream as an input stream, before disposing it.
    Returns:
      cached input stream
    Throws:
      `IOException` - problem getting input stream

  - ### dispose

    public void dispose() throws IOException
    Clear the cache attached to this output stream.
    Throws:
      `IOException` - could not dispose
    Since:
      3.0.0

  - ### flush

    public void flush() throws IOException
    Specified by:
      `flush` in interface `Flushable`
    Overrides:
      `flush` in class `OutputStream`
    Throws:
      `IOException`

  - ### close

    public void close() throws IOException
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `OutputStream`
    Throws:
      `IOException`

  - ### getCacheDirectory

    public final Path getCacheDirectory()
    Gets the cache directory where temporary cache files are created.
    Specified by:
      `getCacheDirectory` in interface `CachedStream`
    Returns:
      the cache directory

  - ### getStreamFactory

    public CachedStreamFactory getStreamFactory()

  - ### isCacheEmpty

    public boolean isCacheEmpty()
    Returns `true` if was nothing to cache (no writing was performed) or if the stream was closed.
    Returns:
      `true` if empty

  - ### newOuputStream

    public CachedOutputStream newOuputStream(OutputStream os)

  - ### newOuputStream

    public CachedOutputStream newOuputStream()

  - ### getMemCacheSize

    public long getMemCacheSize()
    Description copied from interface: `CachedStream`
    Get the amount of bytes used for caching this stream.
    Specified by:
      `getMemCacheSize` in interface `CachedStream`
    Returns:
      amount of bytes