Source code: org/ydp/jai/FileCacheSeekableStream.java
1 /*
2 * The contents of this file are subject to the JAVA ADVANCED IMAGING
3 * SAMPLE INPUT-OUTPUT CODECS AND WIDGET HANDLING SOURCE CODE License
4 * Version 1.0 (the "License"); You may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 * http://www.sun.com/software/imaging/JAI/index.html
7 *
8 * Software distributed under the License is distributed on an "AS IS"
9 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 * the License for the specific language governing rights and limitations
11 * under the License.
12 *
13 * The Original Code is JAVA ADVANCED IMAGING SAMPLE INPUT-OUTPUT CODECS
14 * AND WIDGET HANDLING SOURCE CODE.
15 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
16 * Portions created by: _______________________________________
17 * are Copyright (C): _______________________________________
18 * All Rights Reserved.
19 * Contributor(s): _______________________________________
20 */
21
22 package org.ydp.jai;
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.InputStream;
27 import java.io.IOException;
28 import java.io.RandomAccessFile;
29
30 /**
31 * A subclass of <code>SeekableStream</code> that may be used to wrap
32 * a regular <code>InputStream</code>. Seeking backwards is supported
33 * by means of a file cache. In circumstances that do not allow the
34 * creation of a temporary file (for example, due to security
35 * consideration or the absence of local disk), the
36 * <code>MemoryCacheSeekableStream</code> class may be used instead.
37 *
38 * <p> The <code>mark()</code> and <code>reset()</code> methods are
39 * supported.
40 *
41 * <p><b> This class is not a committed part of the JAI API. It may
42 * be removed or changed in future releases of JAI.</b>
43 */
44 public final class FileCacheSeekableStream extends SeekableStream {
45
46 /** The source stream. */
47 private InputStream stream;
48
49 /** The cache File. */
50 private File cacheFile;
51
52 /** The cache as a RandomAcessFile. */
53 private RandomAccessFile cache;
54
55 /** The length of the read buffer. */
56 private int bufLen = 1024;
57
58 /** The read buffer. */
59 private byte[] buf = new byte[bufLen];
60
61 /** Number of bytes in the cache. */
62 private long length = 0;
63
64 /** Next byte to be read. */
65 private long pointer = 0;
66
67 /** True if we've encountered the end of the source stream. */
68 private boolean foundEOF = false;
69
70 /**
71 * Constructs a <code>MemoryCacheSeekableStream</code> that takes
72 * its source data from a regular <code>InputStream</code>.
73 * Seeking backwards is supported by means of an file cache.
74 *
75 * <p> An <code>IOException</code> will be thrown if the
76 * attempt to create the cache file fails for any reason.
77 */
78 public FileCacheSeekableStream(InputStream stream)
79 throws IOException {
80 this.stream = stream;
81 this.cacheFile = File.createTempFile("jai-FCSS-", ".tmp");
82 cacheFile.deleteOnExit();
83 this.cache = new RandomAccessFile(cacheFile, "rw");
84 }
85
86 /**
87 * Ensures that at least <code>pos</code> bytes are cached,
88 * or the end of the source is reached. The return value
89 * is equal to the smaller of <code>pos</code> and the
90 * length of the source file.
91 */
92 private long readUntil(long pos) throws IOException {
93 // We've already got enough data cached
94 if (pos < length) {
95 return pos;
96 }
97 // pos >= length but length isn't getting any bigger, so return it
98 if (foundEOF) {
99 return length;
100 }
101
102 long len = pos - length;
103 cache.seek(length);
104 while (len > 0) {
105 // Copy a buffer's worth of data from the source to the cache
106 // bufLen will always fit into an int so this is safe
107 int nbytes = stream.read(buf, 0, (int)Math.min(len, (long)bufLen));
108 if (nbytes == -1) {
109 foundEOF = true;
110 return length;
111 }
112
113 cache.setLength(cache.length() + nbytes);
114 cache.write(buf, 0, nbytes);
115 len -= nbytes;
116 length += nbytes;
117 }
118
119 return pos;
120 }
121
122 /**
123 * Returns <code>true</code> since all
124 * <code>FileCacheSeekableStream</code> instances support seeking
125 * backwards.
126 */
127 public boolean canSeekBackwards() {
128 return true;
129 }
130
131 /**
132 * Returns the current offset in this file.
133 *
134 * @return the offset from the beginning of the file, in bytes,
135 * at which the next read occurs.
136 */
137 public long getFilePointer() {
138 return pointer;
139 }
140
141 /**
142 * Sets the file-pointer offset, measured from the beginning of this
143 * file, at which the next read occurs.
144 *
145 * @param pos the offset position, measured in bytes from the
146 * beginning of the file, at which to set the file
147 * pointer.
148 * @exception IOException if <code>pos</code> is less than
149 * <code>0</code> or if an I/O error occurs.
150 */
151 public void seek(long pos) throws IOException {
152 if (pos < 0) {
153 throw new IOException(JaiI18N.getString("FileCacheSeekableStream0"));
154 }
155 pointer = pos;
156 }
157
158 /**
159 * Reads the next byte of data from the input stream. The value byte is
160 * returned as an <code>int</code> in the range <code>0</code> to
161 * <code>255</code>. If no byte is available because the end of the stream
162 * has been reached, the value <code>-1</code> is returned. This method
163 * blocks until input data is available, the end of the stream is detected,
164 * or an exception is thrown.
165 *
166 * @return the next byte of data, or <code>-1</code> if the end of the
167 * stream is reached.
168 * @exception IOException if an I/O error occurs.
169 */
170 public int read() throws IOException {
171 long next = pointer + 1;
172 long pos = readUntil(next);
173 if (pos >= next) {
174 cache.seek(pointer++);
175 return cache.read();
176 } else {
177 return -1;
178 }
179 }
180
181 /**
182 * Reads up to <code>len</code> bytes of data from the input stream into
183 * an array of bytes. An attempt is made to read as many as
184 * <code>len</code> bytes, but a smaller number may be read, possibly
185 * zero. The number of bytes actually read is returned as an integer.
186 *
187 * <p> This method blocks until input data is available, end of file is
188 * detected, or an exception is thrown.
189 *
190 * <p> If <code>b</code> is <code>null</code>, a
191 * <code>NullPointerException</code> is thrown.
192 *
193 * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
194 * <code>off+len</code> is greater than the length of the array
195 * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
196 * thrown.
197 *
198 * <p> If <code>len</code> is zero, then no bytes are read and
199 * <code>0</code> is returned; otherwise, there is an attempt to read at
200 * least one byte. If no byte is available because the stream is at end of
201 * file, the value <code>-1</code> is returned; otherwise, at least one
202 * byte is read and stored into <code>b</code>.
203 *
204 * <p> The first byte read is stored into element <code>b[off]</code>, the
205 * next one into <code>b[off+1]</code>, and so on. The number of bytes read
206 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
207 * bytes actually read; these bytes will be stored in elements
208 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
209 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
210 * <code>b[off+len-1]</code> unaffected.
211 *
212 * <p> In every case, elements <code>b[0]</code> through
213 * <code>b[off]</code> and elements <code>b[off+len]</code> through
214 * <code>b[b.length-1]</code> are unaffected.
215 *
216 * <p> If the first byte cannot be read for any reason other than end of
217 * file, then an <code>IOException</code> is thrown. In particular, an
218 * <code>IOException</code> is thrown if the input stream has been closed.
219 *
220 * @param b the buffer into which the data is read.
221 * @param off the start offset in array <code>b</code>
222 * at which the data is written.
223 * @param len the maximum number of bytes to read.
224 * @return the total number of bytes read into the buffer, or
225 * <code>-1</code> if there is no more data because the end of
226 * the stream has been reached.
227 * @exception IOException if an I/O error occurs.
228 */
229 public int read(byte[] b, int off, int len) throws IOException {
230 if (b == null) {
231 throw new NullPointerException();
232 }
233 if ((off < 0) || (len < 0) || (off + len > b.length)) {
234 throw new IndexOutOfBoundsException();
235 }
236 if (len == 0) {
237 return 0;
238 }
239
240 long pos = readUntil(pointer + len);
241
242 // len will always fit into an int so this is safe
243 len = (int)Math.min((long)len, pos - pointer);
244 if (len > 0) {
245 cache.seek(pointer);
246 cache.readFully(b, off, len);
247 pointer += len;
248 return len;
249 } else {
250 return -1;
251 }
252 }
253
254 /**
255 * Closes this stream and releases any system resources
256 * associated with the stream.
257 *
258 * @throws IOException if an I/O error occurs.
259 */
260 public void close() throws IOException {
261 super.close();
262 cache.close();
263 cacheFile.delete();
264 }
265 }