KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > file > RandomAccessInputStream


1 package com.quadcap.sql.file;
2
3 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.InputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43
44 import com.quadcap.util.Debug;
45
46 /**
47  * An input stream attached to a <code>RandomAccess</code> object.
48  *
49  * @author Stan Bailes
50  */

51 public class RandomAccessInputStream extends InputStream JavaDoc {
52     RandomAccess ra;
53     int position;
54     byte[] buf1 = new byte[1];
55     
56     public RandomAccessInputStream(RandomAccess ra) {
57     this.ra = ra;
58     this.position = 0;
59     }
60
61     public int getPosition() { return position; }
62     public void setPosition(int p) { position = p; }
63
64     /**
65      * Reads the next byte of data from this input stream. The value
66      * byte is returned as an <code>int</code> in the range
67      * <code>0</code> to <code>255</code>. If no byte is available
68      * because the end of the stream has been reached, the value
69      * <code>-1</code> is returned.
70      *
71      * @return the next byte of data, or <code>-1</code> if the end of the
72      * stream is reached.
73      * @exception IOException if an I/O error occurs.
74      */

75     public int read() throws IOException JavaDoc {
76     if (position >= ra.size()) return -1;
77
78     ra.read(position, buf1, 0, 1);
79     position++;
80     return buf1[0] & 0xff;
81     }
82
83     /**
84      * Reads up to <code>len</code> bytes of data from this input stream
85      * into an array of bytes. This method blocks until some input is
86      * available. If the first argument is <code>null,</code> up to
87      * <code>len</code> bytes are read and discarded.
88      *
89      * @param b the buffer into which the data is read.
90      * @param off the start offset of the data.
91      * @param len the maximum number of bytes read.
92      * @return the total number of bytes read into the buffer, or
93      * <code>-1</code> if there is no more data because the end of
94      * the stream has been reached.
95      * @exception IOException if an I/O error occurs.
96      */

97     public int read(byte b[], int off, int len) throws IOException JavaDoc {
98     if (position >= ra.size()) return -1;
99     if (ra.size() - position < len) len = (int)(ra.size() - position);
100
101     if (b != null) ra.read(position, b, off, len);
102     position += len;
103     return len;
104     }
105
106     /**
107      * Skips over and discards <code>n</code> bytes of data from this
108      * input stream. The <code>skip</code> method may, for a variety of
109      * reasons, end up skipping over some smaller number of bytes,
110      * possibly <code>0</code>. The actual number of bytes skipped is
111      * returned.
112      * <p>
113      * The <code>skip</code> method of <code>InputStream</code> creates
114      * a byte array of length <code>n</code> and then reads into it until
115      * <code>n</code> bytes have been read or the end of the stream has
116      * been reached. Subclasses are encouraged to provide a more
117      * efficient implementation of this method.
118      *
119      * @param n the number of bytes to be skipped.
120      * @return the actual number of bytes skipped.
121      * @exception IOException if an I/O error occurs.
122      * @since JDK1.0
123      */

124     public long skip(long n) throws IOException JavaDoc {
125     return read(null, 0, (int)n);
126     }
127
128     /**
129      * Returns the number of bytes that can be read from this input
130      * stream without blocking. The available method of
131      * <code>InputStream</code> returns <code>0</code>. This method
132      * <B>should</B> be overridden by subclasses.
133      *
134      * @return the number of bytes that can be read from this input stream
135      * without blocking.
136      * @exception IOException if an I/O error occurs.
137      */

138     public int available() throws IOException JavaDoc {
139     return (int)(ra.size() - position);
140     }
141
142     /**
143      * Closes this input stream and releases any system resources
144      * associated with the stream.
145      *
146      * @exception IOException if an I/O error occurs.
147      */

148     public void close() throws IOException JavaDoc {
149     ra.close();
150     }
151
152 }
153
Popular Tags