MemoryCacheImageInputStream
and
MemoryCacheImageOutputStream
.
This class keeps an ArrayList
of 8K blocks,
loaded sequentially. Blocks may only be disposed of
from the index 0 forward. As blocks are freed, the
corresponding entries in the array list are set to
null
, but no compacting is performed.
This allows the index for each block to never change,
and the length of the cache is always the same as the
total amount of data ever cached. Cached data is
therefore always contiguous from the point of last
disposal to the current length.
The total number of blocks resident in the cache must not
exceed Integer.MAX_VALUE
. In practice, the limit of
available memory will be exceeded long before this becomes an
issue, since a full cache would contain 8192*2^31 = 16 terabytes of
data.
A MemoryCache
may be reused after a call
to reset()
.
Modifier and Type | Field and Description |
---|---|
private static final int | |
private ArrayList | |
private long | |
private long | length
The largest position ever written to the cache. |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
public void | |
private byte[] | |
public long | getLength()
Returns the total length of data that has been cached, regardless of whether any early blocks have been disposed. |
public long | loadFromStream(InputStream stream, long pos)
Ensures that at least |
private void | |
public int | |
public void | read(byte[] b, int off, int len, long pos)
Copy |
public void | |
public void | write(byte[]
an array of bytes containing data to be written. b, int the starting offset within the data array. off, int the number of bytes to be written. len, long the cache position at which to begin writing. pos)Overwrites and/or appends the cache from a byte array. |
public void | write(int
an b, long int whose 8 least significant bits
will be written.the cache position at which to begin writing. pos)Overwrites or appends a single byte to the cache. |
public void | writeToStream(OutputStream stream, long pos, long len)
Writes out a portion of the cache to an |
BUFFER_LENGTH | back to summary |
---|---|
private static final int BUFFER_LENGTH |
cache | back to summary |
---|---|
private ArrayList<byte[]> cache |
cacheStart | back to summary |
---|---|
private long cacheStart |
length | back to summary |
---|---|
private long length The largest position ever written to the cache. |
MemoryCache | back to summary |
---|---|
pack-priv MemoryCache() |
disposeBefore | back to summary |
---|---|
public void disposeBefore(long pos) Free the blocks up to the position
|
getCacheBlock | back to summary |
---|---|
private byte[] getCacheBlock(long blockNum) throws IOException |
getLength | back to summary |
---|---|
public long getLength() Returns the total length of data that has been cached, regardless of whether any early blocks have been disposed. This value will only ever increase. |
loadFromStream | back to summary |
---|---|
public long loadFromStream(InputStream stream, long pos) throws IOException Ensures that at least
|
pad | back to summary |
---|---|
private void pad(long pos) throws IOException Ensure that there is space to write a byte at the given position. throws IOException if there is no more memory left for cache |
read | back to summary |
---|---|
public int read(long pos) throws IOException Returns the single byte at the given position, as an
|
read | back to summary |
---|---|
public void read(byte[] b, int off, int len, long pos) throws IOException Copy
|
reset | back to summary |
---|---|
public void reset() Erase the entire cache contents and reset the length to 0. The cache object may subsequently be reused as though it had just been allocated. |
write | back to summary |
---|---|
public void write(byte[] b, int off, int len, long pos) throws IOException Overwrites and/or appends the cache from a byte array. The length of the cache will be extended as needed to hold the incoming data.
|
write | back to summary |
---|---|
public void write(int b, long pos) throws IOException Overwrites or appends a single byte to the cache. The length of the cache will be extended as needed to hold the incoming data.
|
writeToStream | back to summary |
---|---|
public void writeToStream(OutputStream stream, long pos, long len) throws IOException Writes out a portion of the cache to an
|