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.

Author:
Pascal Essiembre
  • 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()