001: /*
002: * $RCSfile: MemoryCacheSeekableStream.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.2 $
009: * $Date: 2005/11/17 00:47:30 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codec;
013:
014: import java.io.InputStream;
015: import java.io.IOException;
016: import java.util.Vector;
017:
018: /**
019: * A subclass of <code>SeekableStream</code> that may be used to wrap
020: * a regular <code>InputStream</code>. Seeking backwards is supported
021: * by means of an in-memory cache. For greater efficiency,
022: * <code>FileCacheSeekableStream</code> should be used in
023: * circumstances that allow the creation of a temporary file.
024: *
025: * <p> The <code>mark()</code> and <code>reset()</code> methods are
026: * supported.
027: *
028: * <p><b> This class is not a committed part of the JAI API. It may
029: * be removed or changed in future releases of JAI.</b>
030: */
031: public final class MemoryCacheSeekableStream extends SeekableStream {
032:
033: /** The source input stream. */
034: private InputStream src;
035:
036: /** Position of first unread byte. */
037: private long pointer = 0;
038:
039: /** Log_2 of the sector size. */
040: private static final int SECTOR_SHIFT = 9;
041:
042: /** The sector size. */
043: private static final int SECTOR_SIZE = 1 << SECTOR_SHIFT;
044:
045: /** A mask to determine the offset within a sector. */
046: private static final int SECTOR_MASK = SECTOR_SIZE - 1;
047:
048: /** A Vector of source sectors. */
049: private Vector data = new Vector();
050:
051: /** Number of sectors stored. */
052: int sectors = 0;
053:
054: /** Number of bytes read. */
055: int length = 0;
056:
057: /** True if we've previously reached the end of the source stream */
058: boolean foundEOS = false;
059:
060: /**
061: * Constructs a <code>MemoryCacheSeekableStream</code> that takes
062: * its source data from a regular <code>InputStream</code>.
063: * Seeking backwards is supported by means of an in-memory cache.
064: */
065: public MemoryCacheSeekableStream(InputStream src) {
066: this .src = src;
067: }
068:
069: /** Forwards the request to the real <code>InputStream</code>. */
070: public final int available() throws IOException {
071: return src.available();
072: }
073:
074: /**
075: * Ensures that at least <code>pos</code> bytes are cached,
076: * or the end of the source is reached. The return value
077: * is equal to the smaller of <code>pos</code> and the
078: * length of the source stream.
079: */
080: private long readUntil(long pos) throws IOException {
081: // We've already got enough data cached
082: if (pos < length) {
083: return pos;
084: }
085: // pos >= length but length isn't getting any bigger, so return it
086: if (foundEOS) {
087: return length;
088: }
089:
090: int sector = (int) (pos >> SECTOR_SHIFT);
091:
092: // First unread sector
093: int startSector = length >> SECTOR_SHIFT;
094:
095: // Read sectors until the desired sector
096: for (int i = startSector; i <= sector; i++) {
097: byte[] buf = new byte[SECTOR_SIZE];
098: data.addElement(buf);
099:
100: // Read up to SECTOR_SIZE bytes
101: int len = SECTOR_SIZE;
102: int off = 0;
103: while (len > 0) {
104: int nbytes = src.read(buf, off, len);
105: // Found the end-of-stream
106: if (nbytes == -1) {
107: foundEOS = true;
108: return length;
109: }
110: off += nbytes;
111: len -= nbytes;
112:
113: // Record new data length
114: length += nbytes;
115: }
116: }
117:
118: return length;
119: }
120:
121: /**
122: * Returns <code>true</code> since all
123: * <code>MemoryCacheSeekableStream</code> instances support seeking
124: * backwards.
125: */
126: public boolean canSeekBackwards() {
127: return true;
128: }
129:
130: /**
131: * Returns the current offset in this file.
132: *
133: * @return the offset from the beginning of the file, in bytes,
134: * at which the next read occurs.
135: */
136: public long getFilePointer() {
137: return pointer;
138: }
139:
140: /**
141: * Sets the file-pointer offset, measured from the beginning of this
142: * file, at which the next read occurs.
143: *
144: * @param pos the offset position, measured in bytes from the
145: * beginning of the file, at which to set the file
146: * pointer.
147: * @exception IOException if <code>pos</code> is less than
148: * <code>0</code> or if an I/O error occurs.
149: */
150: public void seek(long pos) throws IOException {
151: if (pos < 0) {
152: throw new IOException(JaiI18N
153: .getString("MemoryCacheSeekableStream0"));
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: */
169: public int read() throws IOException {
170: long next = pointer + 1;
171: long pos = readUntil(next);
172: if (pos >= next) {
173: byte[] buf = (byte[]) data
174: .elementAt((int) (pointer >> SECTOR_SHIFT));
175: return buf[(int) (pointer++ & SECTOR_MASK)] & 0xff;
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: */
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: // End-of-stream
241: if (pos <= pointer) {
242: return -1;
243: }
244:
245: byte[] buf = (byte[]) data
246: .elementAt((int) (pointer >> SECTOR_SHIFT));
247: int nbytes = Math.min((int) (pos < pointer + len ? pos
248: - pointer : len), SECTOR_SIZE
249: - (int) (pointer & SECTOR_MASK));
250: System.arraycopy(buf, (int) (pointer & SECTOR_MASK), b, off,
251: nbytes);
252: pointer += nbytes;
253: return nbytes;
254: }
255: }
|