home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / DataInputStream.java < prev    next >
Text File  |  1998-01-23  |  25KB  |  625 lines

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