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

  1. /*
  2.  * @(#)DataInput.java    1.12 98/03/18
  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.  * The <code>DataInput</code> interface provides
  19.  * for reading bytes from a binary stream and
  20.  * reconstructing from them data in any of
  21.  * the Java primitive types. There is also 
  22.  * a 
  23.  * facility for reconstructing a <code>String</code>
  24.  * from data in Java modified UTF-8 format.
  25.  * <p>
  26.  * It is generally true of all the reading
  27.  * routines in this interface that if end of
  28.  * file is reached before the desired number
  29.  * of bytes has been read, an <code>EOFException</code>
  30.  * (which is a kind of <code>IOException</code>)
  31.  * is thrown. If any byte cannot be read for
  32.  * any reason other than end of file, an <code>IOException</code>
  33.  * other than <code>EOFException</code> is 
  34.  * thrown. In particular, an <code>IOException</code>
  35.  * may be thrown if the input stream has been
  36.  * closed.
  37.  * 
  38.  * @author  Frank Yellin
  39.  * @version 1.12, 03/18/98
  40.  * @see     java.io.DataInputStream 
  41.  * @see     java.io.DataOutput  
  42.  * @since   JDK1.0
  43.  */
  44. public
  45. interface DataInput {
  46.     /**
  47.      * Reads some bytes from an input
  48.      * stream and stores them into the buffer 
  49.      * array <code>b</code>. The number of bytes
  50.      * read is equal 
  51.      * to the length of <code>b</code>.
  52.      * <p>
  53.      * This method blocks until one of the 
  54.      * following conditions occurs:<p>
  55.      * <ul>
  56.      * <li><code>b.length</code>
  57.      * bytes of input data are available, in which
  58.      * case a normal return is made.
  59.      * 
  60.      * <li>End of
  61.      * file is detected, in which case an <code>EOFException</code>
  62.      * is thrown.
  63.      * 
  64.      * <li>An I/O error occurs, in 
  65.      * which case an <code>IOException</code> other
  66.      * than <code>EOFException</code> is thrown.
  67.      * </ul>
  68.      * <p> 
  69.      * If <code>b</code> is <code>null</code>,
  70.      * a <code>NullPointerException</code> is thrown.
  71.      * If <code>b.length</code> is zero, then 
  72.      * no bytes are read. Otherwise, the first 
  73.      * byte read is stored into element <code>b[0]</code>,
  74.      * the next one into <code>b[1]</code>, and
  75.      * so on.
  76.      * If an exception is thrown from
  77.      * this method, then it may be that some but
  78.      * not all bytes of <code>b</code> have been
  79.      * updated with data from the input stream.
  80.      *
  81.      * @param     b   the buffer into which the data is read.
  82.      * @exception  EOFException  if this stream reaches the end before reading
  83.      *               all the bytes.
  84.      * @exception  IOException   if an I/O error occurs.
  85.      * @since      JDK1.0
  86.      */
  87.     void readFully(byte b[]) throws IOException;
  88.  
  89.     /**
  90.      *
  91.      * Reads <code>len</code>
  92.      * bytes from 
  93.      * an input stream.
  94.      * <p>
  95.      * This method
  96.      * blocks until one of the following conditions
  97.      * occurs:<p>
  98.      * <ul>
  99.      * <li><code>len</code> bytes
  100.      * of input data are available, in which case
  101.      * a normal return is made.
  102.      * 
  103.      * <li>End of file
  104.      * is detected, in which case an <code>EOFException</code>
  105.      * is thrown.
  106.      * 
  107.      * <li>An I/O error occurs, in 
  108.      * which case an <code>IOException</code> other
  109.      * than <code>EOFException</code> is thrown.
  110.      * </ul>
  111.      * <p>
  112.      * If <code>b</code> is <code>null</code>,
  113.      * a <code>NullPointerException</code> is thrown.
  114.      * If <code>off</code> is negative, or <code>len</code>
  115.      * is negative, or <code>off+len</code> is 
  116.      * greater than the length of the array <code>b</code>,
  117.      * then an <code>IndexOutOfBoundsException</code>
  118.      * is thrown.
  119.      * If <code>len</code> is zero,
  120.      * then no bytes are read. Otherwise, the first
  121.      * byte read is stored into element <code>b[off]</code>,
  122.      * the next one into <code>b[off+1]</code>,
  123.      * and so on. The number of bytes read is, 
  124.      * at most, equal to <code>len</code>.
  125.      * 
  126.      * @param     b   the buffer into which the data is read.
  127.      * @exception  EOFException  if this stream reaches the end before reading
  128.      *               all the bytes.
  129.      * @exception  IOException   if an I/O error occurs.
  130.      * @since   JDK1.0
  131.      */
  132.     void readFully(byte b[], int off, int len) throws IOException;
  133.  
  134.     /**
  135.      * Makes an attempt to skip over
  136.      * <code>n</code> bytes 
  137.      * of data from the input
  138.      * stream, discarding the skipped bytes. However,
  139.      * it may skip 
  140.      * over some smaller number of
  141.      * bytes, possibly zero. This may result from
  142.      * any of a 
  143.      * number of conditions; reaching
  144.      * end of file before <code>n</code> bytes 
  145.      * have been skipped is 
  146.      * only one possibility.
  147.      * This method never throws an <code>EOFException</code>.
  148.      * The actual 
  149.      * number of bytes skipped is returned.
  150.      *
  151.      * @param      n   the number of bytes to be skipped.
  152.      * @return     the number of bytes skipped, which is always <code>n</code>.
  153.      * @exception  EOFException  if this stream reaches the end before skipping
  154.      *               all the bytes.
  155.      * @exception  IOException   if an I/O error occurs.
  156.      * @since      JDK1.0
  157.      */
  158.     int skipBytes(int n) throws IOException;
  159.  
  160.     /**
  161.      * Reads one input byte and returns
  162.      * <code>true</code> if that byte is nonzero,
  163.      * <code>false</code> if that byte is zero.
  164.      * This method is suitable for reading 
  165.      * the byte written by the <code>writeBoolean</code>
  166.      * method of interface <code>DataOutput</code>.
  167.      * 
  168.      * @return     the <code>boolean</code> value read.
  169.      * @exception  EOFException  if this stream reaches the end before reading
  170.      *               all the bytes.
  171.      * @exception  IOException   if an I/O error occurs.
  172.      * @since      JDK1.0
  173.      */
  174.     boolean readBoolean() throws IOException;
  175.  
  176.     /**
  177.      * Reads and returns one input byte.
  178.      * The byte is treated as a signed value in
  179.      * the range <code>-128</code> through <code>127</code>,
  180.      * inclusive.
  181.      * This method is suitable for
  182.      * reading the byte written by the <code>writeByte</code>
  183.      * method of interface <code>DataOutput</code>.
  184.      *
  185.      * @return     the 8-bit value read.
  186.      * @exception  EOFException  if this stream reaches the end before reading
  187.      *               all the bytes.
  188.      * @exception  IOException   if an I/O error occurs.
  189.      * @since      JDK1.0
  190.      */
  191.     byte readByte() throws IOException;
  192.  
  193.     /**
  194.      * Reads one input byte, zero-extends
  195.      * it to type <code>int</code>, and returns
  196.      * the result, which is therefore in the range
  197.      * <code>0</code> 
  198.      * through <code>255</code>.
  199.      * This method is suitable for reading 
  200.      * the byte written by the <code>writeByte</code>
  201.      * method of interface <code>DataOutput</code>
  202.      * if the argument to <code>writeByte</code>
  203.      * was intended to be a value in the range 
  204.      * <code>0</code> through <code>255</code>.
  205.      *
  206.      * @return     the unsigned 8-bit value read.
  207.      * @exception  EOFException  if this stream reaches the end before reading
  208.      *               all the bytes.
  209.      * @exception  IOException   if an I/O error occurs.
  210.      * @since      JDK1.0
  211.      */
  212.     int readUnsignedByte() throws IOException;
  213.  
  214.     /**
  215.      * Reads two input bytes and returns
  216.      * a <code>short</code> value. Let <code>a</code>
  217.      * be the first byte read and <code>b</code>
  218.      * be the second byte. The value 
  219.      * returned 
  220.      * is:
  221.      * <p><pre><code>(short)((a << 8) * | (b & 0xff))
  222.      * </code></pre>
  223.      * This method
  224.      * is suitable for reading the bytes written
  225.      * by the <code>writeShort</code> method of
  226.      * interface <code>DataOutput</code>.
  227.      *
  228.      * @return     the 16-bit value read.
  229.      * @exception  EOFException  if this stream reaches the end before reading
  230.      *               all the bytes.
  231.      * @exception  IOException   if an I/O error occurs.
  232.      * @since      JDK1.0
  233.      */
  234.     short readShort() throws IOException;
  235.  
  236.     /**
  237.      * Reads two input bytes and returns
  238.      * an <code>int</code> value in the range <code>0</code>
  239.      * through <code>65535</code>. Let <code>a</code>
  240.      * be the first byte read and 
  241.      * <code>b</code>
  242.      * be the second byte. The value returned is:
  243.      * <p><pre><code>(((a & 0xff) << 8) | (b & 0xff))
  244.      * </code></pre>
  245.      * This method is suitable for reading the bytes
  246.      * written by the <code>writeShort</code> method
  247.      * of interface <code>DataOutput</code>  if
  248.      * the argument to <code>writeShort</code> 
  249.      * was intended to be a value in the range 
  250.      * <code>0</code> through <code>65535</code>.
  251.      *
  252.      * @return     the unsigned 16-bit value read.
  253.      * @exception  EOFException  if this stream reaches the end before reading
  254.      *               all the bytes.
  255.      * @exception  IOException   if an I/O error occurs.
  256.      * @since      JDK1.0
  257.      */
  258.     int readUnsignedShort() throws IOException;
  259.  
  260.     /**
  261.      * Reads an input <code>char</code> and returns the <code>char</code> value.
  262.      * A Unicode <code>char</code> is made up of two bytes.
  263.      * Let <code>a</code>
  264.      * be the first byte read and <code>b</code>
  265.      * be the second byte. The value 
  266.      * returned is:
  267.      * <p><pre><code>(char)((a << 8) | (b & 0xff))
  268.      * </code></pre>
  269.      * This method
  270.      * is suitable for reading bytes written by
  271.      * the <code>writeChar</code> method of interface
  272.      * <code>DataOutput</code>.
  273.      *
  274.      * @return     the Unicode <code>char</code> read.
  275.      * @exception  EOFException  if this stream reaches the end before reading
  276.      *               all the bytes.
  277.      * @exception  IOException   if an I/O error occurs.
  278.      * @since      JDK1.0
  279.      */
  280.     char readChar() throws IOException;
  281.  
  282.     /**
  283.      * Reads four input bytes and returns an
  284.      * <code>int</code> value. Let <code>a</code>
  285.      * be the first byte read, <code>b</code> be
  286.      * the second byte, <code>c</code> be the third
  287.      * byte, 
  288.      * and <code>d</code> be the fourth 
  289.      * byte. The value returned is:
  290.      * <p><pre>
  291.      * <code>
  292.      * (((a & 0xff) << 24) | ((b & 0xff) << 16) |
  293.      *  ((c & 0xff) << 8) | (d & 0xff))
  294.      * </code></pre>
  295.      * This method is suitable
  296.      * for reading bytes written by the <code>writeInt</code>
  297.      * method of interface <code>DataOutput</code>.
  298.      *
  299.      * @return     the <code>int</code> value read.
  300.      * @exception  EOFException  if this stream reaches the end before reading
  301.      *               all the bytes.
  302.      * @exception  IOException   if an I/O error occurs.
  303.      * @since      JDK1.0
  304.      */
  305.     int readInt() throws IOException;
  306.  
  307.     /**
  308.      * Reads eight input bytes and returns
  309.      * a <code>long</code> value. Let <code>a</code>
  310.      * be the first byte read, <code>b</code> be
  311.      * the second byte, <code>c</code> be the third
  312.      * byte, <code>d</code> 
  313.      * be the fourth byte,
  314.      * <code>e</code> be the fifth byte, <code>f</code>
  315.      * be the sixth byte, <code>g</code> be the
  316.      * seventh byte, 
  317.      * and <code>h</code> be the
  318.      * eighth byte. The value returned is:
  319.      * <p><pre> <code>
  320.      * (((long)(a & 0xff) << 56) |
  321.      *  ((long)(b & 0xff) << 48) |
  322.      *  ((long)(c & 0xff) << 40) |
  323.      *  ((long)(d & 0xff) << 32) |
  324.      *  ((long)(e & 0xff) << 24) |
  325.      *  ((long)(f & 0xff) << 16) |
  326.      *  ((long)(g & 0xff) <<  8) |
  327.      *  ((long)(h & 0xff)))
  328.      * </code></pre>
  329.      * <p>
  330.      * This method is suitable
  331.      * for reading bytes written by the <code>writeLong</code>
  332.      * method of interface <code>DataOutput</code>.
  333.      *
  334.      * @return     the <code>long</code> value read.
  335.      * @exception  EOFException  if this stream reaches the end before reading
  336.      *               all the bytes.
  337.      * @exception  IOException   if an I/O error occurs.
  338.      * @since      JDK1.0
  339.      */
  340.     long readLong() throws IOException;
  341.  
  342.     /**
  343.      * Reads four input bytes and returns
  344.      * a <code>float</code> value. It does this
  345.      * by first constructing an <code>int</code>
  346.      * value in exactly the manner 
  347.      * of the <code>readInt</code>
  348.      * method, then converting this <code>int</code>
  349.      * value to a <code>float</code> in 
  350.      * exactly the manner of the method <code>Float.intBitsToFloat</code>.
  351.      * This method is suitable for reading
  352.      * bytes written by the <code>writeFloat</code>
  353.      * method of interface <code>DataOutput</code>.
  354.      *
  355.      * @return     the <code>float</code> value read.
  356.      * @exception  EOFException  if this stream reaches the end before reading
  357.      *               all the bytes.
  358.      * @exception  IOException   if an I/O error occurs.
  359.      * @since      JDK1.0
  360.      */
  361.     float readFloat() throws IOException;
  362.  
  363.     /**
  364.      * Reads eight input bytes and returns
  365.      * a <code>double</code> value. It does this
  366.      * by first constructing a <code>long</code>
  367.      * value in exactly the manner 
  368.      * of the <code>readlong</code>
  369.      * method, then converting this <code>long</code>
  370.      * value to a <code>double</code> in exactly
  371.      * the manner of the method <code>Double.longBitsToDouble</code>.
  372.      * This method is suitable for reading
  373.      * bytes written by the <code>writeDouble</code>
  374.      * method of interface <code>DataOutput</code>.
  375.      *
  376.      * @return     the <code>double</code> value read.
  377.      * @exception  EOFException  if this stream reaches the end before reading
  378.      *               all the bytes.
  379.      * @exception  IOException   if an I/O error occurs.
  380.      * @since      JDK1.0
  381.      */
  382.     double readDouble() throws IOException;
  383.  
  384.     /**
  385.      * Reads the next line of text from the input stream. 
  386.      * It reads successive bytes, converting
  387.      * each byte separately into a character, 
  388.      * until it encounters a line terminator or
  389.      * end of 
  390.      * file; the characters read are then
  391.      * returned as a <code>String</code>. Note 
  392.      * that because this 
  393.      * method processes bytes,
  394.      * it does not support input of the full Unicode
  395.      * character set.
  396.      * <p>
  397.      * If end of file is encountered
  398.      * before even one byte can be read, then <code>null</code>
  399.      * is returned. Otherwise, each byte that is
  400.      * read is converted to type <code>char</code>
  401.      * by zero-extension. If the character <code>'\n'</code>
  402.      * is encountered, it is discarded and reading
  403.      * ceases. If the character <code>'\r'</code>
  404.      * is encountered, it is discarded and, if 
  405.      * the following byte converts  to the 
  406.      * character <code>'\n'</code>, then that is
  407.      * discarded also; reading then ceases. If 
  408.      * end of file is encountered before either
  409.      * of the characters <code>'\n'</code> and 
  410.      * <code>'\r'</code> is encountered, reading
  411.      * ceases. Once reading has ceased, a <code>String</code>
  412.      * is returned that contains all the characters
  413.      * read and not discarded, taken in order. 
  414.      * Note that every character in this string
  415.      * will have a value less than <code>\u0100</code>,
  416.      * that is, <code>(char)256</code>.
  417.      *
  418.      * @return     if this stream reaches the end before reading all the bytes.
  419.      * @exception  IOException  if an I/O error occurs.
  420.      * @since      JDK1.0
  421.      */
  422.     String readLine() throws IOException;
  423.  
  424.     /**
  425.      * Reads in a string that has been encoded using a modified UTF-8 format.
  426.      * The general contract of <code>readUTF</code>
  427.      * is that it reads a representation of a Unicode
  428.      * character string encoded in Java modified
  429.      * UTF-8 format; this string of characters 
  430.      * is then returned as a <code>String</code>.
  431.      * <p>
  432.      * First, two bytes are read and used to
  433.      * construct an unsigned 16-bit integer in 
  434.      * exactly the manner of the <code>readUnsignedShort</code>
  435.      * method . This integer value is called the
  436.      * <i>UTF length</i> and specifies the number
  437.      * of additional bytes to be read. These bytes
  438.      * are then converted to characters by considering
  439.      * them in groups. The length of each group
  440.      * is computed from the value of the first 
  441.      * byte of the group. The byte following a 
  442.      * group, if any, is the first byte of the 
  443.      * next group.
  444.      * <p>
  445.      * If the first byte of a group
  446.      * matches the bit pattern <code>0xxxxxxx</code>
  447.      * (where <code>x</code> means "may be <code>0</code>
  448.      * or <code>1</code>"), then the group consists
  449.      * of just that byte. The byte is zero-extended
  450.      * to form a character.
  451.      * <p> 
  452.      * If the first byte
  453.      * of a group matches the bit pattern <code>110xxxxx</code>,
  454.      * then the group consists of that byte <code>a</code>
  455.      * and a second byte <code>b</code>. If there
  456.      * is no byte <code>b</code> (because byte 
  457.      * <code>a</code> was the last of the bytes
  458.      * to be read), or if byte <code>b</code> does
  459.      * not match the bit pattern <code>10xxxxxx</code>,
  460.      * then a <code>UTFDataFormatException</code>
  461.      * is thrown. Otherwise, the group is converted
  462.      * to the character:<p>
  463.      * <pre><code>(char)(((a& 0x1F) << 6) | (b & 0x3F))
  464.      * </code></pre>
  465.      * If the first byte of a group
  466.      * matches the bit pattern <code>1110xxxx</code>,
  467.      * then the group consists of that byte <code>a</code>
  468.      * and two more bytes <code>b</code> and <code>c</code>.
  469.      * If there is no byte <code>c</code> (because
  470.      * byte <code>a</code> was one of the last 
  471.      * two of the bytes to be read), or either 
  472.      * byte <code>b</code> or byte <code>c</code>
  473.      * does not match the bit pattern <code>10xxxxxx</code>,
  474.      * then a <code>UTFDataFormatException</code>
  475.      * is thrown. Otherwise, the group is converted
  476.      * to the character:<p>
  477.      * <pre><code>
  478.      * (char)(((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F))
  479.      * </code></pre>
  480.      * If the first byte of a group matches the
  481.      * pattern <code>1111xxxx</code> or the pattern
  482.      * <code>10xxxxxx</code>, then a <code>UTFDataFormatException</code>
  483.      * is thrown.
  484.      * <p>
  485.      * If end of file is encountered
  486.      * at any time during this entire process, 
  487.      * then an <code>EOFException</code> is thrown.
  488.      * <p>
  489.      * After every group has been converted to
  490.      * a character by this process, the characters
  491.      * are gathered, in the same order in which
  492.      * their corresponding groups were read from
  493.      * the input stream, to form a <code>String</code>,
  494.      * which is returned.
  495.      * <p>
  496.      * The <code>writeUTF</code>
  497.      * method of interface <code>DataOutput</code>
  498.      * may be used to write data that is suitable
  499.      * for reading by this method.
  500.      * @return     a Unicode string.
  501.      * @exception  EOFException            if this stream reaches the end
  502.      *               before reading all the bytes.
  503.      * @exception  IOException             if an I/O error occurs.
  504.      * @exception  UTFDataFormatException  if the bytes do not represent a
  505.      *               valid UTF-8 encoding of a string.
  506.      * @since      JDK1.0
  507.      */
  508.     String readUTF() throws IOException;
  509. }
  510.