Source code: org/ydp/jai/MemoryCacheSeekableStream.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.InputStream;
25 import java.io.IOException;
26 import java.util.Vector;
27
28 /**
29 * A subclass of <code>SeekableStream</code> that may be used to wrap
30 * a regular <code>InputStream</code>. Seeking backwards is supported
31 * by means of an in-memory cache. For greater efficiency,
32 * <code>FileCacheSeekableStream</code> should be used in
33 * circumstances that allow the creation of a temporary file.
34 *
35 * <p> The <code>mark()</code> and <code>reset()</code> methods are
36 * supported.
37 *
38 * <p><b> This class is not a committed part of the JAI API. It may
39 * be removed or changed in future releases of JAI.</b>
40 */
41 public final class MemoryCacheSeekableStream extends SeekableStream {
42
43 /** The source input stream. */
44 private InputStream src;
45
46 /** Position of first unread byte. */
47 private long pointer = 0;
48
49 /** Log_2 of the sector size. */
50 private static final int SECTOR_SHIFT = 9;
51
52 /** The sector size. */
53 private static final int SECTOR_SIZE = 1 << SECTOR_SHIFT;
54
55 /** A mask to determine the offset within a sector. */
56 private static final int SECTOR_MASK = SECTOR_SIZE - 1;
57
58 /** A Vector of source sectors. */
59 private Vector data = new Vector();
60
61 /** Number of sectors stored. */
62 int sectors = 0;
63
64 /** Number of bytes read. */
65 int length = 0;
66
67 /** True if we've previously reached the end of the source stream */
68 boolean foundEOS = 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 in-memory cache.
74 */
75 public MemoryCacheSeekableStream(InputStream src) {
76 this.src = src;
77 }
78
79 /**
80 * Ensures that at least <code>pos</code> bytes are cached,
81 * or the end of the source is reached. The return value
82 * is equal to the smaller of <code>pos</code> and the
83 * length of the source stream.
84 */
85 private long readUntil(long pos) throws IOException {
86 // We've already got enough data cached
87 if (pos < length) {
88 return pos;
89 }
90 // pos >= length but length isn't getting any bigger, so return it
91 if (foundEOS) {
92 return length;
93 }
94
95 int sector = (int)(pos >> SECTOR_SHIFT);
96
97 // First unread sector
98 int startSector = length >> SECTOR_SHIFT;
99
100 // Read sectors until the desired sector
101 for (int i = startSector; i <= sector; i++) {
102 byte[] buf = new byte[SECTOR_SIZE];
103 data.addElement(buf);
104
105 // Read up to SECTOR_SIZE bytes
106 int len = SECTOR_SIZE;
107 int off = 0;
108 while (len > 0) {
109 int nbytes = src.read(buf, off, len);
110 // Found the end-of-stream
111 if (nbytes == -1) {
112 foundEOS = true;
113 return length;
114 }
115 off += nbytes;
116 len -= nbytes;
117
118 // Record new data length
119 length += nbytes;
120 }
121 }
122
123 return length;
124 }
125
126 /**
127 * Returns <code>true</code> since all
128 * <code>MemoryCacheSeekableStream</code> instances support seeking
129 * backwards.
130 */
131 public boolean canSeekBackwards() {
132 return true;
133 }
134
135 /**
136 * Returns the current offset in this file.
137 *
138 * @return the offset from the beginning of the file, in bytes,
139 * at which the next read occurs.
140 */
141 public long getFilePointer() {
142 return pointer;
143 }
144
145 /**
146 * Sets the file-pointer offset, measured from the beginning of this
147 * file, at which the next read occurs.
148 *
149 * @param pos the offset position, measured in bytes from the
150 * beginning of the file, at which to set the file
151 * pointer.
152 * @exception IOException if <code>pos</code> is less than
153 * <code>0</code> or if an I/O error occurs.
154 */
155 public void seek(long pos) throws IOException {
156 if (pos < 0) {
157 throw new IOException(JaiI18N.getString("MemoryCacheSeekableStream0"));
158 }
159 pointer = pos;
160 }
161
162 /**
163 * Reads the next byte of data from the input stream. The value byte is
164 * returned as an <code>int</code> in the range <code>0</code> to
165 * <code>255</code>. If no byte is available because the end of the stream
166 * has been reached, the value <code>-1</code> is returned. This method
167 * blocks until input data is available, the end of the stream is detected,
168 * or an exception is thrown.
169 *
170 * @return the next byte of data, or <code>-1</code> if the end of the
171 * stream is reached.
172 */
173 public int read() throws IOException {
174 long next = pointer + 1;
175 long pos = readUntil(next);
176 if (pos >= next) {
177 byte[] buf =
178 (byte[])data.elementAt((int)(pointer >> SECTOR_SHIFT));
179 return buf[(int)(pointer++ & SECTOR_MASK)] & 0xff;
180 } else {
181 return -1;
182 }
183 }
184
185 /**
186 * Reads up to <code>len</code> bytes of data from the input stream into
187 * an array of bytes. An attempt is made to read as many as
188 * <code>len</code> bytes, but a smaller number may be read, possibly
189 * zero. The number of bytes actually read is returned as an integer.
190 *
191 * <p> This method blocks until input data is available, end of file is
192 * detected, or an exception is thrown.
193 *
194 * <p> If <code>b</code> is <code>null</code>, a
195 * <code>NullPointerException</code> is thrown.
196 *
197 * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
198 * <code>off+len</code> is greater than the length of the array
199 * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
200 * thrown.
201 *
202 * <p> If <code>len</code> is zero, then no bytes are read and
203 * <code>0</code> is returned; otherwise, there is an attempt to read at
204 * least one byte. If no byte is available because the stream is at end of
205 * file, the value <code>-1</code> is returned; otherwise, at least one
206 * byte is read and stored into <code>b</code>.
207 *
208 * <p> The first byte read is stored into element <code>b[off]</code>, the
209 * next one into <code>b[off+1]</code>, and so on. The number of bytes read
210 * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
211 * bytes actually read; these bytes will be stored in elements
212 * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
213 * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
214 * <code>b[off+len-1]</code> unaffected.
215 *
216 * <p> In every case, elements <code>b[0]</code> through
217 * <code>b[off]</code> and elements <code>b[off+len]</code> through
218 * <code>b[b.length-1]</code> are unaffected.
219 *
220 * <p> If the first byte cannot be read for any reason other than end of
221 * file, then an <code>IOException</code> is thrown. In particular, an
222 * <code>IOException</code> is thrown if the input stream has been closed.
223 *
224 * @param b the buffer into which the data is read.
225 * @param off the start offset in array <code>b</code>
226 * at which the data is written.
227 * @param len the maximum number of bytes to read.
228 * @return the total number of bytes read into the buffer, or
229 * <code>-1</code> if there is no more data because the end of
230 * the stream has been reached.
231 */
232 public int read(byte[] b, int off, int len) throws IOException {
233 if (b == null) {
234 throw new NullPointerException();
235 }
236 if ((off < 0) || (len < 0) || (off + len > b.length)) {
237 throw new IndexOutOfBoundsException();
238 }
239 if (len == 0) {
240 return 0;
241 }
242
243 long pos = readUntil(pointer + len);
244 // End-of-stream
245 if (pos <= pointer) {
246 return -1;
247 }
248
249 byte[] buf = (byte[])data.elementAt((int)(pointer >> SECTOR_SHIFT));
250 int nbytes = Math.min(len, SECTOR_SIZE - (int)(pointer & SECTOR_MASK));
251 System.arraycopy(buf, (int)(pointer & SECTOR_MASK),
252 b, off, nbytes);
253 pointer += nbytes;
254 return nbytes;
255 }
256 }