Package com.norconex.commons.lang.io

# Class CachedStreamFactory

java.lang.Object
com.norconex.commons.lang.io.CachedStreamFactory

public class CachedStreamFactory extends Object

Factory for input/output streams caching values for repeat usage.

When using a constructor without all arguments, default values are used. System properties are first checked for such values under these keys:

- `cachedstream.mem.pool`: Pool max memory.
- `cachedstream.mem.instance`: Instances max memory.
- `cachedstream.dir`: Cache directory.

The following are default initialization values when not supplied via constructor arguments or System properties:

- Pool max memory: 1 GB
- Instances max memory: : 100 MB
- Cache directory: Uses the system temporary directory.

Initialization values passed in constructor always take precedence.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `class`
  `CachedStreamFactory.MemoryTracker`

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `DEFAULT_MAX_MEM_INSTANCE`
  `static final int`
  `DEFAULT_MAX_MEM_POOL`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CachedStreamFactory()`
  Creates a new instance with default values (see class documentation)
  `CachedStreamFactory(int maxMemoryPool, int maxMemoryInstance)`
  Constructor.
  `CachedStreamFactory(int maxMemoryPool, int maxMemoryInstance, Path cacheDirectory)`
  Constructor.
  `CachedStreamFactory(Path cacheDirectory)`
  Creates a new instance with default memory values (see class documentation).

- ## Method Summary

  Modifier and Type
  Method
  Description
  `int`
  `getMaxMemoryInstance()`
  `int`
  `getMaxMemoryPool()`
  `CachedInputStream`
  `newInputStream()`
  Creates an empty input stream.
  `CachedInputStream`
  `newInputStream(File file)`
  `CachedInputStream`
  `newInputStream(InputStream is)`
  `CachedInputStream`
  `newInputStream(String content)`
  Creates a new input stream, assuming UTF-8 content.
  `CachedInputStream`
  `newInputStream(Path path)`
  Creates a new cached input stream.
  `CachedOutputStream`
  `newOuputStream()`
  `CachedOutputStream`
  `newOuputStream(OutputStream os)`

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

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

- ## Field Details

  - ### DEFAULT_MAX_MEM_INSTANCE

    public static final int DEFAULT_MAX_MEM_INSTANCE

  - ### DEFAULT_MAX_MEM_POOL

    public static final int DEFAULT_MAX_MEM_POOL

- ## Constructor Details

  - ### CachedStreamFactory

    public CachedStreamFactory(int maxMemoryPool, int maxMemoryInstance)
    Constructor.
    Parameters:
      `maxMemoryPool` - maximum number of bytes used for memory caching by all instances created by this factory combined
      `maxMemoryInstance` - maximum number of bytes used for memory by each cached stream instance created

  - ### CachedStreamFactory

    public CachedStreamFactory(int maxMemoryPool, int maxMemoryInstance, Path cacheDirectory)
    Constructor.
    Parameters:
      `maxMemoryPool` - maximum number of bytes used for memory caching by all instances created by this factory combined
      `maxMemoryInstance` - maximum number of bytes used for memory by each cached stream instance created
      `cacheDirectory` - location where file-based caching takes place
    Since:
      2.0.0

  - ### CachedStreamFactory

    public CachedStreamFactory(Path cacheDirectory)
    Creates a new instance with default memory values (see class documentation).
    Parameters:
      `cacheDirectory` - location where file-based caching takes place
    Since:
      2.0.0

  - ### CachedStreamFactory

    public CachedStreamFactory()
    Creates a new instance with default values (see class documentation)
    Since:
      2.0.0

- ## Method Details

  - ### getMaxMemoryPool

    public int getMaxMemoryPool()

  - ### getMaxMemoryInstance

    public int getMaxMemoryInstance()

  - ### newInputStream

    public CachedInputStream newInputStream()
    Creates an empty input stream. Useful when you need an input stream but null is not accepted.
    Returns:
      an empty cached input stream

  - ### newInputStream

    public CachedInputStream newInputStream(String content)
    Creates a new input stream, assuming UTF-8 content.
    Parameters:
      `content` - content to stream
    Returns:
      cached input stream

  - ### newInputStream

    public CachedInputStream newInputStream(File file)

  - ### newInputStream

    public CachedInputStream newInputStream(Path path)
    Creates a new cached input stream.
    Parameters:
      `path` - path where to cache large files
    Returns:
      cached input stream
    Since:
      2.0.0

  - ### newInputStream

    public CachedInputStream newInputStream(InputStream is)

  - ### newOuputStream

    public CachedOutputStream newOuputStream(OutputStream os)

  - ### newOuputStream

    public CachedOutputStream newOuputStream()