001: /*
002: * @(#)GeneralBase.java 1.12 06/10/10
003: *
004: * Copyright 1990-2006 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:
028: package com.sun.cdc.io;
029:
030: import java.io.*;
031: import javax.microedition.io.*;
032:
033: /**
034: * Base class for objects that implement the DataInput and DataOutput interfaces.
035: * It also behaves like a DataOutputStream so it can be used for UniversalOutputStream
036: *
037: * @version 1.1 2/20/2000
038: */
039: abstract public class GeneralBase extends DataOutputStream implements
040: DataInput, DataOutput {
041:
042: public GeneralBase() {
043: super (null); // No DataOutputStream redirection wanted
044: }
045:
046: /**
047: * Writes the specified byte (the low eight bits of the argument
048: * <code>b</code>) to the underlying output stream. If no exception
049: * is thrown, the counter <code>written</code> is incremented by
050: * <code>1</code>.
051: * <p>
052: * Implements the <code>write</code> method of <code>OutputStream</code>.
053: *
054: * @param b the <code>byte</code> to be written.
055: * @exception IOException if an I/O error occurs.
056: * @see java.io.FilterOutputStream#out
057: */
058: public void write(int b) throws IOException {
059: throw new RuntimeException("No write()");
060: }
061:
062: /**
063: * Writes <code>len</code> bytes from the specified byte array
064: * starting at offset <code>off</code> to the underlying output stream.
065: * If no exception is thrown, the counter <code>written</code> is
066: * incremented by <code>len</code>.
067: *
068: * @param b the data.
069: * @param off the start offset in the data.
070: * @param len the number of bytes to write.
071: * @exception IOException if an I/O error occurs.
072: * @see java.io.FilterOutputStream#out
073: */
074: public void write(byte b[], int off, int len) throws IOException {
075: if (b == null) {
076: throw new NullPointerException();
077: } else if ((off < 0) || (off > b.length) || (len < 0)
078: || ((off + len) > b.length) || ((off + len) < 0)) {
079: throw new IndexOutOfBoundsException();
080: } else if (len == 0) {
081: return;
082: }
083: for (int i = 0; i < len; i++) {
084: write(b[off + i]);
085: }
086: }
087:
088: //
089: // DataOutput functions are inherited from DataOutputStream
090: //
091:
092: /**
093: * Flush the stream.
094: */
095: public void flush() throws IOException {
096: }
097:
098: /**
099: * Close the stream.
100: */
101: public void close() throws IOException {
102: }
103:
104: //
105: // DataInput functions
106: //
107:
108: /**
109: * Reads the next byte of data from the input stream. The value byte is
110: * returned as an <code>int</code> in the range <code>0</code> to
111: * <code>255</code>. If no byte is available because the end of the stream
112: * has been reached, the value <code>-1</code> is returned. This method
113: * blocks until input data is available, the end of the stream is detected,
114: * or an exception is thrown.
115: *
116: * <p> A subclass must provide an implementation of this method.
117: *
118: * @return the next byte of data, or <code>-1</code> if the end of the
119: * stream is reached.
120: * @exception IOException if an I/O error occurs.
121: */
122: public int read() throws IOException {
123: throw new RuntimeException("No read()");
124: }
125:
126: /**
127: * Skips over and discards <code>n</code> bytes of data from this input
128: * stream. The <code>skip</code> method may, for a variety of reasons, end
129: * up skipping over some smaller number of bytes, possibly <code>0</code>.
130: * This may result from any of a number of conditions; reaching end of file
131: * before <code>n</code> bytes have been skipped is only one possibility.
132: * The actual number of bytes skipped is returned. If <code>n</code> is
133: * negative, no bytes are skipped.
134: *
135: * <p> The <code>skip</code> method of <code>InputStream</code> creates a
136: * byte array and then repeatedly reads into it until <code>n</code> bytes
137: * have been read or the end of the stream has been reached. Subclasses are
138: * encouraged to provide a more efficient implementation of this method.
139: *
140: * @param n the number of bytes to be skipped.
141: * @return the actual number of bytes skipped.
142: * @exception IOException if an I/O error occurs.
143: */
144: public long skip(long n) throws IOException {
145: long m = n;
146: while (m > 0) {
147: if (read() > 0) {
148: break;
149: }
150: --m;
151: }
152: return n - m;
153: }
154:
155: /**
156: * See the general contract of the <code>readFully</code>
157: * method of <code>DataInput</code>.
158: * <p>
159: * Bytes
160: * for this operation are read from the contained
161: * input stream.
162: *
163: * @param b the buffer into which the data is read.
164: * @exception EOFException if this input stream reaches the end before
165: * reading all the bytes.
166: * @exception IOException if an I/O error occurs.
167: */
168: public void readFully(byte b[]) throws IOException {
169: readFully(b, 0, b.length);
170: }
171:
172: /**
173: * See the general contract of the <code>readFully</code>
174: * method of <code>DataInput</code>.
175: * <p>
176: * Bytes
177: * for this operation are read from the contained
178: * input stream.
179: *
180: * @param b the buffer into which the data is read.
181: * @param off the start offset of the data.
182: * @param len the number of bytes to read.
183: * @exception EOFException if this input stream reaches the end before
184: * reading all the bytes.
185: * @exception IOException if an I/O error occurs.
186: */
187: public void readFully(byte b[], int off, int len)
188: throws IOException {
189: if (len < 0)
190: throw new IndexOutOfBoundsException();
191:
192: int n = 0;
193: while (n < len) {
194: int ch = read();
195: if (ch < 0) {
196: throw new EOFException();
197: }
198: b[off + (n++)] = (byte) ch;
199: }
200: }
201:
202: /**
203: * See the general contract of the <code>skipBytes</code>
204: * method of <code>DataInput</code>.
205: * <p>
206: * Bytes
207: * for this operation are read from the contained
208: * input stream.
209: *
210: * @param n the number of bytes to be skipped.
211: * @return the actual number of bytes skipped.
212: * @exception IOException if an I/O error occurs.
213: */
214: public int skipBytes(int n) throws IOException {
215: int total = 0;
216: int cur = 0;
217:
218: while ((total < n) && ((cur = (int) skip(n - total)) > 0)) {
219: total += cur;
220: }
221:
222: return total;
223: }
224:
225: /**
226: * See the general contract of the <code>readBoolean</code>
227: * method of <code>DataInput</code>.
228: * <p>
229: * Bytes
230: * for this operation are read from the contained
231: * input stream.
232: *
233: * @return the <code>boolean</code> value read.
234: * @exception EOFException if this input stream has reached the end.
235: */
236: public boolean readBoolean() throws IOException {
237: int ch = read();
238: if (ch < 0)
239: throw new EOFException();
240: return (ch != 0);
241: }
242:
243: /**
244: * See the general contract of the <code>readByte</code>
245: * method of <code>DataInput</code>.
246: * <p>
247: * Bytes
248: * for this operation are read from the contained
249: * input stream.
250: *
251: * @return the next byte of this input stream as a signed 8-bit
252: * <code>byte</code>.
253: * @exception EOFException if this input stream has reached the end.
254: * @exception IOException if an I/O error occurs.
255: */
256: public byte readByte() throws IOException {
257: int ch = read();
258: if (ch < 0)
259: throw new EOFException();
260: return (byte) (ch);
261: }
262:
263: /**
264: * See the general contract of the <code>readUnsignedByte</code>
265: * method of <code>DataInput</code>.
266: * <p>
267: * Bytes
268: * for this operation are read from the contained
269: * input stream.
270: *
271: * @return the next byte of this input stream, interpreted as an
272: * unsigned 8-bit number.
273: * @exception EOFException if this input stream has reached the end.
274: * @exception IOException if an I/O error occurs.
275: */
276: public int readUnsignedByte() throws IOException {
277: int ch = read();
278: if (ch < 0)
279: throw new EOFException();
280: return ch;
281: }
282:
283: /**
284: * See the general contract of the <code>readShort</code>
285: * method of <code>DataInput</code>.
286: * <p>
287: * Bytes
288: * for this operation are read from the contained
289: * input stream.
290: *
291: * @return the next two bytes of this input stream, interpreted as a
292: * signed 16-bit number.
293: * @exception EOFException if this input stream reaches the end before
294: * reading two bytes.
295: * @exception IOException if an I/O error occurs.
296: */
297: public short readShort() throws IOException {
298: int ch1 = read();
299: int ch2 = read();
300: if ((ch1 | ch2) < 0)
301: throw new EOFException();
302: return (short) ((ch1 << 8) + (ch2 << 0));
303: }
304:
305: /**
306: * See the general contract of the <code>readUnsignedShort</code>
307: * method of <code>DataInput</code>.
308: * <p>
309: * Bytes
310: * for this operation are read from the contained
311: * input stream.
312: *
313: * @return the next two bytes of this input stream, interpreted as an
314: * unsigned 16-bit integer.
315: * @exception EOFException if this input stream reaches the end before
316: * reading two bytes.
317: * @exception IOException if an I/O error occurs.
318: */
319: public int readUnsignedShort() throws IOException {
320: int ch1 = read();
321: int ch2 = read();
322: if ((ch1 | ch2) < 0)
323: throw new EOFException();
324: return (ch1 << 8) + (ch2 << 0);
325: }
326:
327: /**
328: * See the general contract of the <code>readChar</code>
329: * method of <code>DataInput</code>.
330: * <p>
331: * Bytes
332: * for this operation are read from the contained
333: * input stream.
334: *
335: * @return the next two bytes of this input stream as a Unicode
336: * character.
337: * @exception EOFException if this input stream reaches the end before
338: * reading two bytes.
339: * @exception IOException if an I/O error occurs.
340: */
341: public char readChar() throws IOException {
342: int ch1 = read();
343: int ch2 = read();
344: if ((ch1 | ch2) < 0)
345: throw new EOFException();
346: return (char) ((ch1 << 8) + (ch2 << 0));
347: }
348:
349: /**
350: * See the general contract of the <code>readInt</code>
351: * method of <code>DataInput</code>.
352: * <p>
353: * Bytes
354: * for this operation are read from the contained
355: * input stream.
356: *
357: * @return the next four bytes of this input stream, interpreted as an
358: * <code>int</code>.
359: * @exception EOFException if this input stream reaches the end before
360: * reading four bytes.
361: * @exception IOException if an I/O error occurs.
362: */
363: public int readInt() throws IOException {
364: int ch1 = read();
365: int ch2 = read();
366: int ch3 = read();
367: int ch4 = read();
368: if ((ch1 | ch2 | ch3 | ch4) < 0)
369: throw new EOFException();
370: return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
371: }
372:
373: /**
374: * See the general contract of the <code>readLong</code>
375: * method of <code>DataInput</code>.
376: * <p>
377: * Bytes
378: * for this operation are read from the contained
379: * input stream.
380: *
381: * @return the next eight bytes of this input stream, interpreted as a
382: * <code>long</code>.
383: * @exception EOFException if this input stream reaches the end before
384: * reading eight bytes.
385: * @exception IOException if an I/O error occurs.
386: */
387: public long readLong() throws IOException {
388: return ((long) (readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
389: }
390:
391: /**
392: * See the general contract of the <code>readUTF</code>
393: * method of <code>DataInput</code>.
394: * <p>
395: * Bytes
396: * for this operation are read from the contained
397: * input stream.
398: *
399: * @return a Unicode string.
400: * @exception EOFException if this input stream reaches the end before
401: * reading all the bytes.
402: * @exception IOException if an I/O error occurs.
403: * @see java.io.DataInputStream#readUTF(java.io.DataInput)
404: */
405: public String readUTF() throws IOException {
406: return DataInputStream.readUTF(this );
407: }
408:
409: /**
410: * Unsupported function
411: */
412: public float readFloat() throws IOException {
413: throw new RuntimeException("Function not supported");
414: }
415:
416: /**
417: * Unsupported function
418: */
419: public double readDouble() throws IOException {
420: throw new RuntimeException("Function not supported");
421: }
422:
423: /**
424: * Unsupported function
425: */
426: public String readLine() throws IOException {
427: throw new RuntimeException("Function not supported");
428: }
429:
430: }
|