home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / java / io / DataInputStream.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  10.0 KB  |  368 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, 12/18/95
  30.  * @author    Arthur van Hoff
  31.  */
  32. public
  33. class DataInputStream extends FilterInputStream implements DataInput {
  34.  
  35.     private boolean isLocalised = false;
  36.     
  37.     /**
  38.      * Creates a new DataInputStream.
  39.      * @param in     the input stream
  40.      */
  41.     public DataInputStream(InputStream in) {
  42.     super(in);
  43.     }
  44.  
  45.     /**
  46.      * Reads data into an array of bytes.
  47.      * This method blocks until some input is available.
  48.      * @param b    the buffer into which the data is read
  49.      * @return  the actual number of bytes read, -1 is
  50.      *         returned when the end of the stream is reached.
  51.      * @exception IOException If an I/O error has occurred.
  52.      */
  53.     public final int read(byte b[]) throws IOException {
  54.     return in.read(b, 0, b.length);
  55.     }
  56.  
  57.     /**
  58.      * Reads data into an array of bytes.
  59.      * This method blocks until some input is available.
  60.      * @param b    the buffer into which the data is read
  61.      * @param off the start offset of the data
  62.      * @param len the maximum number of bytes read
  63.      * @return  the actual number of bytes read, -1 is
  64.      *         returned when the end of the stream is reached.
  65.      * @exception IOException If an I/O error has occurred.
  66.      */
  67.     public final int read(byte b[], int off, int len) throws IOException {
  68.     return in.read(b, off, len);
  69.     }
  70.  
  71.     /**
  72.      * Reads bytes, blocking until all bytes are read.
  73.      * @param b    the buffer into which the data is read
  74.      * @exception IOException If an I/O error has occurred.
  75.      * @exception EOFException If EOF reached before all bytes are read.
  76.      */
  77.     public final void readFully(byte b[]) throws IOException {
  78.     readFully(b, 0, b.length);
  79.     }
  80.  
  81.     /**
  82.      * Reads bytes, blocking until all bytes are read.
  83.      * @param b    the buffer into which the data is read
  84.      * @param off the start offset of the data
  85.      * @param len the maximum number of bytes read
  86.      * @exception IOException If an I/O error has occurred.
  87.      * @exception EOFException If EOF reached before all bytes are read.
  88.      */
  89.     public final void readFully(byte b[], int off, int len) throws IOException {
  90.     InputStream in = this.in;
  91.     int n = 0;
  92.     while (n < len) {
  93.         int count = in.read(b, off + n, len - n);
  94.         if (count < 0)
  95.         throw new EOFException();
  96.         n += count;
  97.     }
  98.     }
  99.  
  100.     /**
  101.      * Skips bytes, blocks until all bytes are skipped.
  102.      * @param n the number of bytes to be skipped
  103.      * @return    the actual number of bytes skipped.
  104.      * @exception IOException If an I/O error has occurred.
  105.      */
  106.     public final int skipBytes(int n) throws IOException {
  107.     InputStream in = this.in;
  108.     for (int i = 0 ; i < n ; i += (int)in.skip(n - i));
  109.     return n;
  110.     }
  111.  
  112.     /**
  113.      * Reads a boolean.
  114.      * @return the boolean read.
  115.      */
  116.     public final boolean readBoolean() throws IOException {
  117.     int ch = in.read();
  118.     if (ch < 0)
  119.         throw new EOFException();
  120.     return (ch != 0);
  121.     }
  122.  
  123.     /**
  124.      * Reads an 8 bit byte.
  125.      * @return the 8 bit byte read.
  126.      */
  127.     public final byte readByte() throws IOException {
  128.     int ch = in.read();
  129.     if (ch < 0)
  130.         throw new EOFException();
  131.     return (byte)(ch);
  132.     }
  133.  
  134.     /**
  135.      * Reads an unsigned 8 bit byte.
  136.      * @return the 8 bit byte read.
  137.      */
  138.     public final int readUnsignedByte() throws IOException {
  139.     int ch = in.read();
  140.     if (ch < 0)
  141.         throw new EOFException();
  142.     return ch;
  143.     }
  144.  
  145.  
  146.     /**
  147.      * Reads a 16 bit short.
  148.      * @return the 16 bit short read.
  149.      */
  150.     public final short readShort() throws IOException {
  151.     InputStream in = this.in;
  152.     int ch1 = in.read();
  153.     int ch2 = in.read();
  154.     if ((ch1 | ch2) < 0)
  155.          throw new EOFException();
  156.     return (short)((ch1 << 8) + (ch2 << 0));
  157.     }
  158.  
  159.  
  160.     /**
  161.      * Reads 16 bit short.
  162.      * @return the 16 bit short read.
  163.      */
  164.     public final int readUnsignedShort() throws IOException {
  165.     InputStream in = this.in;
  166.     int ch1 = in.read();
  167.     int ch2 = in.read();
  168.     if ((ch1 | ch2) < 0)
  169.          throw new EOFException();
  170.     return (ch1 << 8) + (ch2 << 0);
  171.     }
  172.  
  173.  
  174.     /**
  175.      * Reads a 16 bit char.
  176.      * @return the read 16 bit char. 
  177.      */
  178.     public final char readChar() throws IOException {
  179.     InputStream in = this.in;
  180.     if( isLocalised )
  181.     {
  182.         int byte1 = in.read();
  183.         if( byte1 < 0)
  184.              throw new EOFException();
  185.              
  186.         if( com.ms.lang.SystemX.isLocalCharDBCSLeadByte((byte)byte1 ))
  187.             {                   
  188.             byte B[] = new byte[2];
  189.             char C[] ;
  190.             
  191.             int temp = in.read();
  192.             if( temp < 0 )
  193.                 throw new EOFException();
  194.                 
  195.             B[0] = (byte)byte1;
  196.             B[1] = (byte)temp;
  197.                 
  198.             C = com.ms.lang.SystemX.LocalStringToJavaString(B);
  199.             return C[0];
  200.             }
  201.         else
  202.             return com.ms.lang.SystemX.LocalStringToJavaString((byte)byte1);
  203.     }
  204.     else
  205.     {
  206.         int ch1 = in.read();
  207.         int ch2 = in.read();
  208.         if ((ch1 | ch2) < 0)
  209.              throw new EOFException();
  210.         return (char)((ch1 << 8) + (ch2 << 0));
  211.     }
  212.     }
  213.  
  214.     /**
  215.      * Reads a 32 bit int.
  216.      * @return the 32 bit integer read.
  217.      */
  218.     public final int readInt() throws IOException {
  219.     InputStream in = this.in;
  220.     int ch1 = in.read();
  221.     int ch2 = in.read();
  222.     int ch3 = in.read();
  223.     int ch4 = in.read();
  224.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  225.          throw new EOFException();
  226.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  227.     }
  228.  
  229.     /**
  230.      * Reads a 64 bit long.
  231.      * @return the 64 bit long read.
  232.      */
  233.     public final long readLong() throws IOException {
  234.     InputStream in = this.in;
  235.     return (readInt() << 32L) + (readInt() & 0xFFFFFFFFL);
  236.     }
  237.  
  238.     /**
  239.      * Reads a 32 bit float.
  240.      * @return the read 32 bit float.
  241.      */
  242.     public final float readFloat() throws IOException {
  243.     return Float.intBitsToFloat(readInt());
  244.     }
  245.  
  246.     /**
  247.      * Reads a 64 bit double.
  248.      * @return the 64 bit double read.
  249.      */
  250.     public final double readDouble() throws IOException {
  251.     return Double.longBitsToDouble(readLong());
  252.     }
  253.  
  254.     private char lineBuffer[];
  255.  
  256.     /**
  257.      * Reads in a line that has been terminated by a \n, \r, 
  258.      * \r\n or EOF.
  259.      * @return a String copy of the line.
  260.      */
  261.     public final String readLine() throws IOException {
  262.     InputStream in = this.in;
  263.     char buf[] = lineBuffer;
  264.  
  265.     if (buf == null) {
  266.         buf = lineBuffer = new char[128];
  267.     }
  268.  
  269.     int room = buf.length;
  270.     int offset = 0;
  271.     int c;
  272.  
  273. loop:    while (true) {
  274.         switch (c = in.read()) {
  275.           case -1: 
  276.           case '\n':
  277.         break loop;
  278.  
  279.           case '\r':
  280.         int c2 = in.read();
  281.         if (c2 != '\n') {
  282.             if (!(in instanceof PushbackInputStream)) {
  283.             in = this.in = new PushbackInputStream(in);
  284.             }
  285.             ((PushbackInputStream)in).unread(c2);
  286.         }
  287.         break loop;
  288.  
  289.           default:
  290.         if (--room < 0) {
  291.             buf = new char[offset + 128];
  292.             room = buf.length - offset - 1;
  293.             System.arraycopy(lineBuffer, 0, buf, 0, offset);
  294.             lineBuffer = buf;
  295.         }
  296.         buf[offset++] = (char) c;
  297.         break;
  298.         }
  299.     }
  300.     if ((c == -1) && (offset == 0)) {
  301.         return null;
  302.     }
  303.     if( isLocalised )
  304.         return new String(com.ms.lang.SystemX.LocalStringToJavaString(buf, 0, offset));
  305.     else
  306.         return String.copyValueOf(buf, 0, offset);
  307.     }
  308.  
  309.     /**
  310.      * Reads a UTF format String.
  311.      * @return the String.
  312.      */
  313.     public final String readUTF() throws IOException {
  314.         return readUTF(this);
  315.     }
  316.  
  317.     /**
  318.      * Reads a UTF format String from the given input stream.
  319.      * @return the String.
  320.      */
  321.     public final static String readUTF(DataInput in) throws IOException {
  322.         int utflen = in.readUnsignedShort();
  323.         char str[] = new char[utflen];
  324.     int count = 0;
  325.     int strlen = 0;
  326.     while (count < utflen) {
  327.         int c = in.readUnsignedByte();
  328.         int char2, char3;
  329.         switch (c >> 4) { 
  330.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  331.             // 0xxxxxxx
  332.             count++;
  333.             str[strlen++] = (char)c;
  334.             break;
  335.             case 12: case 13:
  336.             // 110x xxxx   10xx xxxx
  337.             count += 2;
  338.             if (count > utflen) 
  339.             throw new UTFDataFormatException();          
  340.             char2 = in.readUnsignedByte();
  341.             if ((char2 & 0xC0) != 0x80)
  342.             throw new UTFDataFormatException();          
  343.             str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  344.             break;
  345.             case 14:
  346.             // 1110 xxxx  10xx xxxx  10xx xxxx
  347.             count += 3;
  348.             if (count > utflen) 
  349.             throw new UTFDataFormatException();          
  350.             char2 = in.readUnsignedByte();
  351.             char3 = in.readUnsignedByte();
  352.             if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  353.             throw new UTFDataFormatException();          
  354.             str[strlen++] = (char)(((c & 0x0F) << 12) |
  355.                        ((char2 & 0x3F) << 6) |
  356.                        ((char3 & 0x3F) << 0));
  357.                     break;                       
  358.             default:
  359.             // 10xx xxxx,  1111 xxxx
  360.             throw new UTFDataFormatException();          
  361.         }
  362.     }
  363.         return new String(str, 0, strlen);
  364.     }
  365. }
  366.  
  367.  
  368.