001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: FileCacheSeekableStream.java 447277 2006-09-18 06:19:34Z jeremias $ */
019:
020: package org.apache.xmlgraphics.image.codec.util;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.RandomAccessFile;
026:
027: /**
028: * A subclass of <code>SeekableStream</code> that may be used to wrap
029: * a regular <code>InputStream</code>. Seeking backwards is supported
030: * by means of a file cache. In circumstances that do not allow the
031: * creation of a temporary file (for example, due to security
032: * consideration or the absence of local disk), the
033: * <code>MemoryCacheSeekableStream</code> class may be used instead.
034: *
035: * <p> The <code>mark()</code> and <code>reset()</code> methods are
036: * supported.
037: *
038: * <p><b> This class is not a committed part of the JAI API. It may
039: * be removed or changed in future releases of JAI.</b>
040: */
041: public final class FileCacheSeekableStream extends SeekableStream {
042:
043: /** The source stream. */
044: private InputStream stream;
045:
046: /** The cache File. */
047: private File cacheFile;
048:
049: /** The cache as a RandomAcessFile. */
050: private RandomAccessFile cache;
051:
052: /** The length of the read buffer. */
053: private int bufLen = 1024;
054:
055: /** The read buffer. */
056: private byte[] buf = new byte[bufLen];
057:
058: /** Number of bytes in the cache. */
059: private long length = 0;
060:
061: /** Next byte to be read. */
062: private long pointer = 0;
063:
064: /** True if we've encountered the end of the source stream. */
065: private boolean foundEOF = false;
066:
067: /**
068: * Constructs a <code>MemoryCacheSeekableStream</code> that takes
069: * its source data from a regular <code>InputStream</code>.
070: * Seeking backwards is supported by means of an file cache.
071: *
072: * <p> An <code>IOException</code> will be thrown if the
073: * attempt to create the cache file fails for any reason.
074: */
075: public FileCacheSeekableStream(InputStream stream)
076: throws IOException {
077: this .stream = stream;
078: this .cacheFile = File.createTempFile("jai-FCSS-", ".tmp");
079: cacheFile.deleteOnExit();
080: this .cache = new RandomAccessFile(cacheFile, "rw");
081: }
082:
083: /**
084: * Ensures that at least <code>pos</code> bytes are cached,
085: * or the end of the source is reached. The return value
086: * is equal to the smaller of <code>pos</code> and the
087: * length of the source file.
088: */
089: private long readUntil(long pos) throws IOException {
090: // We've already got enough data cached
091: if (pos < length) {
092: return pos;
093: }
094: // pos >= length but length isn't getting any bigger, so return it
095: if (foundEOF) {
096: return length;
097: }
098:
099: long len = pos - length;
100: cache.seek(length);
101: while (len > 0) {
102: // Copy a buffer's worth of data from the source to the cache
103: // bufLen will always fit into an int so this is safe
104: int nbytes = stream.read(buf, 0, (int) Math
105: .min(len, bufLen));
106: if (nbytes == -1) {
107: foundEOF = true;
108: return length;
109: }
110:
111: cache.setLength(cache.length() + nbytes);
112: cache.write(buf, 0, nbytes);
113: len -= nbytes;
114: length += nbytes;
115: }
116:
117: return pos;
118: }
119:
120: /**
121: * Returns <code>true</code> since all
122: * <code>FileCacheSeekableStream</code> instances support seeking
123: * backwards.
124: */
125: public boolean canSeekBackwards() {
126: return true;
127: }
128:
129: /**
130: * Returns the current offset in this file.
131: *
132: * @return the offset from the beginning of the file, in bytes,
133: * at which the next read occurs.
134: */
135: public long getFilePointer() {
136: return pointer;
137: }
138:
139: /**
140: * Sets the file-pointer offset, measured from the beginning of this
141: * file, at which the next read occurs.
142: *
143: * @param pos the offset position, measured in bytes from the
144: * beginning of the file, at which to set the file
145: * pointer.
146: * @exception IOException if <code>pos</code> is less than
147: * <code>0</code> or if an I/O error occurs.
148: */
149: public void seek(long pos) throws IOException {
150: if (pos < 0) {
151: throw new IOException(PropertyUtil
152: .getString("FileCacheSeekableStream0"));
153: }
154: pointer = pos;
155: }
156:
157: /**
158: * Reads the next byte of data from the input stream. The value byte is
159: * returned as an <code>int</code> in the range <code>0</code> to
160: * <code>255</code>. If no byte is available because the end of the stream
161: * has been reached, the value <code>-1</code> is returned. This method
162: * blocks until input data is available, the end of the stream is detected,
163: * or an exception is thrown.
164: *
165: * @return the next byte of data, or <code>-1</code> if the end of the
166: * stream is reached.
167: * @exception IOException if an I/O error occurs.
168: */
169: public int read() throws IOException {
170: long next = pointer + 1;
171: long pos = readUntil(next);
172: if (pos >= next) {
173: cache.seek(pointer++);
174: return cache.read();
175: } else {
176: return -1;
177: }
178: }
179:
180: /**
181: * Reads up to <code>len</code> bytes of data from the input stream into
182: * an array of bytes. An attempt is made to read as many as
183: * <code>len</code> bytes, but a smaller number may be read, possibly
184: * zero. The number of bytes actually read is returned as an integer.
185: *
186: * <p> This method blocks until input data is available, end of file is
187: * detected, or an exception is thrown.
188: *
189: * <p> If <code>b</code> is <code>null</code>, a
190: * <code>NullPointerException</code> is thrown.
191: *
192: * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
193: * <code>off+len</code> is greater than the length of the array
194: * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
195: * thrown.
196: *
197: * <p> If <code>len</code> is zero, then no bytes are read and
198: * <code>0</code> is returned; otherwise, there is an attempt to read at
199: * least one byte. If no byte is available because the stream is at end of
200: * file, the value <code>-1</code> is returned; otherwise, at least one
201: * byte is read and stored into <code>b</code>.
202: *
203: * <p> The first byte read is stored into element <code>b[off]</code>, the
204: * next one into <code>b[off+1]</code>, and so on. The number of bytes read
205: * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
206: * bytes actually read; these bytes will be stored in elements
207: * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
208: * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
209: * <code>b[off+len-1]</code> unaffected.
210: *
211: * <p> In every case, elements <code>b[0]</code> through
212: * <code>b[off]</code> and elements <code>b[off+len]</code> through
213: * <code>b[b.length-1]</code> are unaffected.
214: *
215: * <p> If the first byte cannot be read for any reason other than end of
216: * file, then an <code>IOException</code> is thrown. In particular, an
217: * <code>IOException</code> is thrown if the input stream has been closed.
218: *
219: * @param b the buffer into which the data is read.
220: * @param off the start offset in array <code>b</code>
221: * at which the data is written.
222: * @param len the maximum number of bytes to read.
223: * @return the total number of bytes read into the buffer, or
224: * <code>-1</code> if there is no more data because the end of
225: * the stream has been reached.
226: * @exception IOException if an I/O error occurs.
227: */
228: public int read(byte[] b, int off, int len) throws IOException {
229: if (b == null) {
230: throw new NullPointerException();
231: }
232: if ((off < 0) || (len < 0) || (off + len > b.length)) {
233: throw new IndexOutOfBoundsException();
234: }
235: if (len == 0) {
236: return 0;
237: }
238:
239: long pos = readUntil(pointer + len);
240:
241: // len will always fit into an int so this is safe
242: len = (int) Math.min(len, pos - pointer);
243: if (len > 0) {
244: cache.seek(pointer);
245: cache.readFully(b, off, len);
246: pointer += len;
247: return len;
248: } else {
249: return -1;
250: }
251: }
252:
253: /**
254: * Closes this stream and releases any system resources
255: * associated with the stream.
256: *
257: * @throws IOException if an I/O error occurs.
258: */
259: public void close() throws IOException {
260: super.close();
261: cache.close();
262: cacheFile.delete();
263: }
264: }
|