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: /**
030: * A data input stream lets an application read primitive Java data
031: * types from an underlying input stream in a machine-independent
032: * way. An application uses a data output stream to write data that
033: * can later be read by a data input stream.
034: *
035: * @version 12/17/01 (CLDC 1.1)
036: * @see java.io.DataOutputStream
037: * @since JDK1.0, CLDC 1.0
038: */
039:
040: public class DataInputStream extends InputStream implements DataInput {
041:
042: /**
043: * The input stream.
044: */
045: protected InputStream in;
046:
047: /**
048: * Creates a <code>DataInputStream</code>
049: * and saves its argument, the input stream
050: * <code>in</code>, for later use.
051: *
052: * @param in the input stream.
053: */
054: public DataInputStream(InputStream in) {
055: this .in = in;
056: }
057:
058: /**
059: * Reads the next byte of data from this input stream. The value
060: * byte is returned as an <code>int</code> in the range
061: * <code>0</code> to <code>255</code>. If no byte is available
062: * because the end of the stream has been reached, the value
063: * <code>-1</code> is returned. This method blocks until input data
064: * is available, the end of the stream is detected, or an exception
065: * is thrown.
066: * <p>
067: * This method
068: * simply performs <code>in.read()</code> and returns the result.
069: *
070: * @return the next byte of data, or <code>-1</code> if the end of the
071: * stream is reached.
072: * @exception IOException if an I/O error occurs.
073: */
074: public int read() throws IOException {
075: return in.read();
076: }
077:
078: /**
079: * See the general contract of the <code>read</code>
080: * method of <code>DataInput</code>.
081: * <p>
082: * Bytes for this operation are read from the contained
083: * input stream.
084: *
085: * @param b the buffer into which the data is read.
086: * @return the total number of bytes read into the buffer, or
087: * <code>-1</code> if there is no more data because the end
088: * of the stream has been reached.
089: * @exception IOException if an I/O error occurs.
090: * @see java.io.InputStream#read(byte[], int, int)
091: */
092: public final int read(byte b[]) throws IOException {
093: return in.read(b, 0, b.length);
094: }
095:
096: /**
097: * Reads up to <code>len</code> bytes of data from this input stream
098: * into an array of bytes. This method blocks until some input is
099: * available.
100: * <p>
101: * This method simply performs <code>in.read(b, off, len)</code>
102: * and returns the result.
103: *
104: * @param b the buffer into which the data is read.
105: * @param off the start offset of the data.
106: * @param len the maximum number of bytes read.
107: * @return the total number of bytes read into the buffer, or
108: * <code>-1</code> if there is no more data because the end of
109: * the stream has been reached.
110: * @exception IOException if an I/O error occurs.
111: */
112: public final int read(byte b[], int off, int len)
113: throws IOException {
114: return in.read(b, off, len);
115: }
116:
117: /**
118: * See the general contract of the <code>readFully</code>
119: * method of <code>DataInput</code>.
120: * <p>
121: * Bytes for this operation are read from the contained
122: * input stream.
123: *
124: * @param b the buffer into which the data is read.
125: * @exception EOFException if this input stream reaches the end before
126: * reading all the bytes.
127: * @exception IOException if an I/O error occurs.
128: */
129: public final void readFully(byte b[]) throws IOException {
130: readFully(b, 0, b.length);
131: }
132:
133: /**
134: * See the general contract of the <code>readFully</code>
135: * method of <code>DataInput</code>.
136: * <p>
137: * Bytes for this operation are read from the contained
138: * input stream.
139: *
140: * @param b the buffer into which the data is read.
141: * @param off the start offset of the data.
142: * @param len the number of bytes to read.
143: * @exception EOFException if this input stream reaches the end before
144: * reading all the bytes.
145: * @exception IOException if an I/O error occurs.
146: */
147: public final void readFully(byte b[], int off, int len)
148: throws IOException {
149: if (len < 0) {
150: throw new IndexOutOfBoundsException();
151: }
152: int n = 0;
153: while (n < len) {
154: int count = read(b, off + n, len - n);
155: if (count < 0) {
156: throw new EOFException();
157: }
158: n += count;
159: }
160: }
161:
162: /**
163: * See the general contract of the <code>skipBytes</code>
164: * method of <code>DataInput</code>.
165: * <p>
166: * Bytes for this operation are read from the contained
167: * input stream.
168: *
169: * @param n the number of bytes to be skipped.
170: * @return the actual number of bytes skipped.
171: * @exception IOException if an I/O error occurs.
172: */
173: public final int skipBytes(int n) throws IOException {
174: int total = 0;
175: int cur = 0;
176:
177: while ((total < n) && ((cur = (int) skip(n - total)) > 0)) {
178: total += cur;
179: }
180: return total;
181: }
182:
183: /**
184: * See the general contract of the <code>readBoolean</code>
185: * method of <code>DataInput</code>.
186: * <p>
187: * Bytes for this operation are read from the contained
188: * input stream.
189: *
190: * @return the <code>boolean</code> value read.
191: * @exception EOFException if this input stream has reached the end.
192: * @exception IOException if an I/O error occurs.
193: */
194: public final boolean readBoolean() throws IOException {
195: int ch = read();
196: if (ch < 0) {
197: throw new EOFException();
198: }
199: return (ch != 0);
200: }
201:
202: /**
203: * See the general contract of the <code>readByte</code>
204: * method of <code>DataInput</code>.
205: * <p>
206: * Bytes for this operation are read from the contained
207: * input stream.
208: *
209: * @return the next byte of this input stream as a signed 8-bit
210: * <code>byte</code>.
211: * @exception EOFException if this input stream has reached the end.
212: * @exception IOException if an I/O error occurs.
213: */
214: public final byte readByte() throws IOException {
215: int ch = read();
216: if (ch < 0) {
217: throw new EOFException();
218: }
219: return (byte) (ch);
220: }
221:
222: /**
223: * See the general contract of the <code>readUnsignedByte</code>
224: * method of <code>DataInput</code>.
225: * <p>
226: * Bytes for this operation are read from the contained
227: * input stream.
228: *
229: * @return the next byte of this input stream, interpreted as an
230: * unsigned 8-bit number.
231: * @exception EOFException if this input stream has reached the end.
232: * @exception IOException if an I/O error occurs.
233: */
234: public final int readUnsignedByte() throws IOException {
235: int ch = read();
236: if (ch < 0) {
237: throw new EOFException();
238: }
239: return ch;
240: }
241:
242: /**
243: * See the general contract of the <code>readShort</code>
244: * method of <code>DataInput</code>.
245: * <p>
246: * Bytes for this operation are read from the contained
247: * input stream.
248: *
249: * @return the next two bytes of this input stream, interpreted as a
250: * signed 16-bit number.
251: * @exception EOFException if this input stream reaches the end before
252: * reading two bytes.
253: * @exception IOException if an I/O error occurs.
254: */
255: public final short readShort() throws IOException {
256: return (short) readUnsignedShort();
257: }
258:
259: /**
260: * See the general contract of the <code>readUnsignedShort</code>
261: * method of <code>DataInput</code>.
262: * <p>
263: * Bytes for this operation are read from the contained
264: * input stream.
265: *
266: * @return the next two bytes of this input stream, interpreted as an
267: * unsigned 16-bit integer.
268: * @exception EOFException if this input stream reaches the end before
269: * reading two bytes.
270: * @exception IOException if an I/O error occurs.
271: */
272: public final int readUnsignedShort() throws IOException {
273: int ch1 = read();
274: int ch2 = read();
275: if ((ch1 | ch2) < 0) {
276: throw new EOFException();
277: }
278: return (ch1 << 8) + (ch2 << 0);
279: }
280:
281: /**
282: * See the general contract of the <code>readChar</code>
283: * method of <code>DataInput</code>.
284: * <p>
285: * Bytes for this operation are read from the contained
286: * input stream.
287: *
288: * @return the next two bytes of this input stream as a Unicode
289: * character.
290: * @exception EOFException if this input stream reaches the end before
291: * reading two bytes.
292: * @exception IOException if an I/O error occurs.
293: */
294: public final char readChar() throws IOException {
295: return (char) readUnsignedShort();
296: }
297:
298: /**
299: * See the general contract of the <code>readInt</code>
300: * method of <code>DataInput</code>.
301: * <p>
302: * Bytes for this operation are read from the contained
303: * input stream.
304: *
305: * @return the next four bytes of this input stream, interpreted as an
306: * <code>int</code>.
307: * @exception EOFException if this input stream reaches the end before
308: * reading four bytes.
309: * @exception IOException if an I/O error occurs.
310: */
311: public final int readInt() throws IOException {
312: int ch1 = read();
313: int ch2 = read();
314: int ch3 = read();
315: int ch4 = read();
316: if ((ch1 | ch2 | ch3 | ch4) < 0) {
317: throw new EOFException();
318: }
319: return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
320: }
321:
322: /**
323: * See the general contract of the <code>readLong</code>
324: * method of <code>DataInput</code>.
325: * <p>
326: * Bytes for this operation are read from the contained
327: * input stream.
328: *
329: * @return the next eight bytes of this input stream, interpreted as a
330: * <code>long</code>.
331: * @exception EOFException if this input stream reaches the end before
332: * reading eight bytes.
333: * @exception IOException if an I/O error occurs.
334: */
335: public final long readLong() throws IOException {
336: return ((long) (readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
337: }
338:
339: /**
340: * See the general contract of the <code>readFloat</code>
341: * method of <code>DataInput</code>.
342: * <p>
343: * Bytes for this operation are read from the contained
344: * input stream.
345: *
346: * @return the next four bytes of this input stream, interpreted as a
347: * <code>float</code>.
348: * @exception EOFException if this input stream reaches the end before
349: * reading four bytes.
350: * @exception IOException if an I/O error occurs.
351: * @see java.io.DataInputStream#readInt()
352: * @see java.lang.Float#intBitsToFloat(int)
353: * @since CLDC 1.1
354: */
355: public final float readFloat() throws IOException {
356: return Float.intBitsToFloat(readInt());
357: }
358:
359: /**
360: * See the general contract of the <code>readDouble</code>
361: * method of <code>DataInput</code>.
362: * <p>
363: * Bytes for this operation are read from the contained
364: * input stream.
365: *
366: * @return the next eight bytes of this input stream, interpreted as a
367: * <code>double</code>.
368: * @exception EOFException if this input stream reaches the end before
369: * reading eight bytes.
370: * @exception IOException if an I/O error occurs.
371: * @see java.io.DataInputStream#readLong()
372: * @see java.lang.Double#longBitsToDouble(long)
373: * @since CLDC 1.1
374: */
375: public final double readDouble() throws IOException {
376: return Double.longBitsToDouble(readLong());
377: }
378:
379: /**
380: * See the general contract of the <code>readUTF</code>
381: * method of <code>DataInput</code>.
382: * <p>
383: * Bytes for this operation are read from the contained
384: * input stream.
385: *
386: * @return a Unicode string.
387: * @exception EOFException if this input stream reaches the end before
388: * reading all the bytes.
389: * @exception IOException if an I/O error occurs.
390: * @see java.io.DataInputStream#readUTF(java.io.DataInput)
391: */
392: public final String readUTF() throws IOException {
393: return readUTF(this );
394: }
395:
396: /**
397: * Reads from the
398: * stream <code>in</code> a representation
399: * of a Unicode character string encoded in
400: * Java modified UTF-8 format; this string
401: * of characters is then returned as a <code>String</code>.
402: * The details of the modified UTF-8 representation
403: * are exactly the same as for the <code>readUTF</code>
404: * method of <code>DataInput</code>.
405: *
406: * @param in a data input stream.
407: * @return a Unicode string.
408: * @exception EOFException if the input stream reaches the end
409: * before all the bytes.
410: * @exception IOException if an I/O error occurs.
411: * @exception UTFDataFormatException if the bytes do not represent a
412: * valid UTF-8 encoding of a Unicode string.
413: * @see java.io.DataInputStream#readUnsignedShort()
414: */
415: public final static String readUTF(DataInput in) throws IOException {
416: int utflen = in.readUnsignedShort();
417: char str[] = new char[utflen];
418: byte bytearr[] = new byte[utflen];
419: int c, char2, char3;
420: int count = 0;
421: int strlen = 0;
422:
423: in.readFully(bytearr, 0, utflen);
424:
425: while (count < utflen) {
426: c = (int) bytearr[count] & 0xff;
427: switch (c >> 4) {
428: case 0:
429: case 1:
430: case 2:
431: case 3:
432: case 4:
433: case 5:
434: case 6:
435: case 7:
436: /* 0xxxxxxx*/
437: count++;
438: str[strlen++] = (char) c;
439: break;
440: case 12:
441: case 13:
442: /* 110x xxxx 10xx xxxx*/
443: count += 2;
444: if (count > utflen)
445: throw new UTFDataFormatException();
446: char2 = (int) bytearr[count - 1];
447: if ((char2 & 0xC0) != 0x80)
448: throw new UTFDataFormatException();
449: str[strlen++] = (char) (((c & 0x1F) << 6) | (char2 & 0x3F));
450: break;
451: case 14:
452: /* 1110 xxxx 10xx xxxx 10xx xxxx */
453: count += 3;
454: if (count > utflen)
455: throw new UTFDataFormatException();
456: char2 = (int) bytearr[count - 2];
457: char3 = (int) bytearr[count - 1];
458: if (((char2 & 0xC0) != 0x80)
459: || ((char3 & 0xC0) != 0x80))
460: throw new UTFDataFormatException();
461: str[strlen++] = (char) (((c & 0x0F) << 12)
462: | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
463: break;
464: default:
465: /* 10xx xxxx, 1111 xxxx */
466: throw new UTFDataFormatException();
467: }
468: }
469: // The number of chars produced may be less than utflen
470: return new String(str, 0, strlen);
471: }
472:
473: /**
474: * Skips over and discards <code>n</code> bytes of data from the
475: * input stream. The <code>skip</code> method may, for a variety of
476: * reasons, end up skipping over some smaller number of bytes,
477: * possibly <code>0</code>. The actual number of bytes skipped is
478: * returned.
479: * <p>
480: * This method
481: * simply performs <code>in.skip(n)</code>.
482: *
483: * @param n the number of bytes to be skipped.
484: * @return the actual number of bytes skipped.
485: * @exception IOException if an I/O error occurs.
486: */
487: public long skip(long n) throws IOException {
488: return in.skip(n);
489: }
490:
491: /**
492: * Returns the number of bytes that can be read from this input
493: * stream without blocking.
494: * <p>
495: * This method simply performs <code>in.available()</code> and
496: * returns the result.
497: *
498: * @return the number of bytes that can be read from the input stream
499: * without blocking.
500: * @exception IOException if an I/O error occurs.
501: */
502: public int available() throws IOException {
503: return in.available();
504: }
505:
506: /**
507: * Closes this input stream and releases any system resources
508: * associated with the stream.
509: * This
510: * method simply performs <code>in.close()</code>.
511: *
512: * @exception IOException if an I/O error occurs.
513: */
514: public void close() throws IOException {
515: in.close();
516: }
517:
518: /**
519: * Marks the current position in this input stream. A subsequent
520: * call to the <code>reset</code> method repositions this stream at
521: * the last marked position so that subsequent reads re-read the same bytes.
522: * <p>
523: * The <code>readlimit</code> argument tells this input stream to
524: * allow that many bytes to be read before the mark position gets
525: * invalidated.
526: * <p>
527: * This method simply performs <code>in.mark(readlimit)</code>.
528: *
529: * @param readlimit the maximum limit of bytes that can be read before
530: * the mark position becomes invalid.
531: */
532: public synchronized void mark(int readlimit) {
533: in.mark(readlimit);
534: }
535:
536: /**
537: * Repositions this stream to the position at the time the
538: * <code>mark</code> method was last called on this input stream.
539: * <p>
540: * This method
541: * simply performs <code>in.reset()</code>.
542: * <p>
543: * Stream marks are intended to be used in
544: * situations where you need to read ahead a little to see what's in
545: * the stream. Often this is most easily done by invoking some
546: * general parser. If the stream is of the type handled by the
547: * parse, it just chugs along happily. If the stream is not of
548: * that type, the parser should toss an exception when it fails.
549: * If this happens within readlimit bytes, it allows the outer
550: * code to reset the stream and try another parser.
551: *
552: * @exception IOException if the stream has not been marked or if the
553: * mark has been invalidated.
554: */
555: public synchronized void reset() throws IOException {
556: in.reset();
557: }
558:
559: /**
560: * Tests if this input stream supports the <code>mark</code>
561: * and <code>reset</code> methods.
562: * This method
563: * simply performs <code>in.markSupported()</code>.
564: *
565: * @return <code>true</code> if this stream type supports the
566: * <code>mark</code> and <code>reset</code> method;
567: * <code>false</code> otherwise.
568: */
569: public boolean markSupported() {
570: return in.markSupported();
571: }
572: }
|