001: /*
002: * $RCSfile: ByteArraySeekableStream.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.1 $
009: * $Date: 2005/02/11 04:55:29 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codec;
013:
014: import java.io.InputStream;
015: import java.io.IOException;
016:
017: /**
018: * A subclass of <code>SeekableStream</code> that takes input from an
019: * array of bytes. Seeking backwards is supported. The
020: * <code>mark()</code> and <code>resest()</code> methods are
021: * supported.
022: *
023: * <p><b> This class is not a committed part of the JAI API. It may
024: * be removed or changed in future releases of JAI.</b>
025: */
026: public class ByteArraySeekableStream extends SeekableStream {
027:
028: /** Array holding the source data. */
029: private byte[] src;
030:
031: /** The starting offset within the array. */
032: private int offset;
033:
034: /** The length of the valid segment of the array. */
035: private int length;
036:
037: /** The current output position. */
038: private int pointer;
039:
040: /**
041: * Constructs a <code>ByteArraySeekableStream</code> taking
042: * input from a given segment of an input <code>byte</code> array.
043: */
044: public ByteArraySeekableStream(byte[] src, int offset, int length)
045: throws IOException {
046: this .src = src;
047: this .offset = offset;
048: this .length = length;
049: }
050:
051: /**
052: * Constructs a <code>ByteArraySeekableStream</code> taking
053: * input from an entire input <code>byte</code> array.
054: */
055: public ByteArraySeekableStream(byte[] src) throws IOException {
056: this (src, 0, src.length);
057: }
058:
059: /**
060: * Returns the number of bytes that can be read from this input
061: * stream without blocking.
062: * The value returned is
063: * <code>Math.min(offset + length, src.length) - pos</code>,
064: * which is the number of bytes remaining to be read from the input buffer.
065: *
066: * @return the number of bytes that can be read from the input stream
067: * without blocking.
068: */
069: public synchronized int available() {
070: ensureOpen();
071: return Math.min(offset + length, src.length) - pointer;
072: }
073:
074: /**
075: * Returns <code>true</code> since this object supports seeking
076: * backwards.
077: */
078: public boolean canSeekBackwards() {
079: return true;
080: }
081:
082: /**
083: * Returns the current offset in this stream.
084: *
085: * @return the offset from the beginning of the stream, in bytes,
086: * at which the next read occurs.
087: */
088: public long getFilePointer() {
089: return (long) pointer;
090: }
091:
092: /**
093: * Sets the offset, measured from the beginning of this
094: * stream, at which the next read occurs. Seeking backwards is
095: * allowed.
096: *
097: * @param pos the offset position, measured in bytes from the
098: * beginning of the stream, at which to set the stream
099: * pointer.
100: */
101: public void seek(long pos) {
102: pointer = (int) pos;
103: }
104:
105: /**
106: * Reads the next byte of data from the input array. The value byte is
107: * returned as an <code>int</code> in the range <code>0</code> to
108: * <code>255</code>. If no byte is available because the end of the stream
109: * has been reached, the value <code>-1</code> is returned.
110: */
111: public int read() {
112: if (pointer < length + offset) {
113: return (int) (src[pointer++ + offset] & 0xff);
114: } else {
115: return -1;
116: }
117: }
118:
119: /**
120: * Copies up to <code>len</code> bytes of data from the input array into
121: * an array of bytes. An attempt is made to copy as many as
122: * <code>len</code> bytes, but a smaller number may be copied, possibly
123: * zero. The number of bytes actually copied is returned as an integer.
124: *
125: * <p> If <code>b</code> is <code>null</code>, a
126: * <code>NullPointerException</code> is thrown.
127: *
128: * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
129: * <code>off+len</code> is greater than the length of the array
130: * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
131: * thrown.
132: *
133: * <p> If <code>len</code> is zero, then no bytes are copied and
134: * <code>0</code> is returned; otherwise, there is an attempt to copy at
135: * least one byte. If no byte is available because the stream is at end of
136: * stream, the value <code>-1</code> is returned; otherwise, at least one
137: * byte is copied into <code>b</code>.
138: *
139: * <p> The first byte copied is stored into element
140: * <code>b[off]</code>, the next one into <code>b[off+1]</code>,
141: * and so on. The number of bytes copied is, at most, equal to
142: * <code>len</code>. Let <i>k</i> be the number of bytes actually
143: * copied; these bytes will be stored in elements
144: * <code>b[off]</code> through
145: * <code>b[off+</code><i>k</i><code>-1]</code>, leaving elements
146: * <code>b[off+</code><i>k</i><code>]</code> through
147: * <code>b[off+len-1]</code> unaffected.
148: *
149: * <p> In every case, elements <code>b[0]</code> through
150: * <code>b[off]</code> and elements <code>b[off+len]</code> through
151: * <code>b[b.length-1]</code> are unaffected.
152: *
153: * @param b the buffer into which the data is copied.
154: * @param off the start offset in array <code>b</code>
155: * at which the data is written.
156: * @param len the maximum number of bytes to copy.
157: * @return the total number of bytes read into the buffer, or
158: * <code>-1</code> if there is no more data because the end of
159: * the stream has been reached.
160: */
161: public int read(byte[] b, int off, int len) {
162: if (b == null) {
163: throw new NullPointerException();
164: }
165: if ((off < 0) || (len < 0) || (off + len > b.length)) {
166: throw new IndexOutOfBoundsException();
167: }
168: if (len == 0) {
169: return 0;
170: }
171:
172: int oldPointer = pointer;
173: pointer = Math.min(pointer + len, length + offset);
174:
175: if (pointer == oldPointer) {
176: return -1;
177: } else {
178: System.arraycopy(src, oldPointer, b, off, pointer
179: - oldPointer);
180: return pointer - oldPointer;
181: }
182: }
183:
184: /**
185: * Attempts to skip over <code>n</code> bytes of input discarding the
186: * skipped bytes.
187: * <p>
188: *
189: * This method may skip over some smaller number of bytes, possibly zero.
190: * This may result from any of a number of conditions; reaching end of
191: * stream before <code>n</code> bytes have been skipped is only one
192: * possibility. This method never throws an <code>EOFException</code>.
193: * The actual number of bytes skipped is returned. If <code>n</code>
194: * is negative, no bytes are skipped.
195: *
196: * @param n the number of bytes to be skipped.
197: * @return the actual number of bytes skipped.
198: */
199: public int skipBytes(int n) {
200: int oldPointer = pointer;
201: pointer = Math.min(pointer + n, length + offset);
202: return pointer - oldPointer;
203: }
204:
205: /** Does nothing. */
206: public void close() {
207: }
208:
209: /** Returns the number of valid bytes in the input array. */
210: public long length() {
211: return length;
212: }
213:
214: /** Check to make sure that the stream has not been closed */
215: private void ensureOpen() {
216: /* This method does nothing for now. Once we add throws clauses
217: * to the I/O methods in this class, it will throw an IOException
218: * if the stream has been closed.
219: */
220: }
221: }
|