Source code: org/ydp/jai/ByteArraySeekableStream.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
27 /**
28 * A subclass of <code>SeekableStream</code> that takes input from an
29 * array of bytes. Seeking backwards is supported. The
30 * <code>mark()</code> and <code>resest()</code> methods are
31 * supported.
32 *
33 * <p><b> This class is not a committed part of the JAI API. It may
34 * be removed or changed in future releases of JAI.</b>
35 */
36 public class ByteArraySeekableStream extends SeekableStream {
37
38 /** Array holding the source data. */
39 private byte[] src;
40
41 /** The starting offset within the array. */
42 private int offset;
43
44 /** The length of the valid segment of the array. */
45 private int length;
46
47 /** The current output position. */
48 private int pointer;
49
50 /**
51 * Constructs a <code>ByteArraySeekableStream</code> taking
52 * input from a given segment of an input <code>byte</code> array.
53 */
54 public ByteArraySeekableStream(byte[] src, int offset, int length)
55 throws IOException {
56 this.src = src;
57 this.offset = offset;
58 this.length = length;
59 }
60
61 /**
62 * Constructs a <code>ByteArraySeekableStream</code> taking
63 * input from an entire input <code>byte</code> array.
64 */
65 public ByteArraySeekableStream(byte[] src) throws IOException {
66 this(src, 0, src.length);
67 }
68
69 /**
70 * Returns <code>true</code> since this object supports seeking
71 * backwards.
72 */
73 public boolean canSeekBackwards() {
74 return true;
75 }
76
77 /**
78 * Returns the current offset in this stream.
79 *
80 * @return the offset from the beginning of the stream, in bytes,
81 * at which the next read occurs.
82 */
83 public long getFilePointer() {
84 return (long)pointer;
85 }
86
87 /**
88 * Sets the offset, measured from the beginning of this
89 * stream, at which the next read occurs. Seeking backwards is
90 * allowed.
91 *
92 * @param pos the offset position, measured in bytes from the
93 * beginning of the stream, at which to set the stream
94 * pointer.
95 */
96 public void seek(long pos) {
97 pointer = (int)pos;
98 }
99
100 /**
101 * Reads the next byte of data from the input array. The value byte is
102 * returned as an <code>int</code> in the range <code>0</code> to
103 * <code>255</code>. If no byte is available because the end of the stream
104 * has been reached, the value <code>-1</code> is returned.
105 */
106 public int read() {
107 if (pointer < length + offset) {
108 return (int)(src[pointer++ + offset] & 0xff);
109 } else {
110 return -1;
111 }
112 }
113
114 /**
115 * Copies up to <code>len</code> bytes of data from the input array into
116 * an array of bytes. An attempt is made to copy as many as
117 * <code>len</code> bytes, but a smaller number may be copied, possibly
118 * zero. The number of bytes actually copied is returned as an integer.
119 *
120 * <p> If <code>b</code> is <code>null</code>, a
121 * <code>NullPointerException</code> is thrown.
122 *
123 * <p> If <code>off</code> is negative, or <code>len</code> is negative, or
124 * <code>off+len</code> is greater than the length of the array
125 * <code>b</code>, then an <code>IndexOutOfBoundsException</code> is
126 * thrown.
127 *
128 * <p> If <code>len</code> is zero, then no bytes are copied and
129 * <code>0</code> is returned; otherwise, there is an attempt to copy at
130 * least one byte. If no byte is available because the stream is at end of
131 * stream, the value <code>-1</code> is returned; otherwise, at least one
132 * byte is copied into <code>b</code>.
133 *
134 * <p> The first byte copied is stored into element
135 * <code>b[off]</code>, the next one into <code>b[off+1]</code>,
136 * and so on. The number of bytes copied is, at most, equal to
137 * <code>len</code>. Let <i>k</i> be the number of bytes actually
138 * copied; these bytes will be stored in elements
139 * <code>b[off]</code> through
140 * <code>b[off+</code><i>k</i><code>-1]</code>, leaving elements
141 * <code>b[off+</code><i>k</i><code>]</code> through
142 * <code>b[off+len-1]</code> unaffected.
143 *
144 * <p> In every case, elements <code>b[0]</code> through
145 * <code>b[off]</code> and elements <code>b[off+len]</code> through
146 * <code>b[b.length-1]</code> are unaffected.
147 *
148 * @param b the buffer into which the data is copied.
149 * @param off the start offset in array <code>b</code>
150 * at which the data is written.
151 * @param len the maximum number of bytes to copy.
152 * @return the total number of bytes read into the buffer, or
153 * <code>-1</code> if there is no more data because the end of
154 * the stream has been reached.
155 */
156 public int read(byte[] b, int off, int len) {
157 if (b == null) {
158 throw new NullPointerException();
159 }
160 if ((off < 0) || (len < 0) || (off + len > b.length)) {
161 throw new IndexOutOfBoundsException();
162 }
163 if (len == 0) {
164 return 0;
165 }
166
167 int oldPointer = pointer;
168 pointer = Math.min(pointer + len, length + offset);
169
170 if (pointer == oldPointer) {
171 return -1;
172 } else {
173 System.arraycopy(src, oldPointer, b, off, pointer - oldPointer);
174 return pointer - oldPointer;
175 }
176 }
177
178 /**
179 * Attempts to skip over <code>n</code> bytes of input discarding the
180 * skipped bytes.
181 * <p>
182 *
183 * This method may skip over some smaller number of bytes, possibly zero.
184 * This may result from any of a number of conditions; reaching end of
185 * stream before <code>n</code> bytes have been skipped is only one
186 * possibility. This method never throws an <code>EOFException</code>.
187 * The actual number of bytes skipped is returned. If <code>n</code>
188 * is negative, no bytes are skipped.
189 *
190 * @param n the number of bytes to be skipped.
191 * @return the actual number of bytes skipped.
192 */
193 public int skipBytes(int n) {
194 int oldPointer = pointer;
195 pointer = Math.min(pointer + n, length + offset);
196 return pointer - oldPointer;
197 }
198
199 /** Does nothing. */
200 public void close() {
201 }
202
203 /** Returns the number of valid bytes in the input array. */
204 public long length() {
205 return length;
206 }
207 }