Class ContentCache

java.lang.Object
org.codelibs.curl.io.ContentCache
All Implemented Interfaces:
Closeable, AutoCloseable

public class ContentCache extends Object implements Closeable
ContentCache is a class that provides a way to cache content either in memory or in a file. It implements the Closeable interface to ensure that resources are properly released.

This class supports two types of content caching:

  • In-memory caching using a byte array
  • File-based caching using a File object

When an instance of ContentCache is created with a byte array, the content is cached in memory. When an instance is created with a File object, the content is cached in the specified file.

The close() method deletes the file if the content is cached in a file.

The getInputStream() method provides an InputStream to read the cached content. If the content is cached in a file, it returns a FileInputStream. If the content is cached in memory, it returns a ByteArrayInputStream.

Example usage:

 
 // In-memory caching
 byte[] data = "example data".getBytes();
 try (ContentCache cache = new ContentCache(data)) {
     InputStream inputStream = cache.getInputStream();
     // Read from inputStream
 }

 // File-based caching
 File file = new File("example.txt");
 try (ContentCache cache = new ContentCache(file)) {
     InputStream inputStream = cache.getInputStream();
     // Read from inputStream
 }
 
 
  • Field Details

    • logger

      protected static final Logger logger
  • Constructor Details

    • ContentCache

      public ContentCache(byte[] data)
    • ContentCache

      public ContentCache(File file)
  • Method Details

    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getInputStream

      public InputStream getInputStream() throws IOException
      Returns an InputStream to read the content from the cache. If the content is stored in a file, a FileInputStream is returned. Otherwise, a ByteArrayInputStream is returned to read the data from memory.
      Returns:
      an InputStream to read the cached content
      Throws:
      IOException - if an I/O error occurs while creating the InputStream