java.lang.Object
org.codelibs.curl.io.ContentCache
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Returns an InputStream to read the content from the cache.
-
Field Details
-
logger
-
-
Constructor Details
-
ContentCache
public ContentCache(byte[] data) -
ContentCache
-
-
Method Details
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getInputStream
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
-