Top Description Fields Constructors Methods
javax.imageio.stream

pack-priv Class MemoryCache

extends Object
Class Inheritance
Imports
java.util.ArrayList, java.io.InputStream, .OutputStream, .IOException

Package-visible class consolidating common code for 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().

Field Summary

Modifier and TypeField and Description
private static final int
private ArrayList<byte[]>
private long
private long
length

The largest position ever written to the cache.

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public void
disposeBefore(long pos)

Free the blocks up to the position pos.

private byte[]
getCacheBlock(long blockNum)

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 pos bytes are cached, or the end of the source is reached.

private void
pad(long pos)

Ensure that there is space to write a byte at the given position.

public int
read(long pos)

Returns the single byte at the given position, as an int.

public void
read(byte[] b, int off, int len, long pos)

Copy len bytes from the cache, starting at cache position pos, into the array b at offset off.

public void
reset()

Erase the entire cache contents and reset the length to 0.

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 int whose 8 least significant bits will be written.
b
,
long
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 OutputStream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BUFFER_LENGTHback to summary
private static final int BUFFER_LENGTH
cacheback to summary
private ArrayList<byte[]> cache
cacheStartback to summary
private long cacheStart
lengthback to summary
private long length

The largest position ever written to the cache.

Constructor Detail

MemoryCacheback to summary
pack-priv MemoryCache()

Method Detail

disposeBeforeback to summary
public void disposeBefore(long pos)

Free the blocks up to the position pos. The byte at pos remains available.

Exceptions
IndexOutOfBoundsException:
if pos is in a block that has already been disposed.
getCacheBlockback to summary
private byte[] getCacheBlock(long blockNum) throws IOException
getLengthback 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.

loadFromStreamback to summary
public long loadFromStream(InputStream stream, long pos) throws IOException

Ensures that at least pos bytes are cached, or the end of the source is reached. The return value is equal to the smaller of pos and the length of the source.

Exceptions
IOException:
if there is no more memory for cache
padback 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

readback to summary
public int read(long pos) throws IOException

Returns the single byte at the given position, as an int. Returns -1 if this position has not been cached or has been disposed.

Exceptions
IOException:
if an I/O error occurs while reading from the byte array
readback to summary
public void read(byte[] b, int off, int len, long pos) throws IOException

Copy len bytes from the cache, starting at cache position pos, into the array b at offset off.

Exceptions
IOException:
if an I/O exception occurs while reading from the byte array
NullPointerException:
if b is null
IndexOutOfBoundsException:
if off, len or pos are negative or if off + len > b.length or if any portion of the requested data is not in the cache (including if pos is in a block that has already been disposed).
resetback 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.

writeback 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.

Parameters
b:byte[]

an array of bytes containing data to be written.

off:int

the starting offset within the data array.

len:int

the number of bytes to be written.

pos:long

the cache position at which to begin writing.

Exceptions
IOException:
if there is an I/O error while writing to the cache
NullPointerException:
if b is null.
IndexOutOfBoundsException:
if off, len, or pos are negative, or if off+len > b.length.
writeback 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.

Parameters
b:int

an int whose 8 least significant bits will be written.

pos:long

the cache position at which to begin writing.

Exceptions
IOException:
if there is an I/O error while writing to the cache
IndexOutOfBoundsException:
if pos is negative.
writeToStreamback to summary
public void writeToStream(OutputStream stream, long pos, long len) throws IOException

Writes out a portion of the cache to an OutputStream. This method preserves no state about the output stream, and does not dispose of any blocks containing bytes written. To dispose blocks, use disposeBefore().

Exceptions
IOException:
if there is an I/O exception while writing to the stream
IndexOutOfBoundsException:
if any portion of the requested data is not in the cache (including if pos is in a block already disposed), or if either pos or len is < 0.