home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / DataInputStream.java < prev    next >
Text File  |  1996-05-03  |  9KB  |  334 lines

  1. /*
  2.  * @(#)DataInputStream.java    1.28 95/12/18 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.io;
  21.  
  22. /**
  23.  * A data input stream that lets you read primitive Java data types
  24.  * from a stream in a portable way.  Primitive data types are well
  25.  * understood types with associated operations.  For example,
  26.  * Integers are considered primitive data types. 
  27.  *
  28.  * @see DataOutputStream
  29.  * @version     1.28, 18 Dec 1995
  30.  * @author    Arthur van Hoff
  31.  */
  32. public
  33. class DataInputStream extends FilterInputStream implements DataInput {
  34.     /**
  35.      * Creates a new DataInputStream.
  36.      * @param in     the input stream
  37.      */
  38.     public DataInputStream(InputStream in) {
  39.     super(in);
  40.     }
  41.  
  42.     /**
  43.      * Reads data into an array of bytes.
  44.      * This method blocks until some input is available.
  45.      * @param b    the buffer into which the data is read
  46.      * @return  the actual number of bytes read, -1 is
  47.      *         returned when the end of the stream is reached.
  48.      * @exception IOException If an I/O error has occurred.
  49.      */
  50.     public final int read(byte b[]) throws IOException {
  51.     return in.read(b, 0, b.length);
  52.     }
  53.  
  54.     /**
  55.      * Reads data into an array of bytes.
  56.      * This method blocks until some input is available.
  57.      * @param b    the buffer into which the data is read
  58.      * @param off the start offset of the data
  59.      * @param len the maximum number of bytes read
  60.      * @return  the actual number of bytes read, -1 is
  61.      *         returned when the end of the stream is reached.
  62.      * @exception IOException If an I/O error has occurred.
  63.      */
  64.     public final int read(byte b[], int off, int len) throws IOException {
  65.     return in.read(b, off, len);
  66.     }
  67.  
  68.     /**
  69.      * Reads bytes, blocking until all bytes are read.
  70.      * @param b    the buffer into which the data is read
  71.      * @exception IOException If an I/O error has occurred.
  72.      * @exception EOFException If EOF reached before all bytes are read.
  73.      */
  74.     public final void readFully(byte b[]) throws IOException {
  75.     readFully(b, 0, b.length);
  76.     }
  77.  
  78.     /**
  79.      * Reads bytes, blocking until all bytes are read.
  80.      * @param b    the buffer into which the data is read
  81.      * @param off the start offset of the data
  82.      * @param len the maximum number of bytes read
  83.      * @exception IOException If an I/O error has occurred.
  84.      * @exception EOFException If EOF reached before all bytes are read.
  85.      */
  86.     public final void readFully(byte b[], int off, int len) throws IOException {
  87.     InputStream in = this.in;
  88.     int n = 0;
  89.     while (n < len) {
  90.         int count = in.read(b, off + n, len - n);
  91.         if (count < 0)
  92.         throw new EOFException();
  93.         n += count;
  94.     }
  95.     }
  96.  
  97.     /**
  98.      * Skips bytes, blocks until all bytes are skipped.
  99.      * @param n the number of bytes to be skipped
  100.      * @return    the actual number of bytes skipped.
  101.      * @exception IOException If an I/O error has occurred.
  102.      */
  103.     public final int skipBytes(int n) throws IOException {
  104.     InputStream in = this.in;
  105.     for (int i = 0 ; i < n ; i += (int)in.skip(n - i));
  106.     return n;
  107.     }
  108.  
  109.     /**
  110.      * Reads a boolean.
  111.      * @return the boolean read.
  112.      */
  113.     public final boolean readBoolean() throws IOException {
  114.     int ch = in.read();
  115.     if (ch < 0)
  116.         throw new EOFException();
  117.     return (ch != 0);
  118.     }
  119.  
  120.     /**
  121.      * Reads an 8 bit byte.
  122.      * @return the 8 bit byte read.
  123.      */
  124.     public final byte readByte() throws IOException {
  125.     int ch = in.read();
  126.     if (ch < 0)
  127.         throw new EOFException();
  128.     return (byte)(ch);
  129.     }
  130.  
  131.     /**
  132.      * Reads an unsigned 8 bit byte.
  133.      * @return the 8 bit byte read.
  134.      */
  135.     public final int readUnsignedByte() throws IOException {
  136.     int ch = in.read();
  137.     if (ch < 0)
  138.         throw new EOFException();
  139.     return ch;
  140.     }
  141.  
  142.  
  143.     /**
  144.      * Reads a 16 bit short.
  145.      * @return the 16 bit short read.
  146.      */
  147.     public final short readShort() throws IOException {
  148.     InputStream in = this.in;
  149.     int ch1 = in.read();
  150.     int ch2 = in.read();
  151.     if ((ch1 | ch2) < 0)
  152.          throw new EOFException();
  153.     return (short)((ch1 << 8) + (ch2 << 0));
  154.     }
  155.  
  156.  
  157.     /**
  158.      * Reads 16 bit short.
  159.      * @return the 16 bit short read.
  160.      */
  161.     public final int readUnsignedShort() throws IOException {
  162.     InputStream in = this.in;
  163.     int ch1 = in.read();
  164.     int ch2 = in.read();
  165.     if ((ch1 | ch2) < 0)
  166.          throw new EOFException();
  167.     return (ch1 << 8) + (ch2 << 0);
  168.     }
  169.  
  170.  
  171.     /**
  172.      * Reads a 16 bit char.
  173.      * @return the read 16 bit char. 
  174.      */
  175.     public final char readChar() throws IOException {
  176.     InputStream in = this.in;
  177.     int ch1 = in.read();
  178.     int ch2 = in.read();
  179.     if ((ch1 | ch2) < 0)
  180.          throw new EOFException();
  181.     return (char)((ch1 << 8) + (ch2 << 0));
  182.     }
  183.  
  184.     /**
  185.      * Reads a 32 bit int.
  186.      * @return the 32 bit integer read.
  187.      */
  188.     public final int readInt() throws IOException {
  189.     InputStream in = this.in;
  190.     int ch1 = in.read();
  191.     int ch2 = in.read();
  192.     int ch3 = in.read();
  193.     int ch4 = in.read();
  194.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  195.          throw new EOFException();
  196.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  197.     }
  198.  
  199.     /**
  200.      * Reads a 64 bit long.
  201.      * @return the 64 bit long read.
  202.      */
  203.     public final long readLong() throws IOException {
  204.     InputStream in = this.in;
  205.     return (readInt() << 32L) + (readInt() & 0xFFFFFFFFL);
  206.     }
  207.  
  208.     /**
  209.      * Reads a 32 bit float.
  210.      * @return the read 32 bit float.
  211.      */
  212.     public final float readFloat() throws IOException {
  213.     return Float.intBitsToFloat(readInt());
  214.     }
  215.  
  216.     /**
  217.      * Reads a 64 bit double.
  218.      * @return the 64 bit double read.
  219.      */
  220.     public final double readDouble() throws IOException {
  221.     return Double.longBitsToDouble(readLong());
  222.     }
  223.  
  224.     private char lineBuffer[];
  225.  
  226.     /**
  227.      * Reads in a line that has been terminated by a \n, \r, 
  228.      * \r\n or EOF.
  229.      * @return a String copy of the line.
  230.      */
  231.     public final String readLine() throws IOException {
  232.     InputStream in = this.in;
  233.     char buf[] = lineBuffer;
  234.  
  235.     if (buf == null) {
  236.         buf = lineBuffer = new char[128];
  237.     }
  238.  
  239.     int room = buf.length;
  240.     int offset = 0;
  241.     int c;
  242.  
  243. loop:    while (true) {
  244.         switch (c = in.read()) {
  245.           case -1: 
  246.           case '\n':
  247.         break loop;
  248.  
  249.           case '\r':
  250.         int c2 = in.read();
  251.         if (c2 != '\n') {
  252.             if (!(in instanceof PushbackInputStream)) {
  253.             in = this.in = new PushbackInputStream(in);
  254.             }
  255.             ((PushbackInputStream)in).unread(c2);
  256.         }
  257.         break loop;
  258.  
  259.           default:
  260.         if (--room < 0) {
  261.             buf = new char[offset + 128];
  262.             room = buf.length - offset - 1;
  263.             System.arraycopy(lineBuffer, 0, buf, 0, offset);
  264.             lineBuffer = buf;
  265.         }
  266.         buf[offset++] = (char) c;
  267.         break;
  268.         }
  269.     }
  270.     if ((c == -1) && (offset == 0)) {
  271.         return null;
  272.     }
  273.     return String.copyValueOf(buf, 0, offset);
  274.     }
  275.  
  276.     /**
  277.      * Reads a UTF format String.
  278.      * @return the String.
  279.      */
  280.     public final String readUTF() throws IOException {
  281.         return readUTF(this);
  282.     }
  283.  
  284.     /**
  285.      * Reads a UTF format String from the given input stream.
  286.      * @return the String.
  287.      */
  288.     public final static String readUTF(DataInput in) throws IOException {
  289.         int utflen = in.readUnsignedShort();
  290.         char str[] = new char[utflen];
  291.     int count = 0;
  292.     int strlen = 0;
  293.     while (count < utflen) {
  294.         int c = in.readUnsignedByte();
  295.         int char2, char3;
  296.         switch (c >> 4) { 
  297.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  298.             // 0xxxxxxx
  299.             count++;
  300.             str[strlen++] = (char)c;
  301.             break;
  302.             case 12: case 13:
  303.             // 110x xxxx   10xx xxxx
  304.             count += 2;
  305.             if (count > utflen) 
  306.             throw new UTFDataFormatException();          
  307.             char2 = in.readUnsignedByte();
  308.             if ((char2 & 0xC0) != 0x80)
  309.             throw new UTFDataFormatException();          
  310.             str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  311.             break;
  312.             case 14:
  313.             // 1110 xxxx  10xx xxxx  10xx xxxx
  314.             count += 3;
  315.             if (count > utflen) 
  316.             throw new UTFDataFormatException();          
  317.             char2 = in.readUnsignedByte();
  318.             char3 = in.readUnsignedByte();
  319.             if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  320.             throw new UTFDataFormatException();          
  321.             str[strlen++] = (char)(((c & 0x0F) << 12) |
  322.                        ((char2 & 0x3F) << 6) |
  323.                        ((char3 & 0x3F) << 0));
  324.             default:
  325.             // 10xx xxxx,  1111 xxxx
  326.             throw new UTFDataFormatException();          
  327.         }
  328.     }
  329.         return new String(str, 0, strlen);
  330.     }
  331. }
  332.  
  333.  
  334.