home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / DataInputStream.java < prev    next >
Text File  |  1998-09-22  |  25KB  |  617 lines

  1. /*
  2.  * @(#)DataInputStream.java    1.39 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * A data input stream lets an application read primitive Java data 
  19.  * types from an underlying input stream in a machine-independent 
  20.  * way. An application uses a data output stream to write data that 
  21.  * can later be read by a data input stream. 
  22.  * <p>
  23.  * Data input streams and data output streams represent Unicode 
  24.  * strings in a format that is a slight modification of UTF-8. (For 
  25.  * more information, see X/Open Company Ltd., "File System Safe 
  26.  * UCS Transformation Format (FSS_UTF)", X/Open Preliminary 
  27.  * Specification, Document Number: P316. This information also 
  28.  * appears in ISO/IEC 10646, Annex P.) 
  29.  * <p>
  30.  * All characters in the range <code>'\u0001'</code> to 
  31.  * <code>'\u007F'</code> are represented by a single byte:
  32.  * <center><table border="3">
  33.  *   <tr><td><i>0</i></td>  <td>bits 0-7</td></tr>
  34.  * </table></center>
  35.  * <p>
  36.  * The null character <code>'\u0000'</code> and characters in the 
  37.  * range <code>'\u0080'</code> to <code>'\u07FF'</code> are 
  38.  * represented by a pair of bytes:
  39.  * <center><table border="3">
  40.  *   <tr><td>1</td>  <td>1</td>  <td>0</td>  <td>bits 6-10</td></tr>
  41.  *   <tr><td>1</td>  <td>0</td>  <td colspan=2>bits 0-5</td></tr>
  42.  * </table></center><br>
  43.  * Characters in the range <code>'\u0800'</code> to 
  44.  * <code>'\uFFFF'</code> are represented by three bytes:
  45.  * <center><table border="3">
  46.  *   <tr><td>1</td>  <td>1</td>  <td>1</td>  <td>0</td>  <td>bits 12-15</td</tr>
  47.  *   <tr><td>1</td>  <td>0</td>  <td colspan=3>bits 6-11</td></tr>
  48.  *   <tr><td>1</td>  <td>0</td>  <td colspan=3>bits 0-5</td></tr>
  49.  * </table></center>
  50.  * <p>
  51.  * The two differences between this format and the 
  52.  * "standard" UTF-8 format are the following: 
  53.  * <ul>
  54.  * <li>The null byte <code>'\u0000'</code> is encoded in 2-byte format 
  55.  *     rather than 1-byte, so that the encoded strings never have 
  56.  *     embedded nulls. 
  57.  * <li>Only the 1-byte, 2-byte, and 3-byte formats are used. 
  58.  * </ul>
  59.  *
  60.  * @author  Arthur van Hoff
  61.  * @version 1.39, 07/01/98
  62.  * @see     java.io.DataOutputStream
  63.  * @since   JDK1.0
  64.  */
  65. public
  66. class DataInputStream extends FilterInputStream implements DataInput {
  67.     /**
  68.      * Creates a new data input stream to read data from the specified 
  69.      * input stream. 
  70.      *
  71.      * @param  in   the input stream.
  72.      */
  73.     public DataInputStream(InputStream in) {
  74.     super(in);
  75.     }
  76.  
  77.     /**
  78.      * Reads up to <code>byte.length</code> bytes of data from this data 
  79.      * input stream into an array of bytes. This method blocks until some 
  80.      * input is available. 
  81.      * <p>
  82.      * The <code>read</code> method of <code>DataInputStream</code> 
  83.      * calls the <code>read</code> method of its underlying input stream 
  84.      * with the three arguments <code>b</code>, <code>0</code>, and 
  85.      * <code>b.length</code> and returns whatever value that method returns.
  86.      *
  87.      * @param      b   the buffer into which the data is read.
  88.      * @return     the total number of bytes read into the buffer, or
  89.      *             <code>-1</code> if there is no more data because the end
  90.      *             of the stream has been reached.
  91.      * @exception  IOException  if an I/O error occurs.
  92.      * @see        java.io.FilterInputStream#in
  93.      * @see        java.io.InputStream#read(byte[], int, int)
  94.      */
  95.     public final int read(byte b[]) throws IOException {
  96.     return in.read(b, 0, b.length);
  97.     }
  98.  
  99.     /**
  100.      * Reads up to <code>len</code> bytes of data from this data input 
  101.      * stream into an array of bytes. This method blocks until some input 
  102.      * is available. 
  103.      * <p>
  104.      * The <code>read</code> method of <code>DataInputStream</code> 
  105.      * calls the <code>read</code> method of its underlying input stream 
  106.      * with the same arguments and returns whatever value that method returns.
  107.      *
  108.      * @param      b     the buffer into which the data is read.
  109.      * @param      off   the start offset of the data.
  110.      * @param      len   the maximum number of bytes read.
  111.      * @return     the total number of bytes read into the buffer, or
  112.      *             <code>-1</code> if there is no more data because the end
  113.      *             of the stream has been reached.
  114.      * @exception  IOException  if an I/O error occurs.
  115.      * @see        java.io.FilterInputStream#in
  116.      * @see        java.io.InputStream#read(byte[], int, int)
  117.      */
  118.     public final int read(byte b[], int off, int len) throws IOException {
  119.     return in.read(b, off, len);
  120.     }
  121.  
  122.     /**
  123.      * Reads <code>b.length</code> bytes from this data input stream 
  124.      * into the byte array. This method reads repeatedly from the 
  125.      * underlying stream until all the bytes are read. This method blocks 
  126.      * until all the bytes are read, the end of the stream is detected, 
  127.      * or an exception is thrown. 
  128.      *
  129.      * @param      b   the buffer into which the data is read.
  130.      * @exception  EOFException  if this input stream reaches the end before
  131.      *               reading all the bytes.
  132.      * @exception  IOException   if an I/O error occurs.
  133.      * @see        java.io.FilterInputStream#in
  134.      */
  135.     public final void readFully(byte b[]) throws IOException {
  136.     readFully(b, 0, b.length);
  137.     }
  138.  
  139.     /**
  140.      * Reads exactly <code>len</code> bytes from this data input stream 
  141.      * into the byte array. This method reads repeatedly from the 
  142.      * underlying stream until all the bytes are read. This method blocks 
  143.      * until all the bytes are read, the end of the stream is detected, 
  144.      * or an exception is thrown. 
  145.      *
  146.      * @param      b     the buffer into which the data is read.
  147.      * @param      off   the start offset of the data.
  148.      * @param      len   the number of bytes to read.
  149.      * @exception  EOFException  if this input stream reaches the end before
  150.      *               reading all the bytes.
  151.      * @exception  IOException   if an I/O error occurs.
  152.      * @see        java.io.FilterInputStream#in
  153.      */
  154.     public final void readFully(byte b[], int off, int len) throws IOException {
  155.     InputStream in = this.in;
  156.     int n = 0;
  157.     while (n < len) {
  158.         int count = in.read(b, off + n, len - n);
  159.         if (count < 0)
  160.         throw new EOFException();
  161.         n += count;
  162.     }
  163.     }
  164.  
  165.     /**
  166.      * Skips exactly <code>n</code> bytes of input in the underlying 
  167.      * input stream. This method blocks until all the bytes are skipped, 
  168.      * the end of the stream is detected, or an exception is thrown. 
  169.      *
  170.      * @param      n   the number of bytes to be skipped.
  171.      * @return     the number of bytes skipped, which is always <code>n</code>.
  172.      * @exception  EOFException  if this input stream reaches the end before
  173.      *               skipping all the bytes.
  174.      * @exception  IOException   if an I/O error occurs.
  175.      */
  176.     public final int skipBytes(int n) throws IOException {
  177.     InputStream in = this.in;
  178.     for (int i = 0 ; i < n ; i += (int)in.skip(n - i));
  179.     return n;
  180.     }
  181.  
  182.     /**
  183.      * Reads a <code>boolean</code> from this data input stream. This 
  184.      * method reads a single byte from the underlying input stream. A 
  185.      * value of <code>0</code> represents <code>false</code>. Any other 
  186.      * value represents <code>true</code>. This method blocks until 
  187.      * either the byte is read, the end of the stream is detected, or an 
  188.      * exception is thrown. 
  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.      * @see        java.io.FilterInputStream#in
  194.      */
  195.     public final boolean readBoolean() throws IOException {
  196.     int ch = in.read();
  197.     if (ch < 0)
  198.         throw new EOFException();
  199.     return (ch != 0);
  200.     }
  201.  
  202.     /**
  203.      * Reads a signed 8-bit value from this data input stream. This 
  204.      * method reads a byte from the underlying input stream. If the byte 
  205.      * read is <code>b</code>, where 
  206.      * 0 <= <code>b</code> <= 255, then the 
  207.      * result is:
  208.      * <ul><code>
  209.      *     (byte)(b)
  210.      * </code></ul>
  211.      * <p>
  212.      * This method blocks until either the byte is read, the end of the 
  213.      * stream is detected, or an exception is thrown. 
  214.      *
  215.      * @return     the next byte of this input stream as a signed 8-bit
  216.      *             <code>byte</code>.
  217.      * @exception  EOFException  if this input stream has reached the end.
  218.      * @exception  IOException   if an I/O error occurs.
  219.      * @see        java.io.FilterInputStream#in
  220.      */
  221.     public final byte readByte() throws IOException {
  222.     int ch = in.read();
  223.     if (ch < 0)
  224.         throw new EOFException();
  225.     return (byte)(ch);
  226.     }
  227.  
  228.     /**
  229.      * Reads an unsigned 8-bit number from this data input stream. This 
  230.      * method reads a byte from this data input stream's underlying input 
  231.      * stream and returns that byte. This method blocks until the byte is 
  232.      * read, the end of the stream is detected, or an exception is thrown.
  233.      *
  234.      * @return     the next byte of this input stream, interpreted as an
  235.      *             unsigned 8-bit number.
  236.      * @exception  EOFException  if this input stream has reached the end.
  237.      * @exception  IOException   if an I/O error occurs.
  238.      * @see         java.io.FilterInputStream#in
  239.      */
  240.     public final int readUnsignedByte() throws IOException {
  241.     int ch = in.read();
  242.     if (ch < 0)
  243.         throw new EOFException();
  244.     return ch;
  245.     }
  246.  
  247.     /**
  248.      * Reads a signed 16-bit number from this data input stream. The 
  249.      * method reads two bytes from the underlying input stream. If the two
  250.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  251.      * where each of the two values is between <code>0</code> and 
  252.      * <code>255</code>, inclusive, then the result is equal to:
  253.      * <ul><code>
  254.      *     (short)((b1 << 8) | b2)
  255.      * </code></ul>
  256.      * <p>
  257.      * This method blocks until the two bytes are read, the end of the 
  258.      * stream is detected, or an exception is thrown. 
  259.      *
  260.      * @return     the next two bytes of this input stream, interpreted as a
  261.      *             signed 16-bit number.
  262.      * @exception  EOFException  if this input stream reaches the end before
  263.      *               reading two bytes.
  264.      * @exception  IOException   if an I/O error occurs.
  265.      * @see        java.io.FilterInputStream#in
  266.      */
  267.     public final short readShort() throws IOException {
  268.     InputStream in = this.in;
  269.     int ch1 = in.read();
  270.     int ch2 = in.read();
  271.     if ((ch1 | ch2) < 0)
  272.          throw new EOFException();
  273.     return (short)((ch1 << 8) + (ch2 << 0));
  274.     }
  275.  
  276.     /**
  277.      * Reads an unsigned 16-bit number from this data input stream. This 
  278.      * method reads two bytes from the underlying input stream. If the 
  279.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  280.      * where <code>0 <= b1</code>, 
  281.      * <code>b2 <= 255</code>, then the result is equal to:
  282.      * <ul><code>
  283.      *     (b1 << 8) | b2
  284.      * </code></ul>
  285.      * <p>
  286.      * This method blocks until the two bytes are read, the end of the 
  287.      * stream is detected, or an exception is thrown. 
  288.      *
  289.      * @return     the next two bytes of this input stream, interpreted as an
  290.      *             unsigned 16-bit integer.
  291.      * @exception  EOFException  if this input stream reaches the end before
  292.      *               reading two bytes.
  293.      * @exception  IOException   if an I/O error occurs.
  294.      * @see        java.io.FilterInputStream#in
  295.      */
  296.     public final int readUnsignedShort() throws IOException {
  297.     InputStream in = this.in;
  298.     int ch1 = in.read();
  299.     int ch2 = in.read();
  300.     if ((ch1 | ch2) < 0)
  301.          throw new EOFException();
  302.     return (ch1 << 8) + (ch2 << 0);
  303.     }
  304.  
  305.     /**
  306.      * Reads a Unicode character from this data input stream. This 
  307.      * method reads two bytes from the underlying input stream. If the 
  308.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  309.      * where 0 <= <code>b1</code>, 
  310.      * <code>b1</code> <= 255, then the result is equal to:
  311.      * <ul><code>
  312.      *     (char)((b1 << 8) | b2)
  313.      * </code></ul>
  314.      * <p>
  315.      * This method blocks until either the two bytes are read, the end of 
  316.      * the stream is detected, or an exception is thrown. 
  317.      *
  318.      * @return     the next two bytes of this input stream as a Unicode
  319.      *             character.
  320.      * @exception  EOFException  if this input stream reaches the end before
  321.      *               reading two bytes.
  322.      * @exception  IOException   if an I/O error occurs.
  323.      * @see        java.io.FilterInputStream#in
  324.      */
  325.     public final char readChar() throws IOException {
  326.     InputStream in = this.in;
  327.     int ch1 = in.read();
  328.     int ch2 = in.read();
  329.     if ((ch1 | ch2) < 0)
  330.          throw new EOFException();
  331.     return (char)((ch1 << 8) + (ch2 << 0));
  332.     }
  333.  
  334.     /**
  335.      * Reads a signed 32-bit integer from this data input stream. This 
  336.      * method reads four bytes from the underlying input stream. If the 
  337.      * bytes read, in order, are <code>b1</code>, <code>b2</code>, 
  338.      * <code>b3</code>, and <code>b4</code>, where 
  339.      * 0 <= <code>b1</code>, <code>b2</code>, 
  340.      * <code>b3</code>, <code>b4</code> <= 255, then the 
  341.      * result is equal to:
  342.      * <ul><code>
  343.      *     (b1 << 24) | (b2 << 16) + (b3 << 8) +b4
  344.      * </code></ul>
  345.      * <p>
  346.      * This method blocks until the four bytes are read, the end of the 
  347.      * stream is detected, or an exception is thrown. 
  348.      *
  349.      * @return     the next four bytes of this input stream, interpreted as an
  350.      *             <code>int</code>.
  351.      * @exception  EOFException  if this input stream reaches the end before
  352.      *               reading four bytes.
  353.      * @exception  IOException   if an I/O error occurs.
  354.      * @see        java.io.FilterInputStream#in
  355.      */
  356.     public final int readInt() throws IOException {
  357.     InputStream in = this.in;
  358.     int ch1 = in.read();
  359.     int ch2 = in.read();
  360.     int ch3 = in.read();
  361.     int ch4 = in.read();
  362.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  363.          throw new EOFException();
  364.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  365.     }
  366.  
  367.     /**
  368.      * Reads a signed 64-bit integer from this data input stream. This 
  369.      * method reads eight bytes from the underlying input stream. If the 
  370.      * bytes read, in order, are <code>b1</code>, <code>b2</code>, 
  371.      * <code>b3</code>, <code>b4</code>, <code>b5</code>, 
  372.      * <code>b6</code>, <code>b7</code>, and <code>b8</code>, where 
  373.      * <ul><code>
  374.      *     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255,
  375.      * </code></ul>
  376.      * <p>
  377.      * then the result is equal to:
  378.      * <p><blockquote><pre>
  379.      *     ((long)b1 << 56) + ((long)b2 << 48) +
  380.      *        ((long)b3 << 40) + ((long)b4 << 32) +
  381.      *        ((long)b5 << 24) + (b6 << 16) +
  382.      *        (b7 << 8) + b8
  383.      * </pre></blockquote>
  384.      * <p>
  385.      * This method blocks until the eight bytes are read, the end of the 
  386.      * stream is detected, or an exception is thrown. 
  387.      *
  388.      * @return     the next eight bytes of this input stream, interpreted as a
  389.      *             <code>long</code>.
  390.      * @exception  EOFException  if this input stream reaches the end before
  391.      *               reading eight bytes.
  392.      * @exception  IOException   if an I/O error occurs.
  393.      * @see        java.io.FilterInputStream#in
  394.      */
  395.     public final long readLong() throws IOException {
  396.     InputStream in = this.in;
  397.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  398.     }
  399.  
  400.     /**
  401.      * Reads a <code>float</code> from this data input stream. This 
  402.      * method reads an <code>int</code> value as if by the 
  403.      * <code>readInt</code> method and then converts that 
  404.      * <code>int</code> to a <code>float</code> using the 
  405.      * <code>intBitsToFloat</code> method in class <code>Float</code>. 
  406.      * This method blocks until the four bytes are read, the end of the 
  407.      * stream is detected, or an exception is thrown. 
  408.      *
  409.      * @return     the next four bytes of this input stream, interpreted as a
  410.      *             <code>float</code>.
  411.      * @exception  EOFException  if this input stream reaches the end before
  412.      *               reading four bytes.
  413.      * @exception  IOException   if an I/O error occurs.
  414.      * @see        java.io.DataInputStream#readInt()
  415.      * @see        java.lang.Float#intBitsToFloat(int)
  416.      */
  417.     public final float readFloat() throws IOException {
  418.     return Float.intBitsToFloat(readInt());
  419.     }
  420.  
  421.     /**
  422.      * Reads a <code>double</code> from this data input stream. This 
  423.      * method reads a <code>long</code> value as if by the 
  424.      * <code>readLong</code> method and then converts that 
  425.      * <code>long</code> to a <code>double</code> using the 
  426.      * <code>longBitsToDouble</code> method in class <code>Double</code>.
  427.      * <p>
  428.      * This method blocks until the eight bytes are read, the end of the 
  429.      * stream is detected, or an exception is thrown. 
  430.      *
  431.      * @return     the next eight bytes of this input stream, interpreted as a
  432.      *             <code>double</code>.
  433.      * @exception  EOFException  if this input stream reaches the end before
  434.      *               reading eight bytes.
  435.      * @exception  IOException   if an I/O error occurs.
  436.      * @see        java.io.DataInputStream#readLong()
  437.      * @see        java.lang.Double#longBitsToDouble(long)
  438.      */
  439.     public final double readDouble() throws IOException {
  440.     return Double.longBitsToDouble(readLong());
  441.     }
  442.  
  443.     private char lineBuffer[];
  444.  
  445.     /**
  446.      * Reads the next line of text from this data input stream. This 
  447.      * method successively reads bytes from the underlying input stream 
  448.      * until it reaches the end of a line of text. 
  449.      * <p>
  450.      * A line of text is terminated by a carriage return character 
  451.      * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a 
  452.      * carriage return character immediately followed by a newline 
  453.      * character, or the end of the input stream. The line-terminating 
  454.      * character(s), if any, are not returned as part of the string that 
  455.      * is returned. 
  456.      * <p>
  457.      * This method blocks until a newline character is read, a carriage 
  458.      * return and the byte following it are read (to see if it is a 
  459.      * newline), the end of the stream is detected, or an exception is 
  460.      * thrown.
  461.      *
  462.      * @deprecated This method does not properly convert bytes to characters.
  463.      * As of JDK 1.1, the preferred way to read lines of text is via the
  464.      * <code>BufferedReader.readLine()</code> method.  Programs that use the
  465.      * <code>DataInputStream</code> class to read lines can be converted to use
  466.      * the <code>BufferedReader</code> class by replacing code of the form
  467.      * <ul>
  468.      *     <code>DataInputStream d = new DataInputStream(in);</code>
  469.      * </ul>
  470.      * with
  471.      * <ul>
  472.      *     <code>BufferedReader d
  473.      *          = new BufferedReader(new InputStreamReader(in));
  474.      *      </code>
  475.      * </ul>
  476.      *
  477.      * @return     the next line of text from this input stream, or 
  478.      *             <tt>null</tt> if no bytes are read before end-of-file 
  479.      *             is reached.
  480.      * @exception  IOException  if an I/O error occurs.
  481.      * @see        java.io.BufferedReader#readLine()
  482.      * @see        java.io.FilterInputStream#in
  483.      */
  484.     public final String readLine() throws IOException {
  485.     InputStream in = this.in;
  486.     char buf[] = lineBuffer;
  487.  
  488.     if (buf == null) {
  489.         buf = lineBuffer = new char[128];
  490.     }
  491.  
  492.     int room = buf.length;
  493.     int offset = 0;
  494.     int c;
  495.  
  496. loop:    while (true) {
  497.         switch (c = in.read()) {
  498.           case -1: 
  499.           case '\n':
  500.         break loop;
  501.  
  502.           case '\r':
  503.         int c2 = in.read();
  504.         if (c2 != '\n') {
  505.             if (!(in instanceof PushbackInputStream)) {
  506.             in = this.in = new PushbackInputStream(in);
  507.             }
  508.             ((PushbackInputStream)in).unread(c2);
  509.         }
  510.         break loop;
  511.  
  512.           default:
  513.         if (--room < 0) {
  514.             buf = new char[offset + 128];
  515.             room = buf.length - offset - 1;
  516.             System.arraycopy(lineBuffer, 0, buf, 0, offset);
  517.             lineBuffer = buf;
  518.         }
  519.         buf[offset++] = (char) c;
  520.         break;
  521.         }
  522.     }
  523.     if ((c == -1) && (offset == 0)) {
  524.         return null;
  525.     }
  526.     return String.copyValueOf(buf, 0, offset);
  527.     }
  528.  
  529.     /**
  530.      * Reads in a string that has been encoded using a modified UTF-8 
  531.      * format from this data input stream. This method calls 
  532.      * <code>readUTF(this)</code>.
  533.      * See <code>readUTF(java.io.DataInput)</code> for a more 
  534.      * complete description of the format. 
  535.      * <p>
  536.      * This method blocks until all the bytes are read, the end of the 
  537.      * stream is detected, or an exception is thrown. 
  538.      *
  539.      * @return     a Unicode string.
  540.      * @exception  EOFException  if this input stream reaches the end before
  541.      *               reading all the bytes.
  542.      * @exception  IOException   if an I/O error occurs.
  543.      * @see        java.io.DataInputStream#readUTF(java.io.DataInput)
  544.      */
  545.     public final String readUTF() throws IOException {
  546.         return readUTF(this);
  547.     }
  548.  
  549.     /**
  550.      * Reads in a string from the specified data input stream. The 
  551.      * string has been encoded using a modified UTF-8 format. 
  552.      * <p>
  553.      * The first two bytes are read as if by 
  554.      * <code>readUnsignedShort</code>. This value gives the number of 
  555.      * following bytes that are in the encoded string, not
  556.      * the length of the resulting string. The following bytes are then 
  557.      * interpreted as bytes encoding characters in the UTF-8 format 
  558.      * and are converted into characters. 
  559.      * <p>
  560.      * This method blocks until all the bytes are read, the end of the 
  561.      * stream is detected, or an exception is thrown. 
  562.      *
  563.      * @param      in   a data input stream.
  564.      * @return     a Unicode string.
  565.      * @exception  EOFException            if the input stream reaches the end
  566.      *               before all the bytes.
  567.      * @exception  IOException             if an I/O error occurs.
  568.      * @exception  UTFDataFormatException  if the bytes do not represent a
  569.      *               valid UTF-8 encoding of a Unicode string.
  570.      * @see        java.io.DataInputStream#readUnsignedShort()
  571.      */
  572.     public final static String readUTF(DataInput in) throws IOException {
  573.         int utflen = in.readUnsignedShort();
  574.         char str[] = new char[utflen];
  575.     int count = 0;
  576.     int strlen = 0;
  577.     while (count < utflen) {
  578.         int c = in.readUnsignedByte();
  579.         int char2, char3;
  580.         switch (c >> 4) { 
  581.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  582.             // 0xxxxxxx
  583.             count++;
  584.             str[strlen++] = (char)c;
  585.             break;
  586.             case 12: case 13:
  587.             // 110x xxxx   10xx xxxx
  588.             count += 2;
  589.             if (count > utflen) 
  590.             throw new UTFDataFormatException();          
  591.             char2 = in.readUnsignedByte();
  592.             if ((char2 & 0xC0) != 0x80)
  593.             throw new UTFDataFormatException();          
  594.             str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  595.             break;
  596.             case 14:
  597.             // 1110 xxxx  10xx xxxx  10xx xxxx
  598.             count += 3;
  599.             if (count > utflen) 
  600.             throw new UTFDataFormatException();          
  601.             char2 = in.readUnsignedByte();
  602.             char3 = in.readUnsignedByte();
  603.             if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  604.             throw new UTFDataFormatException();          
  605.             str[strlen++] = (char)(((c & 0x0F) << 12) |
  606.                        ((char2 & 0x3F) << 6) |
  607.                        ((char3 & 0x3F) << 0));
  608.             break;
  609.             default:
  610.             // 10xx xxxx,  1111 xxxx
  611.             throw new UTFDataFormatException();          
  612.         }
  613.     }
  614.         return new String(str, 0, strlen);
  615.     }
  616. }
  617.