public class ByteSourceCache extends Object
The cached bytes can be viewed as a ByteSource by
Example Usage: cache a file content in memory
FileByteSource fileSrc = new FileByteSource("/tmp/data.bin"); ByteSourceCache cache = new ByteSourceCache(fileSrc, null); // read bytes from the cache ByteSource view = cache.newView(); AsyncIterator.forEach( view::read, System.out::println ).finally_( view::close );
If an error occurred while the cache reads the origin source, the error will be reflected when reading from views.
|Constructor and Description|
Create an in-memory cache of the origin source.
Return the total number of bytes in the origin source; null if unknown yet.
Create a ByteSource view of the cached bytes.
public Long getTotalBytes()
This number may be unknown initially (because it was not supplied to the constructor). However, it eventually becomes known after the cache reads all bytes from the origin source (barring any error).
This number may be useful, for example, in Content-Length header of http responses.
public ByteSource newView()
This method is thread-safe.