001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.io;
028:
029: import com.sun.cldchi.jvm.JVM;
030:
031: /**
032: * A <code>ByteArrayInputStream</code> contains
033: * an internal buffer that contains bytes that
034: * may be read from the stream. An internal
035: * counter keeps track of the next byte to
036: * be supplied by the <code>read</code> method.
037: *
038: * @version 12/17/01 (CLDC 1.1)
039: * @since JDK1.0, CLDC 1.0
040: */
041: public class ByteArrayInputStream extends InputStream {
042:
043: /**
044: * An array of bytes that was provided
045: * by the creator of the stream. Elements <code>buf[0]</code>
046: * through <code>buf[count-1]</code> are the
047: * only bytes that can ever be read from the
048: * stream; element <code>buf[pos]</code> is
049: * the next byte to be read.
050: */
051: protected byte buf[];
052:
053: /**
054: * The index of the next character to read from the input stream buffer.
055: * This value should always be nonnegative
056: * and not larger than the value of <code>count</code>.
057: * The next byte to be read from the input stream buffer
058: * will be <code>buf[pos]</code>.
059: */
060: protected int pos;
061:
062: /**
063: * The currently marked position in the stream.
064: * ByteArrayInputStream objects are marked at position zero by
065: * default when constructed. They may be marked at another
066: * position within the buffer by the <code>mark()</code> method.
067: * The current buffer position is set to this point by the
068: * <code>reset()</code> method.
069: *
070: * @since JDK1.1
071: */
072: protected int mark = 0;
073:
074: /**
075: * The index one greater than the last valid character in the input
076: * stream buffer.
077: * This value should always be nonnegative
078: * and not larger than the length of <code>buf</code>.
079: * It is one greater than the position of
080: * the last byte within <code>buf</code> that
081: * can ever be read from the input stream buffer.
082: */
083: protected int count;
084:
085: /**
086: * Creates a <code>ByteArrayInputStream</code>
087: * so that it uses <code>buf</code> as its
088: * buffer array.
089: * The buffer array is not copied.
090: * The initial value of <code>pos</code>
091: * is <code>0</code> and the initial value
092: * of <code>count</code> is the length of
093: * <code>buf</code>.
094: *
095: * @param buf the input buffer.
096: */
097: public ByteArrayInputStream(byte buf[]) {
098: this .buf = buf;
099: this .pos = 0;
100: this .count = buf.length;
101: }
102:
103: /**
104: * Creates <code>ByteArrayInputStream</code>
105: * that uses <code>buf</code> as its
106: * buffer array. The initial value of <code>pos</code>
107: * is <code>offset</code> and the initial value
108: * of <code>count</code> is <code>offset+length</code>.
109: * The buffer array is not copied.
110: * <p>
111: * Note that if bytes are simply read from
112: * the resulting input stream, elements <code>buf[pos]</code>
113: * through <code>buf[pos+len-1]</code> will
114: * be read; however, if a <code>reset</code>
115: * operation is performed, then bytes <code>buf[0]</code>
116: * through b<code>uf[pos-1]</code> will then
117: * become available for input.
118: *
119: * @param buf the input buffer.
120: * @param offset the offset in the buffer of the first byte to read.
121: * @param length the maximum number of bytes to read from the buffer.
122: */
123: public ByteArrayInputStream(byte buf[], int offset, int length) {
124: this .buf = buf;
125: this .pos = offset;
126: this .count = Math.min(offset + length, buf.length);
127: this .mark = offset;
128: }
129:
130: /**
131: * Reads the next byte of data from this input stream. The value
132: * byte is returned as an <code>int</code> in the range
133: * <code>0</code> to <code>255</code>. If no byte is available
134: * because the end of the stream has been reached, the value
135: * <code>-1</code> is returned.
136: * <p>
137: * This <code>read</code> method
138: * cannot block.
139: *
140: * @return the next byte of data, or <code>-1</code> if the end of the
141: * stream has been reached.
142: */
143: public synchronized int read() {
144: return (pos < count) ? (buf[pos++] & 0xff) : -1;
145: }
146:
147: /**
148: * Reads up to <code>len</code> bytes of data into an array of bytes
149: * from this input stream.
150: * If <code>pos</code> equals <code>count</code>,
151: * then <code>-1</code> is returned to indicate
152: * end of file. Otherwise, the number <code>k</code>
153: * of bytes read is equal to the smaller of
154: * <code>len</code> and <code>count-pos</code>.
155: * If <code>k</code> is positive, then bytes
156: * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
157: * are copied into <code>b[off]</code> through
158: * <code>b[off+k-1]</code> in the manner performed
159: * by <code>System.arraycopy</code>. The
160: * value <code>k</code> is added into <code>pos</code>
161: * and <code>k</code> is returned.
162: * <p>
163: * This <code>read</code> method cannot block.
164: *
165: * @param b the buffer into which the data is read.
166: * @param off the start offset of the data.
167: * @param len the maximum number of bytes read.
168: * @return the total number of bytes read into the buffer, or
169: * <code>-1</code> if there is no more data because the end of
170: * the stream has been reached.
171: */
172: public synchronized int read(byte b[], int off, int len) {
173: if (b == null) {
174: throw new NullPointerException();
175: } else if ((off < 0) || (off > b.length) || (len < 0)
176: || ((off + len) > b.length) || ((off + len) < 0)) {
177: throw new IndexOutOfBoundsException();
178: }
179: if (pos >= count) {
180: return -1;
181: }
182: if (pos + len > count) {
183: len = count - pos;
184: }
185: if (len <= 0) {
186: return 0;
187: }
188: JVM.unchecked_byte_arraycopy(buf, pos, b, off, len);
189: pos += len;
190: return len;
191: }
192:
193: /**
194: * Skips <code>n</code> bytes of input from this input stream. Fewer
195: * bytes might be skipped if the end of the input stream is reached.
196: * The actual number <code>k</code>
197: * of bytes to be skipped is equal to the smaller
198: * of <code>n</code> and <code>count-pos</code>.
199: * The value <code>k</code> is added into <code>pos</code>
200: * and <code>k</code> is returned.
201: *
202: * @param n the number of bytes to be skipped.
203: * @return the actual number of bytes skipped.
204: */
205: public synchronized long skip(long n) {
206: if (pos + n > count) {
207: n = count - pos;
208: }
209: if (n < 0) {
210: return 0;
211: }
212: pos += n;
213: return n;
214: }
215:
216: /**
217: * Returns the number of bytes that can be read from this input
218: * stream without blocking.
219: * The value returned is
220: * <code>count - pos</code>,
221: * which is the number of bytes remaining to be read from the input buffer.
222: *
223: * @return the number of bytes that can be read from the input stream
224: * without blocking.
225: */
226: public synchronized int available() {
227: return count - pos;
228: }
229:
230: /**
231: * Tests if ByteArrayInputStream supports mark/reset.
232: *
233: * @since JDK1.1
234: */
235: public boolean markSupported() {
236: return true;
237: }
238:
239: /**
240: * Set the current marked position in the stream.
241: * ByteArrayInputStream objects are marked at position zero by
242: * default when constructed. They may be marked at another
243: * position within the buffer by this method.
244: *
245: * @since JDK1.1
246: */
247: public void mark(int readAheadLimit) {
248: mark = pos;
249: }
250:
251: /**
252: * Resets the buffer to the marked position. The marked position
253: * is the beginning unless another position was marked.
254: * The value of <code>pos</code> is set to 0.
255: */
256: public synchronized void reset() {
257: pos = mark;
258: }
259:
260: /**
261: * Closes this input stream and releases any system resources
262: * associated with the stream.
263: * <p>
264: */
265: public synchronized void close() throws IOException {
266: }
267:
268: }
|