KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > CellBufferInputStream


1 /**
2  * com.mckoi.database.CellBufferInputStream 12 Sep 1998
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 //import java.io.ByteArrayInputStream;
28
import java.io.DataInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.DataInput JavaDoc;
32
33 /**
34  * This is a reusable cell stream object that is extended from the
35  * ByteArrayInputStream class, which provides methods for reusing the object
36  * on a different byte[] arrays. It is used as an efficient way of reading
37  * cell information from a binary fixed length cell type file.
38  * <p>
39  * It would usually be wrapped in a DataInputStream object.
40  * <p>
41  * @author Tobias Downer
42  */

43
44 final class CellBufferInputStream extends InputStream JavaDoc implements CellInput {
45
46 // /**
47
// * A wrapped DataInputStream over this stream.
48
// */
49
// private DataInputStream wrapped_data;
50

51   private byte buf[];
52   private int pos;
53   private int mark = 0;
54   private int count;
55
56   /**
57    * The Constructor.
58    */

59   CellBufferInputStream() {
60     this.buf = null;
61     this.pos = 0;
62     this.count = 0;
63   }
64
65   /**
66    * Sets up the stream to the start of the underlying array.
67    */

68   void setArray(byte[] new_buffer) {
69     buf = new_buffer;
70     pos = 0;
71     mark = 0;
72     count = new_buffer.length;
73   }
74
75   /**
76    * Sets up the stream to the underlying array with the given variables.
77    */

78   void setArray(byte[] new_buffer, int offset, int length) {
79     buf = new_buffer;
80     pos = offset;
81     mark = 0;
82     count = Math.min(new_buffer.length, length + offset);
83   }
84
85   /**
86    * Sped up methods.
87    */

88   public int read() {
89     return buf[pos++] & 0x0FF;
90 // return (pos < count) ? (buf[pos++] & 0xff) : -1;
91
}
92
93   public int read(byte b[], int off, int len) {
94 // if (b == null) {
95
// throw new NullPointerException();
96
// } else if ((off < 0) || (off > b.length) || (len < 0) ||
97
// ((off + len) > b.length) || ((off + len) < 0)) {
98
// throw new IndexOutOfBoundsException();
99
// }
100
if (pos >= count) {
101       return -1;
102     }
103     if (pos + len > count) {
104       len = count - pos;
105     }
106     if (len <= 0) {
107       return 0;
108     }
109     System.arraycopy(buf, pos, b, off, len);
110     pos += len;
111     return len;
112   }
113
114   public long skip(long n) {
115     if (pos + n > count) {
116       n = count - pos;
117     }
118     if (n < 0) {
119       return 0;
120     }
121     pos += n;
122     return n;
123   }
124
125   public int available() {
126     return count - pos;
127   }
128
129   public void mark(int readAheadLimit) {
130     mark = pos;
131   }
132
133   public void reset() {
134     pos = mark;
135   }
136
137   public void close() throws IOException JavaDoc {
138   }
139
140
141   // ---------- Implemented from DataInput ----------
142

143   public void readFully(byte[] b) throws IOException JavaDoc {
144     read(b, 0, b.length);
145   }
146
147   public void readFully(byte b[], int off, int len) throws IOException JavaDoc {
148     read(b, off, len);
149   }
150
151   public int skipBytes(int n) throws IOException JavaDoc {
152     return (int) skip(n);
153   }
154
155   public boolean readBoolean() throws IOException JavaDoc {
156     return (read() != 0);
157   }
158
159   public byte readByte() throws IOException JavaDoc {
160     return (byte) read();
161   }
162
163   public int readUnsignedByte() throws IOException JavaDoc {
164     return read();
165   }
166
167   public short readShort() throws IOException JavaDoc {
168     int ch1 = read();
169     int ch2 = read();
170     return (short)((ch1 << 8) + (ch2 << 0));
171   }
172
173   public int readUnsignedShort() throws IOException JavaDoc {
174     int ch1 = read();
175     int ch2 = read();
176     return (ch1 << 8) + (ch2 << 0);
177   }
178
179   public char readChar() throws IOException JavaDoc {
180     int ch1 = read();
181     int ch2 = read();
182     return (char)((ch1 << 8) + (ch2 << 0));
183   }
184
185   private char[] char_buffer = new char[8192];
186
187   public String JavaDoc readChars(int length) throws IOException JavaDoc {
188     if (length <= char_buffer.length) {
189       for (int i = 0; i < length; ++i) {
190         char_buffer[i] = (char) (((buf[pos++] & 0x0FF) << 8) +
191                                  ((buf[pos++] & 0x0FF) << 0));
192       }
193       return new String JavaDoc(char_buffer, 0, length);
194     }
195     else {
196       StringBuffer JavaDoc chrs = new StringBuffer JavaDoc(length);
197       for (int i = length; i > 0; --i) {
198         chrs.append((char) (((buf[pos++] & 0x0FF) << 8) +
199                             ((buf[pos++] & 0x0FF) << 0)));
200       }
201       return new String JavaDoc(chrs);
202     }
203   }
204
205   public int readInt() throws IOException JavaDoc {
206     return ((buf[pos++] & 0x0FF) << 24) +
207            ((buf[pos++] & 0x0FF) << 16) +
208            ((buf[pos++] & 0x0FF) << 8) +
209            ((buf[pos++] & 0x0FF) << 0);
210   }
211
212   public long readLong() throws IOException JavaDoc {
213     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
214   }
215
216   public float readFloat() throws IOException JavaDoc {
217     return Float.intBitsToFloat(readInt());
218   }
219
220   public double readDouble() throws IOException JavaDoc {
221     return Double.longBitsToDouble(readLong());
222   }
223
224   public String JavaDoc readLine() throws IOException JavaDoc {
225     throw new Error JavaDoc("Not implemented.");
226   }
227
228   public String JavaDoc readUTF() throws IOException JavaDoc {
229     throw new Error JavaDoc("Not implemented.");
230   }
231
232
233
234
235 // /**
236
// * Returns a wrapped DataInputStream for this stream. This is a
237
// * convenience, but will improve on efficiency of a
238
// * 'new DataInputStream(...)' type allocation.
239
// */
240
// DataInputStream getDataInputStream() {
241
// if (wrapped_data != null) {
242
// return wrapped_data;
243
// }
244
// return wrapped_data = new DataInputStream(this);
245
// }
246

247 }
248
Popular Tags