home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / io / DataInputStream.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  23.9 KB  |  623 lines

  1. /*
  2.  * @(#)DataInputStream.java    1.43 98/03/18
  3.  *
  4.  * Copyright 1994-1997 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.43, 03/18/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.      * Makes an attempt to skip over <code>n</code> bytes of data in the 
  167.      * underlying input stream, discarding the skipped bytes.  However,
  168.      * it may skip over some smaller number of bytes, possibly zero.  
  169.      * This may result from any of a number of conditions; reaching end of
  170.      * file before n bytes have been skipped is one possibility.  This 
  171.      * method never throws an <code>EOFException</code>.  The actual number
  172.      * of bytes skipped is returned.  If <code>n</code> is negative, no bytes
  173.      * are skipped.
  174.      * 
  175.      * @param      n   the number of bytes to be skipped.
  176.      * @return     the actual number of bytes skipped.
  177.      * @exception  IOException   if an I/O error occurs.
  178.      */
  179.     public final int skipBytes(int n) throws IOException {
  180.     InputStream in = this.in;
  181.     int total = 0;
  182.     int cur = 0;
  183.  
  184.     while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
  185.         total += cur;
  186.     }
  187.     
  188.     return total;
  189.     }
  190.  
  191.     /**
  192.      * Reads a <code>boolean</code> from this data input stream. This 
  193.      * method reads a single byte from the underlying input stream. A 
  194.      * value of <code>0</code> represents <code>false</code>. Any other 
  195.      * value represents <code>true</code>. This method blocks until 
  196.      * either the byte is read, the end of the stream is detected, or an 
  197.      * exception is thrown. 
  198.      *
  199.      * @return     the <code>boolean</code> value read.
  200.      * @exception  EOFException  if this input stream has reached the end.
  201.      * @exception  IOException   if an I/O error occurs.
  202.      * @see        java.io.FilterInputStream#in
  203.      */
  204.     public final boolean readBoolean() throws IOException {
  205.     int ch = in.read();
  206.     if (ch < 0)
  207.         throw new EOFException();
  208.     return (ch != 0);
  209.     }
  210.  
  211.     /**
  212.      * Reads a signed 8-bit value from this data input stream. This 
  213.      * method reads a byte from the underlying input stream. If the byte 
  214.      * read is <code>b</code>, where 
  215.      * 0 <= <code>b</code> <= 255, then the 
  216.      * result is:
  217.      * <blockquote><pre>
  218.      *     (byte)(b)
  219.      * </pre></blockquote>
  220.      * <p>
  221.      * This method blocks until either the byte is read, the end of the 
  222.      * stream is detected, or an exception is thrown. 
  223.      *
  224.      * @return     the next byte of this input stream as a signed 8-bit
  225.      *             <code>byte</code>.
  226.      * @exception  EOFException  if this input stream has reached the end.
  227.      * @exception  IOException   if an I/O error occurs.
  228.      * @see        java.io.FilterInputStream#in
  229.      */
  230.     public final byte readByte() throws IOException {
  231.     int ch = in.read();
  232.     if (ch < 0)
  233.         throw new EOFException();
  234.     return (byte)(ch);
  235.     }
  236.  
  237.     /**
  238.      * Reads an unsigned 8-bit number from this data input stream. This 
  239.      * method reads a byte from this data input stream's underlying input 
  240.      * stream and returns that byte. This method blocks until the byte is 
  241.      * read, the end of the stream is detected, or an exception is thrown.
  242.      *
  243.      * @return     the next byte of this input stream, interpreted as an
  244.      *             unsigned 8-bit number.
  245.      * @exception  EOFException  if this input stream has reached the end.
  246.      * @exception  IOException   if an I/O error occurs.
  247.      * @see         java.io.FilterInputStream#in
  248.      */
  249.     public final int readUnsignedByte() throws IOException {
  250.     int ch = in.read();
  251.     if (ch < 0)
  252.         throw new EOFException();
  253.     return ch;
  254.     }
  255.  
  256.     /**
  257.      * Reads a signed 16-bit number from this data input stream. The 
  258.      * method reads two bytes from the underlying input stream. If the two
  259.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  260.      * where each of the two values is between <code>0</code> and 
  261.      * <code>255</code>, inclusive, then the result is equal to:
  262.      * <blockquote><pre>
  263.      *     (short)((b1 << 8) | b2)
  264.      * </pre></blockquote>
  265.      * <p>
  266.      * This method blocks until the two bytes are read, the end of the 
  267.      * stream is detected, or an exception is thrown. 
  268.      *
  269.      * @return     the next two bytes of this input stream, interpreted as a
  270.      *             signed 16-bit number.
  271.      * @exception  EOFException  if this input stream reaches the end before
  272.      *               reading two bytes.
  273.      * @exception  IOException   if an I/O error occurs.
  274.      * @see        java.io.FilterInputStream#in
  275.      */
  276.     public final short readShort() throws IOException {
  277.     InputStream in = this.in;
  278.     int ch1 = in.read();
  279.     int ch2 = in.read();
  280.     if ((ch1 | ch2) < 0)
  281.          throw new EOFException();
  282.     return (short)((ch1 << 8) + (ch2 << 0));
  283.     }
  284.  
  285.     /**
  286.      * Reads an unsigned 16-bit number from this data input stream. This 
  287.      * method reads two bytes from the underlying input stream. If the 
  288.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  289.      * where <code>0 <= b1</code>, 
  290.      * <code>b2 <= 255</code>, then the result is equal to:
  291.      * <blockquote><pre>
  292.      *     (b1 << 8) | b2
  293.      * </pre></blockquote>
  294.      * <p>
  295.      * This method blocks until the two bytes are read, the end of the 
  296.      * stream is detected, or an exception is thrown. 
  297.      *
  298.      * @return     the next two bytes of this input stream, interpreted as an
  299.      *             unsigned 16-bit integer.
  300.      * @exception  EOFException  if this input stream reaches the end before
  301.      *               reading two bytes.
  302.      * @exception  IOException   if an I/O error occurs.
  303.      * @see        java.io.FilterInputStream#in
  304.      */
  305.     public final int readUnsignedShort() throws IOException {
  306.     InputStream in = this.in;
  307.     int ch1 = in.read();
  308.     int ch2 = in.read();
  309.     if ((ch1 | ch2) < 0)
  310.          throw new EOFException();
  311.     return (ch1 << 8) + (ch2 << 0);
  312.     }
  313.  
  314.     /**
  315.      * Reads a Unicode character from this data input stream. This 
  316.      * method reads two bytes from the underlying input stream. If the 
  317.      * bytes read, in order, are <code>b1</code> and <code>b2</code>, 
  318.      * where 0 <= <code>b1</code>, 
  319.      * <code>b1</code> <= 255, then the result is equal to:
  320.      * <blockquote><pre>
  321.      *     (char)((b1 << 8) | b2)
  322.      * </pre></blockquote>
  323.      * <p>
  324.      * This method blocks until either the two bytes are read, the end of 
  325.      * the stream is detected, or an exception is thrown. 
  326.      *
  327.      * @return     the next two bytes of this input stream as a Unicode
  328.      *             character.
  329.      * @exception  EOFException  if this input stream reaches the end before
  330.      *               reading two bytes.
  331.      * @exception  IOException   if an I/O error occurs.
  332.      * @see        java.io.FilterInputStream#in
  333.      */
  334.     public final char readChar() throws IOException {
  335.     InputStream in = this.in;
  336.     int ch1 = in.read();
  337.     int ch2 = in.read();
  338.     if ((ch1 | ch2) < 0)
  339.          throw new EOFException();
  340.     return (char)((ch1 << 8) + (ch2 << 0));
  341.     }
  342.  
  343.     /**
  344.      * Reads a signed 32-bit integer from this data input stream. This 
  345.      * method reads four bytes from the underlying input stream. If the 
  346.      * bytes read, in order, are <code>b1</code>, <code>b2</code>, 
  347.      * <code>b3</code>, and <code>b4</code>, where 
  348.      * 0 <= <code>b1</code>, <code>b2</code>, 
  349.      * <code>b3</code>, <code>b4</code> <= 255, then the 
  350.      * result is equal to:
  351.      * <blockquote><pre>
  352.      *     (b1 << 24) | (b2 << 16) + (b3 << 8) +b4
  353.      * </pre></blockquote>
  354.      * <p>
  355.      * This method blocks until the four bytes are read, the end of the 
  356.      * stream is detected, or an exception is thrown. 
  357.      *
  358.      * @return     the next four bytes of this input stream, interpreted as an
  359.      *             <code>int</code>.
  360.      * @exception  EOFException  if this input stream reaches the end before
  361.      *               reading four bytes.
  362.      * @exception  IOException   if an I/O error occurs.
  363.      * @see        java.io.FilterInputStream#in
  364.      */
  365.     public final int readInt() throws IOException {
  366.     InputStream in = this.in;
  367.     int ch1 = in.read();
  368.     int ch2 = in.read();
  369.     int ch3 = in.read();
  370.     int ch4 = in.read();
  371.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  372.          throw new EOFException();
  373.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  374.     }
  375.  
  376.     /**
  377.      * Reads a signed 64-bit integer from this data input stream. This 
  378.      * method reads eight bytes from the underlying input stream. If the 
  379.      * bytes read, in order, are <code>b1</code>, <code>b2</code>, 
  380.      * <code>b3</code>, <code>b4</code>, <code>b5</code>, 
  381.      * <code>b6</code>, <code>b7</code>, and <code>b8</code>, where 
  382.      * <blockquote><pre>
  383.      *     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <= 255,
  384.      * </pre></blockquote>
  385.      * <p>
  386.      * then the result is equal to:
  387.      * <p><blockquote><pre>
  388.      *     ((long)b1 << 56) + ((long)b2 << 48) +
  389.      *        ((long)b3 << 40) + ((long)b4 << 32) +
  390.      *        ((long)b5 << 24) + (b6 << 16) +
  391.      *        (b7 << 8) + b8
  392.      * </pre></blockquote>
  393.      * <p>
  394.      * This method blocks until the eight bytes are read, the end of the 
  395.      * stream is detected, or an exception is thrown. 
  396.      *
  397.      * @return     the next eight bytes of this input stream, interpreted as a
  398.      *             <code>long</code>.
  399.      * @exception  EOFException  if this input stream reaches the end before
  400.      *               reading eight bytes.
  401.      * @exception  IOException   if an I/O error occurs.
  402.      * @see        java.io.FilterInputStream#in
  403.      */
  404.     public final long readLong() throws IOException {
  405.     InputStream in = this.in;
  406.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  407.     }
  408.  
  409.     /**
  410.      * Reads a <code>float</code> from this data input stream. This 
  411.      * method reads an <code>int</code> value as if by the 
  412.      * <code>readInt</code> method and then converts that 
  413.      * <code>int</code> to a <code>float</code> using the 
  414.      * <code>intBitsToFloat</code> method in class <code>Float</code>. 
  415.      * This method blocks until the four bytes are read, the end of the 
  416.      * stream is detected, or an exception is thrown. 
  417.      *
  418.      * @return     the next four bytes of this input stream, interpreted as a
  419.      *             <code>float</code>.
  420.      * @exception  EOFException  if this input stream reaches the end before
  421.      *               reading four bytes.
  422.      * @exception  IOException   if an I/O error occurs.
  423.      * @see        java.io.DataInputStream#readInt()
  424.      * @see        java.lang.Float#intBitsToFloat(int)
  425.      */
  426.     public final float readFloat() throws IOException {
  427.     return Float.intBitsToFloat(readInt());
  428.     }
  429.  
  430.     /**
  431.      * Reads a <code>double</code> from this data input stream. This 
  432.      * method reads a <code>long</code> value as if by the 
  433.      * <code>readLong</code> method and then converts that 
  434.      * <code>long</code> to a <code>double</code> using the 
  435.      * <code>longBitsToDouble</code> method in class <code>Double</code>.
  436.      * <p>
  437.      * This method blocks until the eight bytes are read, the end of the 
  438.      * stream is detected, or an exception is thrown. 
  439.      *
  440.      * @return     the next eight bytes of this input stream, interpreted as a
  441.      *             <code>double</code>.
  442.      * @exception  EOFException  if this input stream reaches the end before
  443.      *               reading eight bytes.
  444.      * @exception  IOException   if an I/O error occurs.
  445.      * @see        java.io.DataInputStream#readLong()
  446.      * @see        java.lang.Double#longBitsToDouble(long)
  447.      */
  448.     public final double readDouble() throws IOException {
  449.     return Double.longBitsToDouble(readLong());
  450.     }
  451.  
  452.     private char lineBuffer[];
  453.  
  454.     /**
  455.      * Reads the next line of text from this data input stream. This 
  456.      * method successively reads bytes from the underlying input stream 
  457.      * until it reaches the end of a line of text. 
  458.      * <p>
  459.      * A line of text is terminated by a carriage return character 
  460.      * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a 
  461.      * carriage return character immediately followed by a newline 
  462.      * character, or the end of the input stream. The line-terminating 
  463.      * character(s), if any, are not returned as part of the string that 
  464.      * is returned. 
  465.      * <p>
  466.      * This method blocks until a newline character is read, a carriage 
  467.      * return and the byte following it are read (to see if it is a 
  468.      * newline), the end of the stream is detected, or an exception is 
  469.      * thrown.
  470.      *
  471.      * @deprecated This method does not properly convert bytes to characters.
  472.      * As of JDK 1.1, the preferred way to read lines of text is via the
  473.      * <code>BufferedReader.readLine()</code> method.  Programs that use the
  474.      * <code>DataInputStream</code> class to read lines can be converted to use
  475.      * the <code>BufferedReader</code> class by replacing code of the form:
  476.      * <blockquote><pre>
  477.      *     DataInputStream d = new DataInputStream(in);
  478.      * </pre></blockquote>
  479.      * with:
  480.      * <blockquote><pre>
  481.      *     BufferedReader d
  482.      *          = new BufferedReader(new InputStreamReader(in));
  483.      * </pre></blockquote>
  484.      *
  485.      * @return     the next line of text from this input stream.
  486.      * @exception  IOException  if an I/O error occurs.
  487.      * @see        java.io.BufferedReader#readLine()
  488.      * @see        java.io.FilterInputStream#in
  489.      */
  490.     public final String readLine() throws IOException {
  491.     InputStream in = this.in;
  492.     char buf[] = lineBuffer;
  493.  
  494.     if (buf == null) {
  495.         buf = lineBuffer = new char[128];
  496.     }
  497.  
  498.     int room = buf.length;
  499.     int offset = 0;
  500.     int c;
  501.  
  502. loop:    while (true) {
  503.         switch (c = in.read()) {
  504.           case -1: 
  505.           case '\n':
  506.         break loop;
  507.  
  508.           case '\r':
  509.         int c2 = in.read();
  510.         if (c2 != '\n') {
  511.             if (!(in instanceof PushbackInputStream)) {
  512.             in = this.in = new PushbackInputStream(in);
  513.             }
  514.             ((PushbackInputStream)in).unread(c2);
  515.         }
  516.         break loop;
  517.  
  518.           default:
  519.         if (--room < 0) {
  520.             buf = new char[offset + 128];
  521.             room = buf.length - offset - 1;
  522.             System.arraycopy(lineBuffer, 0, buf, 0, offset);
  523.             lineBuffer = buf;
  524.         }
  525.         buf[offset++] = (char) c;
  526.         break;
  527.         }
  528.     }
  529.     if ((c == -1) && (offset == 0)) {
  530.         return null;
  531.     }
  532.     return String.copyValueOf(buf, 0, offset);
  533.     }
  534.  
  535.     /**
  536.      * Reads in a string that has been encoded using a modified UTF-8 
  537.      * format from this data input stream. This method calls 
  538.      * <code>readUTF(this)</code>.
  539.      * See <code>readUTF(java.io.DataInput)</code> for a more 
  540.      * complete description of the format. 
  541.      * <p>
  542.      * This method blocks until all the bytes are read, the end of the 
  543.      * stream is detected, or an exception is thrown. 
  544.      *
  545.      * @return     a Unicode string.
  546.      * @exception  EOFException  if this input stream reaches the end before
  547.      *               reading all the bytes.
  548.      * @exception  IOException   if an I/O error occurs.
  549.      * @see        java.io.DataInputStream#readUTF(java.io.DataInput)
  550.      */
  551.     public final String readUTF() throws IOException {
  552.         return readUTF(this);
  553.     }
  554.  
  555.     /**
  556.      * Reads in a string from the specified data input stream. The 
  557.      * string has been encoded using a modified UTF-8 format. 
  558.      * <p>
  559.      * The first two bytes are read as if by 
  560.      * <code>readUnsignedShort</code>. This value gives the number of 
  561.      * following bytes that are in the encoded string, not
  562.      * the length of the resulting string. The following bytes are then 
  563.      * interpreted as bytes encoding characters in the UTF-8 format 
  564.      * and are converted into characters. 
  565.      * <p>
  566.      * This method blocks until all the bytes are read, the end of the 
  567.      * stream is detected, or an exception is thrown. 
  568.      *
  569.      * @param      in   a data input stream.
  570.      * @return     a Unicode string.
  571.      * @exception  EOFException            if the input stream reaches the end
  572.      *               before all the bytes.
  573.      * @exception  IOException             if an I/O error occurs.
  574.      * @exception  UTFDataFormatException  if the bytes do not represent a
  575.      *               valid UTF-8 encoding of a Unicode string.
  576.      * @see        java.io.DataInputStream#readUnsignedShort()
  577.      */
  578.     public final static String readUTF(DataInput in) throws IOException {
  579.         int utflen = in.readUnsignedShort();
  580.         char str[] = new char[utflen];
  581.     int count = 0;
  582.     int strlen = 0;
  583.     while (count < utflen) {
  584.         int c = in.readUnsignedByte();
  585.         int char2, char3;
  586.         switch (c >> 4) { 
  587.             case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  588.             // 0xxxxxxx
  589.             count++;
  590.             str[strlen++] = (char)c;
  591.             break;
  592.             case 12: case 13:
  593.             // 110x xxxx   10xx xxxx
  594.             count += 2;
  595.             if (count > utflen) 
  596.             throw new UTFDataFormatException();          
  597.             char2 = in.readUnsignedByte();
  598.             if ((char2 & 0xC0) != 0x80)
  599.             throw new UTFDataFormatException();          
  600.             str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
  601.             break;
  602.             case 14:
  603.             // 1110 xxxx  10xx xxxx  10xx xxxx
  604.             count += 3;
  605.             if (count > utflen) 
  606.             throw new UTFDataFormatException();          
  607.             char2 = in.readUnsignedByte();
  608.             char3 = in.readUnsignedByte();
  609.             if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
  610.             throw new UTFDataFormatException();          
  611.             str[strlen++] = (char)(((c & 0x0F) << 12) |
  612.                        ((char2 & 0x3F) << 6) |
  613.                        ((char3 & 0x3F) << 0));
  614.             break;
  615.             default:
  616.             // 10xx xxxx,  1111 xxxx
  617.             throw new UTFDataFormatException();          
  618.         }
  619.     }
  620.         return new String(str, 0, strlen);
  621.     }
  622. }
  623.