1 //******************************************************************************
2 //
3 // File: DataInputStream.java
4 // Package: edu.rit.io
5 // Unit: Class edu.rit.io.DataInputStream
6 //
7 // This Java source file is copyright (C) 2009 by Alan Kaminsky. All rights
8 // reserved. For further information, contact the author, Alan Kaminsky, at
9 // ark@cs.rit.edu.
10 //
11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12 // software; you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation; either
14 // version 3 of the License, or (at your option) any later version.
15 //
16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 //
20 // Linking this library statically or dynamically with other modules is making a
21 // combined work based on this library. Thus, the terms and conditions of the GNU
22 // General Public License cover the whole combination.
23 //
24 // As a special exception, the copyright holders of this library give you
25 // permission to link this library with independent modules to produce an
26 // executable, regardless of the license terms of these independent modules, and
27 // to copy and distribute the resulting executable under terms of your choice,
28 // provided that you also meet, for each linked independent module, the terms
29 // and conditions of the license of that module. An independent module is a module
30 // which is not derived from or based on this library. If you modify this library,
31 // you may extend this exception to your version of the library, but you are not
32 // obligated to do so. If you do not wish to do so, delete this exception
33 // statement from your version.
34 //
35 // A copy of the GNU General Public License is provided in the file gpl.txt. You
36 // may also obtain a copy of the GNU General Public License on the World Wide
37 // Web at http://www.gnu.org/licenses/gpl.html.
38 //
39 //******************************************************************************
40 package edu.rit.io;
41
42 import java.io.EOFException;
43 import java.io.FilterInputStream;
44 import java.io.IOException;
45 import java.io.InputStream;
46
47 /**
48 * Class DataInputStream provides an input stream that reads primitive data
49 * types and strings in binary form. It behaves similarly to class
50 * java.io.DataInputStream, except the methods for reading types byte, short,
51 * char, int, long, and String are implemented differently. These methods read
52 * an integer value using a variable number of bytes, as described below. This
53 * can save space in the file if small integer values are written more
54 * frequently than large integer values. The byte stream being read must have
55 * been written using class {@linkplain DataOutputStream}.
56 * <P>
57 * Note that class DataInputStream does <I>not</I> implement interface
58 * java.io.DataInput, because the methods do not obey the contract specified in
59 * that interface.
60 *
61 * @author Alan Kaminsky
62 * @version 19-Dec-2009
63 */
64 public class DataInputStream
65 extends FilterInputStream {
66
67 // Exported constructors.
68 /**
69 * Construct a new data input stream.
70 *
71 * @param in Underlying input stream.
72 */
73 public DataInputStream(InputStream in) {
74 super(in);
75 }
76
77 // Exported operations.
78 /**
79 * Read a Boolean value from this data input stream. One byte is read; if it
80 * is 0, false is returned; otherwise true is returned.
81 *
82 * @return Boolean value.
83 * @exception EOFException Thrown if the end of the stream is encountered.
84 * @exception IOException Thrown if an I/O error occurred.
85 * @throws java.io.IOException if any.
86 */
87 public boolean readBoolean()
88 throws IOException {
89 int v = in.read();
90 if (v == -1) {
91 throw new EOFException();
92 }
93 return v != 0;
94 }
95
96 /**
97 * Read a byte value from this data input stream. An int is read using
98 * <code>readInt()</code>, the int is converted to a byte, and the byte is
99 * returned.
100 *
101 * @return Byte value.
102 * @exception EOFException Thrown if the end of the stream is encountered.
103 * @exception IOException Thrown if an I/O error occurred.
104 * @throws java.io.IOException if any.
105 */
106 public byte readByte()
107 throws IOException {
108 return (byte) readInt();
109 }
110
111 /**
112 * Read a short value from this data input stream. An int is read using
113 * <code>readInt()</code>, the int is converted to a short, and the short is
114 * returned.
115 *
116 * @return Short value.
117 * @exception EOFException Thrown if the end of the stream is encountered.
118 * @exception IOException Thrown if an I/O error occurred.
119 * @throws java.io.IOException if any.
120 */
121 public short readShort()
122 throws IOException {
123 return (short) readInt();
124 }
125
126 /**
127 * Read a character value from this data input stream. An int is read using
128 * <code>readInt()</code>, the int is converted to a char, and the char is
129 * returned.
130 *
131 * @return Character value.
132 * @exception EOFException Thrown if the end of the stream is encountered.
133 * @exception IOException Thrown if an I/O error occurred.
134 * @throws java.io.IOException if any.
135 */
136 public char readChar()
137 throws IOException {
138 return (char) readInt();
139 }
140
141 /**
142 * Read an integer value from this data input stream. From one to five bytes
143 * are read, in big-endian order, as follows:
144 * <UL>
145 *
146 * <LI>
147 * If the first byte's most significant bit is 0, then the least significant
148 * 7 bits are sign-extended to an int and returned.
149 *
150 * <LI>
151 * Else if the first byte's two most significant bits are 10, then one more
152 * byte is read, and the least significant 14 bits are sign-extended to an
153 * int and returned.
154 *
155 * <LI>
156 * Else if the first byte's three most significant bits are 110, then two
157 * more bytes are read, and the least significant 21 bits are sign-extended
158 * to an int and returned.
159 *
160 * <LI>
161 * Else if the first byte's four most significant bits are 1110, then three
162 * more bytes are read, and the least significant 28 bits are sign-extended
163 * to an int and returned.
164 *
165 * <LI>
166 * Else four more bytes are read, and the least significant 32 bits are
167 * returned.
168 * </UL>
169 *
170 * @return Integer value.
171 * @exception EOFException Thrown if the end of the stream is encountered.
172 * @exception IOException Thrown if an I/O error occurred.
173 * @throws java.io.IOException if any.
174 */
175 public int readInt()
176 throws IOException {
177 int v;
178 int b = in.read();
179 if (b == -1) {
180 throw new EOFException();
181 }
182 v = b;
183 if ((b & 0x80) == 0x00) {
184 v <<= 25;
185 v >>= 25;
186 return v;
187 } else if ((b & 0xC0) == 0x80) {
188 b = in.read();
189 if (b == -1) {
190 throw new EOFException();
191 }
192 v = (v << 8) | b;
193 v <<= 18;
194 v >>= 18;
195 return v;
196 } else if ((b & 0xE0) == 0xC0) {
197 b = in.read();
198 if (b == -1) {
199 throw new EOFException();
200 }
201 v = (v << 8) | b;
202 b = in.read();
203 if (b == -1) {
204 throw new EOFException();
205 }
206 v = (v << 8) | b;
207 v <<= 11;
208 v >>= 11;
209 return v;
210 } else if ((b & 0xF0) == 0xE0) {
211 b = in.read();
212 if (b == -1) {
213 throw new EOFException();
214 }
215 v = (v << 8) | b;
216 b = in.read();
217 if (b == -1) {
218 throw new EOFException();
219 }
220 v = (v << 8) | b;
221 b = in.read();
222 if (b == -1) {
223 throw new EOFException();
224 }
225 v = (v << 8) | b;
226 v <<= 4;
227 v >>= 4;
228 return v;
229 } else {
230 b = in.read();
231 if (b == -1) {
232 throw new EOFException();
233 }
234 v = b;
235 b = in.read();
236 if (b == -1) {
237 throw new EOFException();
238 }
239 v = (v << 8) | b;
240 b = in.read();
241 if (b == -1) {
242 throw new EOFException();
243 }
244 v = (v << 8) | b;
245 b = in.read();
246 if (b == -1) {
247 throw new EOFException();
248 }
249 v = (v << 8) | b;
250 return v;
251 }
252 }
253
254 /**
255 * Read an unsigned byte value from this data input stream. An int is read
256 * using <code>readUnsignedInt()</code>, the int is converted to a byte, and the
257 * byte is returned.
258 *
259 * @return Byte value.
260 * @exception EOFException Thrown if the end of the stream is encountered.
261 * @exception IOException Thrown if an I/O error occurred.
262 * @throws java.io.IOException if any.
263 */
264 public byte readUnsignedByte()
265 throws IOException {
266 return (byte) readUnsignedInt();
267 }
268
269 /**
270 * Read an unsigned short value from this data input stream. An int is read
271 * using <code>readUnsignedInt()</code>, the int is converted to a short, and
272 * the short is returned.
273 *
274 * @return Short value.
275 * @exception EOFException Thrown if the end of the stream is encountered.
276 * @exception IOException Thrown if an I/O error occurred.
277 * @throws java.io.IOException if any.
278 */
279 public short readUnsignedShort()
280 throws IOException {
281 return (short) readUnsignedInt();
282 }
283
284 /**
285 * Read an unsigned character value from this data input stream. An int is
286 * read using <code>readUnsignedInt()</code>, the int is converted to a char,
287 * and the char is returned.
288 *
289 * @return Character value.
290 * @exception EOFException Thrown if the end of the stream is encountered.
291 * @exception IOException Thrown if an I/O error occurred.
292 * @throws java.io.IOException if any.
293 */
294 public char readUnsignedChar()
295 throws IOException {
296 return (char) readUnsignedInt();
297 }
298
299 /**
300 * Read an unsigned integer value from this data input stream. From one to
301 * five bytes are read, in big-endian order, as follows:
302 * <UL>
303 *
304 * <LI>
305 * If the first byte's most significant bit is 0, then the least significant
306 * 7 bits are zero-extended to an int and returned.
307 *
308 * <LI>
309 * Else if the first byte's two most significant bits are 10, then one more
310 * byte is read, and the least significant 14 bits are zero-extended to an
311 * int and returned.
312 *
313 * <LI>
314 * Else if the first byte's three most significant bits are 110, then two
315 * more bytes are read, and the least significant 21 bits are zero-extended
316 * to an int and returned.
317 *
318 * <LI>
319 * Else if the first byte's four most significant bits are 1110, then three
320 * more bytes are read, and the least significant 28 bits are zero-extended
321 * to an int and returned.
322 *
323 * <LI>
324 * Else four more bytes are read, and the least significant 32 bits are
325 * returned.
326 * </UL>
327 *
328 * @return Integer value.
329 * @exception EOFException Thrown if the end of the stream is encountered.
330 * @exception IOException Thrown if an I/O error occurred.
331 * @throws java.io.IOException if any.
332 */
333 public int readUnsignedInt()
334 throws IOException {
335 int v;
336 int b = in.read();
337 if (b == -1) {
338 throw new EOFException();
339 }
340 v = b;
341 if ((b & 0x80) == 0x00) {
342 return v;
343 } else if ((b & 0xC0) == 0x80) {
344 v &= 0x3F;
345 b = in.read();
346 if (b == -1) {
347 throw new EOFException();
348 }
349 v = (v << 8) | b;
350 return v;
351 } else if ((b & 0xE0) == 0xC0) {
352 v &= 0x1F;
353 b = in.read();
354 if (b == -1) {
355 throw new EOFException();
356 }
357 v = (v << 8) | b;
358 b = in.read();
359 if (b == -1) {
360 throw new EOFException();
361 }
362 v = (v << 8) | b;
363 return v;
364 } else if ((b & 0xF0) == 0xE0) {
365 v &= 0x0F;
366 b = in.read();
367 if (b == -1) {
368 throw new EOFException();
369 }
370 v = (v << 8) | b;
371 b = in.read();
372 if (b == -1) {
373 throw new EOFException();
374 }
375 v = (v << 8) | b;
376 b = in.read();
377 if (b == -1) {
378 throw new EOFException();
379 }
380 v = (v << 8) | b;
381 return v;
382 } else {
383 b = in.read();
384 if (b == -1) {
385 throw new EOFException();
386 }
387 v = b;
388 b = in.read();
389 if (b == -1) {
390 throw new EOFException();
391 }
392 v = (v << 8) | b;
393 b = in.read();
394 if (b == -1) {
395 throw new EOFException();
396 }
397 v = (v << 8) | b;
398 b = in.read();
399 if (b == -1) {
400 throw new EOFException();
401 }
402 v = (v << 8) | b;
403 return v;
404 }
405 }
406
407 /**
408 * Read a long value from this data input stream. From one to nine bytes are
409 * read, in big-endian order, as follows:
410 * <UL>
411 *
412 * <LI>
413 * If the first byte's most significant bit is 0, then the least significant
414 * 7 bits are sign-extended to a long and returned.
415 *
416 * <LI>
417 * Else if the first byte's two most significant bits are 10, then one more
418 * byte is read, and the least significant 14 bits are sign-extended to a
419 * long and returned.
420 *
421 * <LI>
422 * Else if the first byte's three most significant bits are 110, then two
423 * more bytes are read, and the least significant 21 bits are sign-extended
424 * to a long and returned.
425 *
426 * <LI>
427 * Else if the first byte's four most significant bits are 1110, then three
428 * more bytes are read, and the least significant 28 bits are sign-extended
429 * to a long and returned.
430 *
431 * <LI>
432 * Else if the first byte's five most significant bits are 11110, then four
433 * more bytes are read, and the least significant 35 bits are sign-extended
434 * to a long and returned.
435 *
436 * <LI>
437 * Else if the first byte's six most significant bits are 111110, then five
438 * more bytes are read, and the least significant 42 bits are sign-extended
439 * to a long and returned.
440 *
441 * <LI>
442 * Else if the first byte's seven most significant bits are 1111110, then
443 * six more bytes are read, and the least significant 49 bits are
444 * sign-extended to a long and returned.
445 *
446 * <LI>
447 * Else if the first byte is 11111110, then seven more bytes are read, and
448 * the least significant 56 bits are sign-extended to a long and returned.
449 *
450 * <LI>
451 * Else eight more bytes are read, and the least significant 64 bits are
452 * returned.
453 * </UL>
454 *
455 * @return Long value.
456 * @exception EOFException Thrown if the end of the stream is encountered.
457 * @exception IOException Thrown if an I/O error occurred.
458 * @throws java.io.IOException if any.
459 */
460 public long readLong()
461 throws IOException {
462 long v;
463 int b = in.read();
464 if (b == -1) {
465 throw new EOFException();
466 }
467 v = b;
468 if ((b & 0x80) == 0x00) {
469 v <<= 57L;
470 v >>= 57L;
471 return v;
472 } else if ((b & 0xC0) == 0x80) {
473 b = in.read();
474 if (b == -1) {
475 throw new EOFException();
476 }
477 v = (v << 8L) | b;
478 v <<= 50L;
479 v >>= 50L;
480 return v;
481 } else if ((b & 0xE0) == 0xC0) {
482 b = in.read();
483 if (b == -1) {
484 throw new EOFException();
485 }
486 v = (v << 8L) | b;
487 b = in.read();
488 if (b == -1) {
489 throw new EOFException();
490 }
491 v = (v << 8L) | b;
492 v <<= 43L;
493 v >>= 43L;
494 return v;
495 } else if ((b & 0xF0) == 0xE0) {
496 b = in.read();
497 if (b == -1) {
498 throw new EOFException();
499 }
500 v = (v << 8L) | b;
501 b = in.read();
502 if (b == -1) {
503 throw new EOFException();
504 }
505 v = (v << 8L) | b;
506 b = in.read();
507 if (b == -1) {
508 throw new EOFException();
509 }
510 v = (v << 8L) | b;
511 v <<= 36L;
512 v >>= 36L;
513 return v;
514 } else if ((b & 0xF8) == 0xF0) {
515 b = in.read();
516 if (b == -1) {
517 throw new EOFException();
518 }
519 v = (v << 8L) | b;
520 b = in.read();
521 if (b == -1) {
522 throw new EOFException();
523 }
524 v = (v << 8L) | b;
525 b = in.read();
526 if (b == -1) {
527 throw new EOFException();
528 }
529 v = (v << 8L) | b;
530 b = in.read();
531 if (b == -1) {
532 throw new EOFException();
533 }
534 v = (v << 8L) | b;
535 v <<= 29L;
536 v >>= 29L;
537 return v;
538 } else if ((b & 0xFC) == 0xF8) {
539 b = in.read();
540 if (b == -1) {
541 throw new EOFException();
542 }
543 v = (v << 8L) | b;
544 b = in.read();
545 if (b == -1) {
546 throw new EOFException();
547 }
548 v = (v << 8L) | b;
549 b = in.read();
550 if (b == -1) {
551 throw new EOFException();
552 }
553 v = (v << 8L) | b;
554 b = in.read();
555 if (b == -1) {
556 throw new EOFException();
557 }
558 v = (v << 8L) | b;
559 b = in.read();
560 if (b == -1) {
561 throw new EOFException();
562 }
563 v = (v << 8L) | b;
564 v <<= 22L;
565 v >>= 22L;
566 return v;
567 } else if ((b & 0xFE) == 0xFC) {
568 b = in.read();
569 if (b == -1) {
570 throw new EOFException();
571 }
572 v = (v << 8L) | b;
573 b = in.read();
574 if (b == -1) {
575 throw new EOFException();
576 }
577 v = (v << 8L) | b;
578 b = in.read();
579 if (b == -1) {
580 throw new EOFException();
581 }
582 v = (v << 8L) | b;
583 b = in.read();
584 if (b == -1) {
585 throw new EOFException();
586 }
587 v = (v << 8L) | b;
588 b = in.read();
589 if (b == -1) {
590 throw new EOFException();
591 }
592 v = (v << 8L) | b;
593 b = in.read();
594 if (b == -1) {
595 throw new EOFException();
596 }
597 v = (v << 8L) | b;
598 v <<= 15L;
599 v >>= 15L;
600 return v;
601 } else if (b == 0xFE) {
602 b = in.read();
603 if (b == -1) {
604 throw new EOFException();
605 }
606 v = b;
607 b = in.read();
608 if (b == -1) {
609 throw new EOFException();
610 }
611 v = (v << 8L) | b;
612 b = in.read();
613 if (b == -1) {
614 throw new EOFException();
615 }
616 v = (v << 8L) | b;
617 b = in.read();
618 if (b == -1) {
619 throw new EOFException();
620 }
621 v = (v << 8L) | b;
622 b = in.read();
623 if (b == -1) {
624 throw new EOFException();
625 }
626 v = (v << 8L) | b;
627 b = in.read();
628 if (b == -1) {
629 throw new EOFException();
630 }
631 v = (v << 8L) | b;
632 b = in.read();
633 if (b == -1) {
634 throw new EOFException();
635 }
636 v = (v << 8L) | b;
637 v <<= 8L;
638 v >>= 8L;
639 return v;
640 } else {
641 b = in.read();
642 if (b == -1) {
643 throw new EOFException();
644 }
645 v = b;
646 b = in.read();
647 if (b == -1) {
648 throw new EOFException();
649 }
650 v = (v << 8L) | b;
651 b = in.read();
652 if (b == -1) {
653 throw new EOFException();
654 }
655 v = (v << 8L) | b;
656 b = in.read();
657 if (b == -1) {
658 throw new EOFException();
659 }
660 v = (v << 8L) | b;
661 b = in.read();
662 if (b == -1) {
663 throw new EOFException();
664 }
665 v = (v << 8L) | b;
666 b = in.read();
667 if (b == -1) {
668 throw new EOFException();
669 }
670 v = (v << 8L) | b;
671 b = in.read();
672 if (b == -1) {
673 throw new EOFException();
674 }
675 v = (v << 8L) | b;
676 b = in.read();
677 if (b == -1) {
678 throw new EOFException();
679 }
680 v = (v << 8L) | b;
681 return v;
682 }
683 }
684
685 /**
686 * Read an unsigned long value from this data input stream. From one to nine
687 * bytes are read, in big-endian order, as follows:
688 * <UL>
689 *
690 * <LI>
691 * If the first byte's most significant bit is 0, then the least significant
692 * 7 bits are zero-extended to a long and returned.
693 *
694 * <LI>
695 * Else if the first byte's two most significant bits are 10, then one more
696 * byte is read, and the least significant 14 bits are zero-extended to a
697 * long and returned.
698 *
699 * <LI>
700 * Else if the first byte's three most significant bits are 110, then two
701 * more bytes are read, and the least significant 21 bits are zero-extended
702 * to a long and returned.
703 *
704 * <LI>
705 * Else if the first byte's four most significant bits are 1110, then three
706 * more bytes are read, and the least significant 28 bits are zero-extended
707 * to a long and returned.
708 *
709 * <LI>
710 * Else if the first byte's five most significant bits are 11110, then four
711 * more bytes are read, and the least significant 35 bits are zero-extended
712 * to a long and returned.
713 *
714 * <LI>
715 * Else if the first byte's six most significant bits are 111110, then five
716 * more bytes are read, and the least significant 42 bits are zero-extended
717 * to a long and returned.
718 *
719 * <LI>
720 * Else if the first byte's seven most significant bits are 1111110, then
721 * six more bytes are read, and the least significant 49 bits are
722 * zero-extended to a long and returned.
723 *
724 * <LI>
725 * Else if the first byte is 11111110, then seven more bytes are read, and
726 * the least significant 56 bits are zero-extended to a long and returned.
727 *
728 * <LI>
729 * Else eight more bytes are read, and the least significant 64 bits are
730 * returned.
731 * </UL>
732 *
733 * @return Long value.
734 * @exception EOFException Thrown if the end of the stream is encountered.
735 * @exception IOException Thrown if an I/O error occurred.
736 * @throws java.io.IOException if any.
737 */
738 public long readUnsignedLong()
739 throws IOException {
740 long v;
741 int b = in.read();
742 if (b == -1) {
743 throw new EOFException();
744 }
745 v = b;
746 if ((b & 0x80) == 0x00) {
747 return v;
748 } else if ((b & 0xC0) == 0x80) {
749 v &= 0x3FL;
750 b = in.read();
751 if (b == -1) {
752 throw new EOFException();
753 }
754 v = (v << 8L) | b;
755 return v;
756 } else if ((b & 0xE0) == 0xC0) {
757 v &= 0x1FL;
758 b = in.read();
759 if (b == -1) {
760 throw new EOFException();
761 }
762 v = (v << 8L) | b;
763 b = in.read();
764 if (b == -1) {
765 throw new EOFException();
766 }
767 v = (v << 8L) | b;
768 return v;
769 } else if ((b & 0xF0) == 0xE0) {
770 v &= 0x0FL;
771 b = in.read();
772 if (b == -1) {
773 throw new EOFException();
774 }
775 v = (v << 8L) | b;
776 b = in.read();
777 if (b == -1) {
778 throw new EOFException();
779 }
780 v = (v << 8L) | b;
781 b = in.read();
782 if (b == -1) {
783 throw new EOFException();
784 }
785 v = (v << 8L) | b;
786 return v;
787 } else if ((b & 0xF8) == 0xF0) {
788 v &= 0x07L;
789 b = in.read();
790 if (b == -1) {
791 throw new EOFException();
792 }
793 v = (v << 8L) | b;
794 b = in.read();
795 if (b == -1) {
796 throw new EOFException();
797 }
798 v = (v << 8L) | b;
799 b = in.read();
800 if (b == -1) {
801 throw new EOFException();
802 }
803 v = (v << 8L) | b;
804 b = in.read();
805 if (b == -1) {
806 throw new EOFException();
807 }
808 v = (v << 8L) | b;
809 return v;
810 } else if ((b & 0xFC) == 0xF8) {
811 v &= 0x03L;
812 b = in.read();
813 if (b == -1) {
814 throw new EOFException();
815 }
816 v = (v << 8L) | b;
817 b = in.read();
818 if (b == -1) {
819 throw new EOFException();
820 }
821 v = (v << 8L) | b;
822 b = in.read();
823 if (b == -1) {
824 throw new EOFException();
825 }
826 v = (v << 8L) | b;
827 b = in.read();
828 if (b == -1) {
829 throw new EOFException();
830 }
831 v = (v << 8L) | b;
832 b = in.read();
833 if (b == -1) {
834 throw new EOFException();
835 }
836 v = (v << 8L) | b;
837 return v;
838 } else if ((b & 0xFE) == 0xFC) {
839 v &= 0x01L;
840 b = in.read();
841 if (b == -1) {
842 throw new EOFException();
843 }
844 v = (v << 8L) | b;
845 b = in.read();
846 if (b == -1) {
847 throw new EOFException();
848 }
849 v = (v << 8L) | b;
850 b = in.read();
851 if (b == -1) {
852 throw new EOFException();
853 }
854 v = (v << 8L) | b;
855 b = in.read();
856 if (b == -1) {
857 throw new EOFException();
858 }
859 v = (v << 8L) | b;
860 b = in.read();
861 if (b == -1) {
862 throw new EOFException();
863 }
864 v = (v << 8L) | b;
865 b = in.read();
866 if (b == -1) {
867 throw new EOFException();
868 }
869 v = (v << 8L) | b;
870 return v;
871 } else if (b == 0xFE) {
872 b = in.read();
873 if (b == -1) {
874 throw new EOFException();
875 }
876 v = b;
877 b = in.read();
878 if (b == -1) {
879 throw new EOFException();
880 }
881 v = (v << 8L) | b;
882 b = in.read();
883 if (b == -1) {
884 throw new EOFException();
885 }
886 v = (v << 8L) | b;
887 b = in.read();
888 if (b == -1) {
889 throw new EOFException();
890 }
891 v = (v << 8L) | b;
892 b = in.read();
893 if (b == -1) {
894 throw new EOFException();
895 }
896 v = (v << 8L) | b;
897 b = in.read();
898 if (b == -1) {
899 throw new EOFException();
900 }
901 v = (v << 8L) | b;
902 b = in.read();
903 if (b == -1) {
904 throw new EOFException();
905 }
906 v = (v << 8L) | b;
907 return v;
908 } else {
909 b = in.read();
910 if (b == -1) {
911 throw new EOFException();
912 }
913 v = b;
914 b = in.read();
915 if (b == -1) {
916 throw new EOFException();
917 }
918 v = (v << 8L) | b;
919 b = in.read();
920 if (b == -1) {
921 throw new EOFException();
922 }
923 v = (v << 8L) | b;
924 b = in.read();
925 if (b == -1) {
926 throw new EOFException();
927 }
928 v = (v << 8L) | b;
929 b = in.read();
930 if (b == -1) {
931 throw new EOFException();
932 }
933 v = (v << 8L) | b;
934 b = in.read();
935 if (b == -1) {
936 throw new EOFException();
937 }
938 v = (v << 8L) | b;
939 b = in.read();
940 if (b == -1) {
941 throw new EOFException();
942 }
943 v = (v << 8L) | b;
944 b = in.read();
945 if (b == -1) {
946 throw new EOFException();
947 }
948 v = (v << 8L) | b;
949 return v;
950 }
951 }
952
953 /**
954 * Read a float value from this data input stream. Four bytes are read in
955 * big-endian order yielding an int value <code>v</code>, then
956 * <code>Float.intBitsToFloat(v)</code> is returned.
957 *
958 * @return Float value.
959 * @exception EOFException Thrown if the end of the stream is encountered.
960 * @exception IOException Thrown if an I/O error occurred.
961 * @throws java.io.IOException if any.
962 */
963 public float readFloat()
964 throws IOException {
965 int v;
966 int b;
967 b = in.read();
968 if (b == -1) {
969 throw new EOFException();
970 }
971 v = b;
972 b = in.read();
973 if (b == -1) {
974 throw new EOFException();
975 }
976 v = (v << 8) | b;
977 b = in.read();
978 if (b == -1) {
979 throw new EOFException();
980 }
981 v = (v << 8) | b;
982 b = in.read();
983 if (b == -1) {
984 throw new EOFException();
985 }
986 v = (v << 8) | b;
987 return Float.intBitsToFloat(v);
988 }
989
990 /**
991 * Read a double value from this data input stream. Eight bytes are read in
992 * big-endian order yielding a long value <code>v</code>, then
993 * <code>Double.longBitsToDouble(v)</code> is returned.
994 *
995 * @return Double value.
996 * @exception EOFException Thrown if the end of the stream is encountered.
997 * @exception IOException Thrown if an I/O error occurred.
998 * @throws java.io.IOException if any.
999 */
1000 public double readDouble()
1001 throws IOException {
1002 long v;
1003 int b;
1004 b = in.read();
1005 if (b == -1) {
1006 throw new EOFException();
1007 }
1008 v = b;
1009 b = in.read();
1010 if (b == -1) {
1011 throw new EOFException();
1012 }
1013 v = (v << 8L) | b;
1014 b = in.read();
1015 if (b == -1) {
1016 throw new EOFException();
1017 }
1018 v = (v << 8L) | b;
1019 b = in.read();
1020 if (b == -1) {
1021 throw new EOFException();
1022 }
1023 v = (v << 8L) | b;
1024 b = in.read();
1025 if (b == -1) {
1026 throw new EOFException();
1027 }
1028 v = (v << 8L) | b;
1029 b = in.read();
1030 if (b == -1) {
1031 throw new EOFException();
1032 }
1033 v = (v << 8L) | b;
1034 b = in.read();
1035 if (b == -1) {
1036 throw new EOFException();
1037 }
1038 v = (v << 8L) | b;
1039 b = in.read();
1040 if (b == -1) {
1041 throw new EOFException();
1042 }
1043 v = (v << 8L) | b;
1044 return Double.longBitsToDouble(v);
1045 }
1046
1047 /**
1048 * Read a string value from this data input stream. The length of the string
1049 * is read using <code>readUnsignedInt()</code>, then each character of the
1050 * string is read using <code>readUnsignedInt()</code>.
1051 *
1052 * @return String value.
1053 * @exception EOFException Thrown if the end of the stream is encountered.
1054 * @exception IOException Thrown if an I/O error occurred.
1055 * @throws java.io.IOException if any.
1056 */
1057 public String readString()
1058 throws IOException {
1059 int n = readUnsignedInt();
1060 char[] v = new char[n];
1061 for (int i = 0; i < n; ++i) {
1062 v[i] = (char) readUnsignedInt();
1063 }
1064 return new String(v);
1065 }
1066
1067 }