|
Java example source code file (MemoryCache.java)
The MemoryCache.java Java example source code
/*
* Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package javax.imageio.stream;
import java.util.ArrayList;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
/**
* Package-visible class consolidating common code for
* <code>MemoryCacheImageInputStream and
* <code>MemoryCacheImageOutputStream.
* This class keeps an <code>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
* <code>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.
*
* <p> The total number of blocks resident in the cache must not
* exceed <code>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 <code>MemoryCache may be reused after a call
* to <code>reset().
*/
class MemoryCache {
private static final int BUFFER_LENGTH = 8192;
private ArrayList cache = new ArrayList();
private long cacheStart = 0L;
/**
* The largest position ever written to the cache.
*/
private long length = 0L;
private byte[] getCacheBlock(long blockNum) throws IOException {
long blockOffset = blockNum - cacheStart;
if (blockOffset > Integer.MAX_VALUE) {
// This can only happen when the cache hits 16 terabytes of
// contiguous data...
throw new IOException("Cache addressing limit exceeded!");
}
return (byte[])cache.get((int)blockOffset);
}
/**
* Ensures that at least <code>pos bytes are cached,
* or the end of the source is reached. The return value
* is equal to the smaller of <code>pos and the
* length of the source.
*/
public long loadFromStream(InputStream stream, long pos)
throws IOException {
// We've already got enough data cached
if (pos < length) {
return pos;
}
int offset = (int)(length % BUFFER_LENGTH);
byte [] buf = null;
long len = pos - length;
if (offset != 0) {
buf = getCacheBlock(length/BUFFER_LENGTH);
}
while (len > 0) {
if (buf == null) {
try {
buf = new byte[BUFFER_LENGTH];
} catch (OutOfMemoryError e) {
throw new IOException("No memory left for cache!");
}
offset = 0;
}
int left = BUFFER_LENGTH - offset;
int nbytes = (int)Math.min(len, (long)left);
nbytes = stream.read(buf, offset, nbytes);
if (nbytes == -1) {
return length; // EOF
}
if (offset == 0) {
cache.add(buf);
}
len -= nbytes;
length += nbytes;
offset += nbytes;
if (offset >= BUFFER_LENGTH) {
// we've filled the current buffer, so a new one will be
// allocated next time around (and offset will be reset to 0)
buf = null;
}
}
return pos;
}
/**
* Writes out a portion of the cache to an <code>OutputStream.
* This method preserves no state about the output stream, and does
* not dispose of any blocks containing bytes written. To dispose
* blocks, use {@link #disposeBefore <code>disposeBefore()}.
*
* @exception IndexOutOfBoundsException if any portion of
* the requested data is not in the cache (including if <code>pos
* is in a block already disposed), or if either <code>pos or
* <code>len is < 0.
*/
public void writeToStream(OutputStream stream, long pos, long len)
throws IOException {
if (pos + len > length) {
throw new IndexOutOfBoundsException("Argument out of cache");
}
if ((pos < 0) || (len < 0)) {
throw new IndexOutOfBoundsException("Negative pos or len");
}
if (len == 0) {
return;
}
long bufIndex = pos/BUFFER_LENGTH;
if (bufIndex < cacheStart) {
throw new IndexOutOfBoundsException("pos already disposed");
}
int offset = (int)(pos % BUFFER_LENGTH);
byte[] buf = getCacheBlock(bufIndex++);
while (len > 0) {
if (buf == null) {
buf = getCacheBlock(bufIndex++);
offset = 0;
}
int nbytes = (int)Math.min(len, (long)(BUFFER_LENGTH - offset));
stream.write(buf, offset, nbytes);
buf = null;
len -= nbytes;
}
}
/**
* Ensure that there is space to write a byte at the given position.
*/
private void pad(long pos) throws IOException {
long currIndex = cacheStart + cache.size() - 1;
long lastIndex = pos/BUFFER_LENGTH;
long numNewBuffers = lastIndex - currIndex;
for (long i = 0; i < numNewBuffers; i++) {
try {
cache.add(new byte[BUFFER_LENGTH]);
} catch (OutOfMemoryError e) {
throw new IOException("No memory left for cache!");
}
}
}
/**
* 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.
*
* @param b an array of bytes containing data to be written.
* @param off the starting offset withing the data array.
* @param len the number of bytes to be written.
* @param pos the cache position at which to begin writing.
*
* @exception NullPointerException if <code>b is
Other Java examples (source code examples)Here is a short list of links related to this Java MemoryCache.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.