All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twelvemonkeys.io.MemoryCacheSeekableStream Maven / Gradle / Ivy

There is a newer version: 2.3
Show newest version
/*
 * Copyright (c) 2008, Harald Kuhr
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name "TwelveMonkeys" nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.twelvemonkeys.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * A {@code SeekableInputStream} implementation that caches data in memory.
 * 

* * @see FileCacheSeekableStream * * @author Harald Kuhr * @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/io/MemoryCacheSeekableStream.java#1 $ */ public final class MemoryCacheSeekableStream extends SeekableInputStream { final private InputStream mStream; private long mStreamPosition; private MemoryCache mCache = new MemoryCache(); /** * Creates a {@code MemoryCacheSeekableStream}, reading from the given * {@code InputStream}. Data will be cached in memory. * * @param pStream the {@code InputStream} to read from. */ public MemoryCacheSeekableStream(InputStream pStream) { mStream = pStream; } public int available() throws IOException { long avail = mStreamPosition - mPosition + mStream.available(); return avail > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) avail; } public int read() throws IOException { checkOpen(); int read; if (mPosition == mStreamPosition) { // Read a byte from the stream read = mStream.read(); if (read >= 0) { mStreamPosition++; mCache.write(read); } } else { // ..or read byte from the cache syncPosition(); read = mCache.read(); } // TODO: This field is not REALLY considered accessible.. :-P if (read != -1) { mPosition++; } return read; } public int read(byte[] pBytes, int pOffset, int pLength) throws IOException { checkOpen(); int length; if (mPosition == mStreamPosition) { // Read bytes from the stream length = mStream.read(pBytes, pOffset, pLength); if (length > 0) { mStreamPosition += length; mCache.write(pBytes, pOffset, length); } } else { // ...or read bytes from the cache syncPosition(); length = mCache.read(pBytes, pOffset, pLength); } // TODO: This field is not REALLY considered accessible.. :-P if (length > 0) { mPosition += length; } return length; } private void syncPosition() throws IOException { if (mCache.mPosition != mPosition) { mCache.seek(mPosition); // Assure EOF is correctly thrown } } public final boolean isCached() { return true; } public final boolean isCachedMemory() { return true; } public final boolean isCachedFile() { return false; } protected void seekImpl(long pPosition) throws IOException { if (mPosition < pPosition) { // Read diff from stream into cache long left = pPosition - mPosition; int bufferLen = left > 1024 ? 1024 : (int) left; byte[] buffer = new byte[bufferLen]; while (left > 0) { int length = buffer.length < left ? buffer.length : (int) left; int read = mStream.read(buffer, 0, length); if (read > 0) { mCache.write(buffer, 0, read); mStreamPosition += read; left -= read; } else if (read < 0) { break; } } } else if (mPosition > pPosition) { // Seek backwards into the cache mCache.seek(pPosition); } // NOTE: If mPosition == pPosition then we're good to go } protected void flushBeforeImpl(long pPosition) { mCache.flush(pPosition); } protected void closeImpl() { mCache = null; } final static class MemoryCache { final static int BLOCK_SIZE = 1 << 13; private final List mCache = new ArrayList(); private long mLength; private long mPosition; private long mStart; private byte[] getBlock() throws IOException { final long currPos = mPosition - mStart; if (currPos < 0) { throw new IOException("Cache flushed before read position"); } long index = currPos / BLOCK_SIZE; if (index >= Integer.MAX_VALUE) { throw new IOException("Memory cache max size exceeded"); } if (index >= mCache.size()) { try { mCache.add(new byte[BLOCK_SIZE]); //System.out.println("Allocating new block, size: " + BLOCK_SIZE); //System.out.println("New total size: " + mCache.size() * BLOCK_SIZE + " (" + mCache.size() + " blocks)"); } catch (OutOfMemoryError e) { throw new IOException("No more memory for cache: " + mCache.size() * BLOCK_SIZE); } } //System.out.println("index: " + index); return (byte[]) mCache.get((int) index); } public void write(int pByte) throws IOException { byte[] buffer = getBlock(); int idx = (int) (mPosition % BLOCK_SIZE); buffer[idx] = (byte) pByte; mPosition++; if (mPosition > mLength) { mLength = mPosition; } } // TODO: OptimizeMe!!! public void write(byte[] pBuffer, int pOffset, int pLength) throws IOException { //* byte[] buffer = getBlock(); for (int i = 0; i < pLength; i++) { int index = (int) mPosition % BLOCK_SIZE; if (index == 0) { buffer = getBlock(); } buffer[index] = pBuffer[pOffset + i]; mPosition++; } if (mPosition > mLength) { mLength = mPosition; } //*/ /* for (int i = 0; i < pLength; i++) { write(pBuffer[i + pOffset]); } //*/ } public int read() throws IOException { if (mPosition >= mLength) { return -1; } byte[] buffer = getBlock(); int idx = (int) (mPosition % BLOCK_SIZE); mPosition++; return buffer[idx]; } // TODO: OptimizeMe!!! public int read(byte[] pBytes, int pOffset, int pLength) throws IOException { //* if (mPosition >= mLength) { return -1; } byte[] buffer = getBlock(); int idx = (int) (mPosition % BLOCK_SIZE); // Find maxIdx and simplify test in for-loop int iMax = Math.min(pLength, buffer.length - idx); iMax = (int) Math.min(iMax, mLength - mPosition); int i; //for (i = 0; i < pLength && i < buffer.length - idx && i < mLength - mPosition; i++) { for (i = 0; i < iMax; i++) { pBytes[pOffset + i] = buffer[idx + i]; } mPosition += i; return i; //*/ /* int i; for (i = 0; i < pLength; i++) { byte read = (byte) read(); if (read == -1) { break; } pBytes[i + pOffset] = read; } return i; //*/ } public void seek(long pPosition) throws IOException { if (pPosition < mStart) { throw new IOException("Seek before flush position"); } mPosition = pPosition; } public void flush(long pPosition) { int firstPos = (int) (pPosition / BLOCK_SIZE) - 1; for (int i = 0; i < firstPos; i++) { mCache.remove(0); } mStart = pPosition; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy