public class ByteSourceCache extends Object
The cached bytes can be viewed as a ByteSource by newView()
.
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 |
---|
ByteSourceCache(ByteSource origin,
Long totalBytes)
Create an in-memory cache of the origin source.
|
Instance Methods | |
---|---|
Long |
getTotalBytes()
Return the total number of bytes in the origin source; null if unknown yet.
|
ByteSource |
newView()
Create a ByteSource view of the cached bytes.
|
public ByteSourceCache(ByteSource origin, Long totalBytes)
totalBytes
- total number of bytes in the origin source. null if unknown.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.