home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / RandomAccessFile.java < prev    next >
Text File  |  1997-05-21  |  32KB  |  862 lines

  1. /*
  2.  * @(#)RandomAccessFile.java    1.31 97/02/05
  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. import java.io.File;
  26.  
  27. /**
  28.  * Instances of this class support both reading and writing to a 
  29.  * random access file. An application can modify the position in the 
  30.  * file at which the next read or write occurs. 
  31.  * This class provides a sense of security
  32.  * by offering methods that allow specified mode accesses of 
  33.  * read-only or read-write to files.
  34.  *
  35.  * @author  unascribed
  36.  * @version 1.31, 02/05/97
  37.  * @since   JDK1.0
  38.  */
  39. public
  40. class RandomAccessFile implements DataOutput, DataInput {
  41.     private FileDescriptor fd;
  42.  
  43.     /**
  44.      * Creates a random access file stream to read from, and optionally 
  45.      * to write to, a file with the specified name. 
  46.      * <p>
  47.      * The mode argument must either be equal to <code>"r"</code> or 
  48.      * <code>"rw"</code>, indicating either to open the file for input or 
  49.      * for both input and output. 
  50.      *
  51.      * @param      name   the system-dependent filename.
  52.      * @param      mode   the access mode.
  53.      * @exception  IllegalArgumentException  if the mode argument is not equal
  54.      *               to <code>"r"</code> or to <code>"rw"</code>.
  55.      * @exception  IOException               if an I/O error occurs.
  56.      * @exception  SecurityException         if a security manager exists, its
  57.      *               <code>checkRead</code> method is called with the name
  58.      *               argument to see if the application is allowed read access
  59.      *               to the file. If the mode argument is equal to
  60.      *               <code>"rw"</code>, its <code>checkWrite</code> method also
  61.      *               is called with the name argument to see if the application
  62.      *               is allowed write access to the file. Either of these may
  63.      *               result in a security exception.
  64.      * @see        java.lang.SecurityException
  65.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  66.      * @since      JDK1.0
  67.      */
  68.     public RandomAccessFile(String name, String mode) throws IOException {
  69.     boolean rw = mode.equals("rw");
  70.     if (!rw && !mode.equals("r"))
  71.         throw new IllegalArgumentException("mode must be r or rw");
  72.     SecurityManager security = System.getSecurityManager();
  73.     if (security != null) {
  74.         security.checkRead(name);
  75.         if (rw) {
  76.         security.checkWrite(name);
  77.         }
  78.     }
  79.     fd = new FileDescriptor();
  80.     open(name, rw);
  81.     }
  82.     
  83.     /**
  84.      * Creates a random access file stream to read from, and optionally 
  85.      * to write to, the file specified by the <code>File</code> argument. 
  86.      * <p>
  87.      * The mode argument must either be equal to <code>"r"</code> or to 
  88.      * <code>"rw"</code>, indicating either to open the file for input, 
  89.      * or for both input and output, respectively. 
  90.      *
  91.      * @param      file   the file object.
  92.      * @param      mode   the access mode.
  93.      * @exception  IllegalArgumentException  if the mode argument is not equal
  94.      *               to <code>"r"</code> or to <code>"rw"</code>.
  95.      * @exception  IOException               if an I/O error occurs.
  96.      * @exception  SecurityException         if a security manager exists, its
  97.      *               <code>checkRead</code> method is called with the pathname
  98.      *               of the <code>File</code> argument to see if the
  99.      *               application is allowed read access to the file. If the
  100.      *               mode argument is equal to <code>"rw"</code>, its
  101.      *               <code>checkWrite</code> method also is called with the
  102.      *               pathname to see if the application is allowed write access
  103.      *               to the file.
  104.      * @see        java.io.File#getPath()
  105.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  106.      * @since      JDK1.0
  107.      */
  108.     public RandomAccessFile(File file, String mode) throws IOException {
  109.     this(file.getPath(), mode);
  110.     }
  111.  
  112.     /**
  113.      * Returns the opaque file descriptor object associated with this stream.
  114.      *
  115.      * @return     the file descriptor object associated with this stream.
  116.      * @exception  IOException  if an I/O error occurs.
  117.      * @see        java.io.FileDescriptor
  118.      * @since      JDK1.0
  119.      */
  120.     public final FileDescriptor getFD() throws IOException {
  121.     if (fd != null) return fd;
  122.     throw new IOException();
  123.     }
  124.  
  125.     /**
  126.      * Opens a file and returns the file descriptor.  The file is 
  127.      * opened in read-write mode if writeable is true, else 
  128.      * the file is opened as read-only.
  129.      * @param name the name of the file
  130.      * @param writeable the boolean indicating whether file is 
  131.      * writeable or not.
  132.      */
  133.     private native void open(String name, boolean writeable) throws IOException;
  134.  
  135.     // 'Read' primitives
  136.     
  137.     /**
  138.      * Reads a byte of data from this file. This method blocks if no 
  139.      * input is yet available. 
  140.      *
  141.      * @return     the next byte of data, or <code>-1</code> if the end of the
  142.      *             file is reached.
  143.      * @exception  IOException  if an I/O error occurs.
  144.      * @since      JDK1.0
  145.      */
  146.     public native int read() throws IOException;
  147.  
  148.     /**
  149.      * Reads a sub array as a sequence of bytes. 
  150.      * @param b the data to be written
  151.      * @param off the start offset in the data
  152.      * @param len the number of bytes that are written
  153.      * @exception IOException If an I/O error has occurred.
  154.      */
  155.     private native int readBytes(byte b[], int off, int len) throws IOException;
  156.  
  157.     /**
  158.      * Reads up to <code>len</code> bytes of data from this file into an 
  159.      * array of bytes. This method blocks until at least one byte of input 
  160.      * is available. 
  161.      *
  162.      * @param      b     the buffer into which the data is read.
  163.      * @param      off   the start offset of the data.
  164.      * @param      len   the maximum number of bytes read.
  165.      * @return     the total number of bytes read into the buffer, or
  166.      *             <code>-1</code> if there is no more data because the end of
  167.      *             the file has been reached.
  168.      * @exception  IOException  if an I/O error occurs.
  169.      * @since      JDK1.0
  170.      */
  171.     public int read(byte b[], int off, int len) throws IOException {
  172.     return readBytes(b, off, len);
  173.     }
  174.  
  175.     /**
  176.      * Reads up to <code>b.length</code> bytes of data from this file 
  177.      * into an array of bytes. This method blocks until at least one byte 
  178.      * of input is available. 
  179.      *
  180.      * @param      b   the buffer into which the data is read.
  181.      * @return     the total number of bytes read into the buffer, or
  182.      *             <code>-1</code> if there is no more data because the end of
  183.      *             this file has been reached.
  184.      * @exception  IOException  if an I/O error occurs.
  185.      * @since      JDK1.0 
  186.      */
  187.     public int read(byte b[]) throws IOException {
  188.     return readBytes(b, 0, b.length);
  189.     }
  190.    
  191.     /**
  192.      * Reads <code>b.length</code> bytes from this file into the byte 
  193.      * array. This method reads repeatedly from the file until all the 
  194.      * bytes are read. This method blocks until all the bytes are read, 
  195.      * the end of the stream is detected, or an exception is thrown. 
  196.      *
  197.      * @param      b   the buffer into which the data is read.
  198.      * @exception  EOFException  if this file reaches the end before reading
  199.      *               all the bytes.
  200.      * @exception  IOException   if an I/O error occurs.
  201.      * @since      JDK1.0
  202.      */
  203.     public final void readFully(byte b[]) throws IOException {
  204.     readFully(b, 0, b.length);
  205.     }
  206.  
  207.     /**
  208.      * Reads exactly <code>len</code> bytes from this file into the byte 
  209.      * array. This method reads repeatedly from the file until all the 
  210.      * bytes are read. This method blocks until all the bytes are read, 
  211.      * the end of the stream is detected, or an exception is thrown. 
  212.      *
  213.      * @param      b     the buffer into which the data is read.
  214.      * @param      off   the start offset of the data.
  215.      * @param      len   the number of bytes to read.
  216.      * @exception  EOFException  if this file reaches the end before reading
  217.      *               all the bytes.
  218.      * @exception  IOException   if an I/O error occurs.
  219.      * @since      JDK1.0
  220.      */
  221.     public final void readFully(byte b[], int off, int len) throws IOException {
  222.     int n = 0;
  223.     while (n < len) {
  224.         int count = this.read(b, off + n, len - n);
  225.         if (count < 0)
  226.         throw new EOFException();
  227.         n += count;
  228.     }
  229.     }
  230.  
  231.     /**
  232.      * Skips exactly <code>n</code> bytes of input. 
  233.      * <p>
  234.      * This method blocks until all the bytes are skipped, the end of 
  235.      * the stream is detected, or an exception is thrown. 
  236.      *
  237.      * @param      n   the number of bytes to be skipped.
  238.      * @return     the number of bytes skipped, which is always <code>n</code>.
  239.      * @exception  EOFException  if this file reaches the end before skipping
  240.      *               all the bytes.
  241.      * @exception  IOException  if an I/O error occurs.
  242.      * @since      JDK1.0
  243.      */
  244.     public int skipBytes(int n) throws IOException {
  245.         seek(getFilePointer() + n);
  246.         return n;
  247.     }
  248.  
  249.     // 'Write' primitives
  250.  
  251.     /**
  252.      * Writes the specified byte to this file. 
  253.      *
  254.      * @param      b   the <code>byte</code> to be written.
  255.      * @exception  IOException  if an I/O error occurs.
  256.      * @since      JDK1.0
  257.      */
  258.     public native void write(int b) throws IOException;
  259.  
  260.     /**
  261.      * Writes a sub array as a sequence of bytes. 
  262.      * @param b the data to be written
  263.      * @param off the start offset in the data
  264.      * @param len the number of bytes that are written
  265.      * @exception IOException If an I/O error has occurred.
  266.      */
  267.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  268.  
  269.     /**
  270.      * Writes <code>b.length</code> bytes from the specified byte array 
  271.      * starting at offset <code>off</code> to this file. 
  272.      *
  273.      * @param      b   the data.
  274.      * @exception  IOException  if an I/O error occurs.
  275.      * @since      JDK1.0
  276.      */
  277.     public void write(byte b[]) throws IOException {
  278.     writeBytes(b, 0, b.length); 
  279.     }
  280.  
  281.     /**
  282.      * Writes <code>len</code> bytes from the specified byte array 
  283.      * starting at offset <code>off</code> to this file. 
  284.      *
  285.      * @param      b     the data.
  286.      * @param      off   the start offset in the data.
  287.      * @param      len   the number of bytes to write.
  288.      * @exception  IOException  if an I/O error occurs.
  289.      * @since      JDK1.0
  290.      */
  291.     public void write(byte b[], int off, int len) throws IOException {
  292.     writeBytes(b, off, len);
  293.     }
  294.  
  295.     // 'Random access' stuff
  296.  
  297.     /**
  298.      * Returns the current offset in this file. 
  299.      *
  300.      * @return     the offset from the beginning of the file, in bytes,
  301.      *             at which the next read or write occurs.
  302.      * @exception  IOException  if an I/O error occurs.
  303.      * @since      JDK1.0
  304.      */
  305.     public native long getFilePointer() throws IOException;
  306.  
  307.     /**
  308.      * Sets the offset from the beginning of this file at which the next 
  309.      * read or write occurs. 
  310.      *
  311.      * @param      pos   the absolute position.
  312.      * @exception  IOException  if an I/O error occurs.
  313.      * @since      JDK1.0
  314.      */
  315.     public native void seek(long pos) throws IOException;
  316.  
  317.     /**
  318.      * Returns the length of this file.
  319.      *
  320.      * @return     the length of this file.
  321.      * @exception  IOException  if an I/O error occurs.
  322.      * @since      JDK1.0
  323.      */
  324.     public native long length() throws IOException;
  325.  
  326.     /**
  327.      * Closes this random access file stream and releases any system 
  328.      * resources associated with the stream. 
  329.      *
  330.      * @exception  IOException  if an I/O error occurs.
  331.      * @since      JDK1.0
  332.      */
  333.     public native void close() throws IOException;
  334.  
  335.     //
  336.     //  Some "reading/writing Java data types" methods stolen from
  337.     //  DataInputStream and DataOutputStream.
  338.     //
  339.  
  340.     /**
  341.      * Reads a <code>boolean</code> from this file. This method reads a 
  342.      * single byte from the file. A value of <code>0</code> represents 
  343.      * <code>false</code>. Any other value represents <code>true</code>. 
  344.      * This method blocks until the byte is read, the end of the stream 
  345.      * is detected, or an exception is thrown. 
  346.      *
  347.      * @return     the <code>boolean</code> value read.
  348.      * @exception  EOFException  if this file has reached the end.
  349.      * @exception  IOException   if an I/O error occurs.
  350.      * @since      JDK1.0
  351.      */
  352.     public final boolean readBoolean() throws IOException {
  353.     int ch = this.read();
  354.     if (ch < 0)
  355.         throw new EOFException();
  356.     return (ch != 0);
  357.     }
  358.  
  359.     /**
  360.      * Reads a signed 8-bit value from this file. This method reads a 
  361.      * byte from the file. If the byte read is <code>b</code>, where 
  362.      * <code>0 <= b <= 255</code>, 
  363.      * then the result is:
  364.      * <ul><code>
  365.      *     (byte)(b)
  366.      *</code></ul>
  367.      * <p>
  368.      * This method blocks until the byte is read, the end of the stream 
  369.      * is detected, or an exception is thrown. 
  370.      *
  371.      * @return     the next byte of this file as a signed 8-bit
  372.      *             <code>byte</code>.
  373.      * @exception  EOFException  if this file has reached the end.
  374.      * @exception  IOException   if an I/O error occurs.
  375.      * @since      JDK1.0
  376.      */
  377.     public final byte readByte() throws IOException {
  378.     int ch = this.read();
  379.     if (ch < 0)
  380.         throw new EOFException();
  381.     return (byte)(ch);
  382.     }
  383.  
  384.     /**
  385.      * Reads an unsigned 8-bit number from this file. This method reads 
  386.      * a byte from this file and returns that byte. 
  387.      * <p>
  388.      * This method blocks until the byte is read, the end of the stream 
  389.      * is detected, or an exception is thrown. 
  390.      *
  391.      * @return     the next byte of this file, interpreted as an unsigned
  392.      *             8-bit number.
  393.      * @exception  EOFException  if this file has reached the end.
  394.      * @exception  IOException   if an I/O error occurs.
  395.      * @since      JDK1.0
  396.      */
  397.     public final int readUnsignedByte() throws IOException {
  398.     int ch = this.read();
  399.     if (ch < 0)
  400.         throw new EOFException();
  401.     return ch;
  402.     }
  403.  
  404.     /**
  405.      * Reads a signed 16-bit number from this file. The method reads 2 
  406.      * bytes from this file. If the two bytes read, in order, are 
  407.      * <code>b1</code> and <code>b2</code>, where each of the two values is 
  408.      * between <code>0</code> and <code>255</code>, inclusive, then the 
  409.      * result is equal to:
  410.      * <ul><code>
  411.      *     (short)((b1 << 8) | b2)
  412.      * </code></ul>
  413.      * <p>
  414.      * This method blocks until the two bytes are read, the end of the 
  415.      * stream is detected, or an exception is thrown. 
  416.      *
  417.      * @return     the next two bytes of this file, interpreted as a signed
  418.      *             16-bit number.
  419.      * @exception  EOFException  if this file reaches the end before reading
  420.      *               two bytes.
  421.      * @exception  IOException   if an I/O error occurs.
  422.      * @since      JDK1.0
  423.      */
  424.     public final short readShort() throws IOException {
  425.     int ch1 = this.read();
  426.     int ch2 = this.read();
  427.     if ((ch1 | ch2) < 0)
  428.          throw new EOFException();
  429.     return (short)((ch1 << 8) + (ch2 << 0));
  430.     }
  431.  
  432.     /**
  433.      * Reads an unsigned 16-bit number from this file. This method reads 
  434.      * two bytes from the file. If the bytes read, in order, are 
  435.      * <code>b1</code> and <code>b2</code>, where 
  436.      * <code>0 <= b1, b2 <= 255</code>, 
  437.      * then the result is equal to:
  438.      * <ul><code>
  439.      *     (b1 << 8) | b2
  440.      * </code></ul>
  441.      * <p>
  442.      * This method blocks until the two bytes are read, the end of the 
  443.      * stream is detected, or an exception is thrown. 
  444.      *
  445.      * @return     the next two bytes of this file, interpreted as an unsigned
  446.      *             16-bit integer.
  447.      * @exception  EOFException  if this file reaches the end before reading
  448.      *               two bytes.
  449.      * @exception  IOException   if an I/O error occurs.
  450.      * @since      JDK1.0
  451.      */
  452.     public final int readUnsignedShort() throws IOException {
  453.     int ch1 = this.read();
  454.     int ch2 = this.read();
  455.     if ((ch1 | ch2) < 0)
  456.          throw new EOFException();
  457.     return (ch1 << 8) + (ch2 << 0);
  458.     }
  459.  
  460.     /**
  461.      * Reads a Unicode character from this file. This method reads two
  462.      * bytes from the file. If the bytes read, in order, are 
  463.      * <code>b1</code> and <code>b2</code>, where 
  464.      * <code>0 <= b1, b2 <= 255</code>, 
  465.      * then the result is equal to:
  466.      * <ul><code>
  467.      *     (char)((b1 << 8) | b2)
  468.      * </code></ul>
  469.      * <p>
  470.      * This method blocks until the two bytes are read, the end of the 
  471.      * stream is detected, or an exception is thrown. 
  472.      *
  473.      * @return     the next two bytes of this file as a Unicode character.
  474.      * @exception  EOFException  if this file reaches the end before reading
  475.      *               two bytes.
  476.      * @exception  IOException   if an I/O error occurs.
  477.      * @since      JDK1.0
  478.      */
  479.     public final char readChar() throws IOException {
  480.     int ch1 = this.read();
  481.     int ch2 = this.read();
  482.     if ((ch1 | ch2) < 0)
  483.          throw new EOFException();
  484.     return (char)((ch1 << 8) + (ch2 << 0));
  485.     }
  486.  
  487.     /**
  488.      * Reads a signed 32-bit integer from this file. This method reads 4 
  489.      * bytes from the file. If the bytes read, in order, are <code>b1</code>,
  490.      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where 
  491.      * <code>0 <= b1, b2, b3, b4 <= 255</code>, 
  492.      * then the result is equal to:
  493.      * <ul><code>
  494.      *     (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
  495.      * </code></ul>
  496.      * <p>
  497.      * This method blocks until the four bytes are read, the end of the 
  498.      * stream is detected, or an exception is thrown. 
  499.      *
  500.      * @return     the next four bytes of this file, interpreted as an
  501.      *             <code>int</code>.
  502.      * @exception  EOFException  if this file reaches the end before reading
  503.      *               four bytes.
  504.      * @exception  IOException   if an I/O error occurs.
  505.      * @since      JDK1.0
  506.      */
  507.     public final int readInt() throws IOException {
  508.     int ch1 = this.read();
  509.     int ch2 = this.read();
  510.     int ch3 = this.read();
  511.     int ch4 = this.read();
  512.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  513.          throw new EOFException();
  514.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  515.     }
  516.  
  517.     /**
  518.      * Reads a signed 64-bit integer from this file. This method reads eight
  519.      * bytes from the file. If the bytes read, in order, are 
  520.      * <code>b1</code>, <code>b2</code>, <code>b3</code>, 
  521.      * <code>b4</code>, <code>b5</code>, <code>b6</code>, 
  522.      * <code>b7</code>, and <code>b8,</code> where:
  523.      * <ul><code>
  524.      *     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
  525.      * </code></ul>
  526.      * <p>
  527.      * then the result is equal to:
  528.      * <p><blockquote><pre>
  529.      *     ((long)b1 << 56) + ((long)b2 << 48)
  530.      *     + ((long)b3 << 40) + ((long)b4 << 32)
  531.      *     + ((long)b5 << 24) + ((long)b6 << 16)
  532.      *     + ((long)b7 << 8) + b8
  533.      * </pre></blockquote>
  534.      * <p>
  535.      * This method blocks until the eight bytes are read, the end of the 
  536.      * stream is detected, or an exception is thrown. 
  537.      *
  538.      * @return     the next eight bytes of this file, interpreted as a
  539.      *             <code>long</code>.
  540.      * @exception  EOFException  if this file reaches the end before reading
  541.      *               eight bytes.
  542.      * @exception  IOException   if an I/O error occurs.
  543.      * @since      JDK1.0
  544.      */
  545.     public final long readLong() throws IOException {
  546.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  547.     }
  548.  
  549.     /**
  550.      * Reads a <code>float</code> from this file. This method reads an 
  551.      * <code>int</code> value as if by the <code>readInt</code> method 
  552.      * and then converts that <code>int</code> to a <code>float</code> 
  553.      * using the <code>intBitsToFloat</code> method in class 
  554.      * <code>Float</code>. 
  555.      * <p>
  556.      * This method blocks until the four bytes are read, the end of the 
  557.      * stream is detected, or an exception is thrown. 
  558.      *
  559.      * @return     the next four bytes of this file, interpreted as a
  560.      *             <code>float</code>.
  561.      * @exception  EOFException  if this file reaches the end before reading
  562.      *             four bytes.
  563.      * @exception  IOException   if an I/O error occurs.
  564.      * @see        java.io.RandomAccessFile#readInt()
  565.      * @see        java.lang.Float#intBitsToFloat(int)
  566.      * @since      JDK1.0
  567.      */
  568.     public final float readFloat() throws IOException {
  569.     return Float.intBitsToFloat(readInt());
  570.     }
  571.  
  572.     /**
  573.      * Reads a <code>double</code> from this file. This method reads a 
  574.      * <code>long</code> value as if by the <code>readLong</code> method 
  575.      * and then converts that <code>long</code> to a <code>double</code> 
  576.      * using the <code>longBitsToDouble</code> method in 
  577.      * class <code>Double</code>.
  578.      * <p>
  579.      * This method blocks until the eight bytes are read, the end of the 
  580.      * stream is detected, or an exception is thrown. 
  581.      *
  582.      * @return     the next eight bytes of this file, interpreted as a
  583.      *             <code>double</code>.
  584.      * @exception  EOFException  if this file reaches the end before reading
  585.      *             eight bytes.
  586.      * @exception  IOException   if an I/O error occurs.
  587.      * @see        java.io.RandomAccessFile#readLong()
  588.      * @see        java.lang.Double#longBitsToDouble(long)
  589.      * @since      JDK1.0
  590.      */
  591.     public final double readDouble() throws IOException {
  592.     return Double.longBitsToDouble(readLong());
  593.     }
  594.  
  595.     /**
  596.      * Reads the next line of text from this file. This method 
  597.      * successively reads bytes from the file until it reaches the end of 
  598.      * a line of text. 
  599.      * <p>
  600.      * A line of text is terminated by a carriage-return character 
  601.      * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a 
  602.      * carriage-return character immediately followed by a newline 
  603.      * character, or the end of the input stream. The line-terminating 
  604.      * character(s), if any, are included as part of the string returned. 
  605.      * <p>
  606.      * This method blocks until a newline character is read, a carriage 
  607.      * return and the byte following it are read (to see if it is a 
  608.      * newline), the end of the stream is detected, or an exception is thrown.
  609.      *
  610.      * @return     the next line of text from this file.
  611.      * @exception  IOException  if an I/O error occurs.
  612.      * @since      JDK1.0
  613.      */
  614.     public final String readLine() throws IOException {
  615.     StringBuffer input = new StringBuffer();
  616.     int c;
  617.  
  618.     while (((c = read()) != -1) && (c != '\n')) {
  619.         input.append((char)c);
  620.     }
  621.     if ((c == -1) && (input.length() == 0)) {
  622.         return null;
  623.     }
  624.     return input.toString();
  625.     }
  626.  
  627.     /**
  628.      * Reads in a string from this file. The string has been encoded 
  629.      * using a modified UTF-8 format. 
  630.      * <p>
  631.      * The first two bytes are read as if by 
  632.      * <code>readUnsignedShort</code>. This value gives the number of 
  633.      * following bytes that are in the encoded string, not
  634.      * the length of the resulting string. The following bytes are then 
  635.      * interpreted as bytes encoding characters in the UTF-8 format 
  636.      * and are converted into characters. 
  637.      * <p>
  638.      * This method blocks until all the bytes are read, the end of the 
  639.      * stream is detected, or an exception is thrown. 
  640.      *
  641.      * @return     a Unicode string.
  642.      * @exception  EOFException            if this file reaches the end before
  643.      *               reading all the bytes.
  644.      * @exception  IOException             if an I/O error occurs.
  645.      * @exception  UTFDataFormatException  if the bytes do not represent 
  646.      *               valid UTF-8 encoding of a Unicode string.
  647.      * @see        java.io.RandomAccessFile#readUnsignedShort()
  648.      * @since      JDK1.0
  649.      */
  650.     public final String readUTF() throws IOException {
  651.     return DataInputStream.readUTF(this);
  652.     }
  653.  
  654.     /**
  655.      * Writes a <code>boolean</code> to the file as a 1-byte value. The 
  656.      * value <code>true</code> is written out as the value 
  657.      * <code>(byte)1</code>; the value <code>false</code> is written out 
  658.      * as the value <code>(byte)0</code>.
  659.      *
  660.      * @param      v   a <code>boolean</code> value to be written.
  661.      * @exception  IOException  if an I/O error occurs.
  662.      * @since      JDK1.0
  663.      */
  664.     public final void writeBoolean(boolean v) throws IOException {
  665.     write(v ? 1 : 0);
  666.     //written++;
  667.     }
  668.  
  669.     /**
  670.      * Writes a <code>byte</code> to the file as a 1-byte value. 
  671.      *
  672.      * @param      v   a <code>byte</code> value to be written.
  673.      * @exception  IOException  if an I/O error occurs.
  674.      * @since      JDK1.0
  675.      */
  676.     public final void writeByte(int v) throws IOException {
  677.     write(v);
  678.     //written++;
  679.     }
  680.  
  681.     /**
  682.      * Writes a <code>short</code> to the file as two bytes, high byte first.
  683.      *
  684.      * @param      v   a <code>short</code> to be written.
  685.      * @exception  IOException  if an I/O error occurs.
  686.      * @since      JDK1.0
  687.      */
  688.     public final void writeShort(int v) throws IOException {
  689.     write((v >>> 8) & 0xFF);
  690.     write((v >>> 0) & 0xFF);
  691.     //written += 2;
  692.     }
  693.  
  694.     /**
  695.      * Writes a <code>char</code> to the file as a 2-byte value, high
  696.      * byte first.
  697.      *
  698.      * @param      v   a <code>char</code> value to be written.
  699.      * @exception  IOException  if an I/O error occurs.
  700.      * @since      JDK1.0
  701.      */
  702.     public final void writeChar(int v) throws IOException {
  703.     write((v >>> 8) & 0xFF);
  704.     write((v >>> 0) & 0xFF);
  705.     //written += 2;
  706.     }
  707.  
  708.     /**
  709.      * Writes an <code>int</code> to the file as four bytes, high byte first.
  710.      *
  711.      * @param      v   an <code>int</code> to be written.
  712.      * @exception  IOException  if an I/O error occurs.
  713.      * @since      JDK1.0
  714.      */
  715.     public final void writeInt(int v) throws IOException {
  716.     write((v >>> 24) & 0xFF);
  717.     write((v >>> 16) & 0xFF);
  718.     write((v >>>  8) & 0xFF);
  719.     write((v >>>  0) & 0xFF);
  720.     //written += 4;
  721.     }
  722.  
  723.     /**
  724.      * Writes a <code>long</code> to the file as eight bytes, high byte first.
  725.      *
  726.      * @param      v   a <code>long</code> to be written.
  727.      * @exception  IOException  if an I/O error occurs.
  728.      * @since      JDK1.0
  729.      */
  730.     public final void writeLong(long v) throws IOException {
  731.     write((int)(v >>> 56) & 0xFF);
  732.     write((int)(v >>> 48) & 0xFF);
  733.     write((int)(v >>> 40) & 0xFF);
  734.     write((int)(v >>> 32) & 0xFF);
  735.     write((int)(v >>> 24) & 0xFF);
  736.     write((int)(v >>> 16) & 0xFF);
  737.     write((int)(v >>>  8) & 0xFF);
  738.     write((int)(v >>>  0) & 0xFF);
  739.     //written += 8;
  740.     }
  741.  
  742.     /**
  743.      * Converts the float argument to an <code>int</code> using the 
  744.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  745.      * and then writes that <code>int</code> value to the file as a 
  746.      * 4-byte quantity, high byte first. 
  747.      *
  748.      * @param      v   a <code>float</code> value to be written.
  749.      * @exception  IOException  if an I/O error occurs.
  750.      * @see        java.lang.Float#floatToIntBits(float)
  751.      * @since      JDK1.0
  752.      */
  753.     public final void writeFloat(float v) throws IOException {
  754.     writeInt(Float.floatToIntBits(v));
  755.     }
  756.  
  757.     /**
  758.      * Converts the double argument to a <code>long</code> using the 
  759.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  760.      * and then writes that <code>long</code> value to the file as an 
  761.      * 8-byte quantity, high byte first. 
  762.      *
  763.      * @param      v   a <code>double</code> value to be written.
  764.      * @exception  IOException  if an I/O error occurs.
  765.      * @see        java.lang.Double#doubleToLongBits(double)
  766.      * @since      JDK1.0
  767.      */
  768.     public final void writeDouble(double v) throws IOException {
  769.     writeLong(Double.doubleToLongBits(v));
  770.     }
  771.  
  772.     /**
  773.      * Writes the string to the file as a sequence of bytes. Each 
  774.      * character in the string is written out, in sequence, by discarding 
  775.      * its high eight bits. 
  776.      *
  777.      * @param      s   a string of bytes to be written.
  778.      * @exception  IOException  if an I/O error occurs.
  779.      * @since      JDK1.0
  780.      */
  781.     public final void writeBytes(String s) throws IOException {
  782.     int len = s.length();
  783.     for (int i = 0 ; i < len ; i++) {
  784.         write((byte)s.charAt(i));
  785.     }
  786.     //written += len;
  787.     }
  788.  
  789.     /**
  790.      * Writes a string to the file as a sequence of characters. Each 
  791.      * character is written to the data output stream as if by the 
  792.      * <code>writeChar</code> method. 
  793.      *
  794.      * @param      s   a <code>String</code> value to be written.
  795.      * @exception  IOException  if an I/O error occurs.
  796.      * @see        java.io.RandomAccessFile#writeChar(int)
  797.      * @since      JDK1.0
  798.      */
  799.     public final void writeChars(String s) throws IOException {
  800.     int len = s.length();
  801.     for (int i = 0 ; i < len ; i++) {
  802.         int v = s.charAt(i);
  803.         write((v >>> 8) & 0xFF);
  804.         write((v >>> 0) & 0xFF);
  805.     }
  806.     //written += len * 2;
  807.     }
  808.  
  809.     /**
  810.      * Writes a string to the file using UTF-8 encoding in a 
  811.      * machine-independent manner. 
  812.      * <p>
  813.      * First, two bytes are written to the file as if by the 
  814.      * <code>writeShort</code> method giving the number of bytes to 
  815.      * follow. This value is the number of bytes actually written out, 
  816.      * not the length of the string. Following the length, each character 
  817.      * of the string is output, in sequence, using the UTF-8 encoding 
  818.      * for each character. 
  819.      *
  820.      * @param      str   a string to be written.
  821.      * @exception  IOException  if an I/O error occurs.
  822.      * @since      JDK1.0
  823.      */
  824.     public final void writeUTF(String str) throws IOException {
  825.     int strlen = str.length();
  826.     int utflen = 0;
  827.  
  828.     for (int i = 0 ; i < strlen ; i++) {
  829.         int c = str.charAt(i);
  830.         if ((c >= 0x0001) && (c <= 0x007F)) {
  831.         utflen++;
  832.         } else if (c > 0x07FF) {
  833.         utflen += 3;
  834.         } else {
  835.         utflen += 2;
  836.         }
  837.     }
  838.  
  839.     if (utflen > 65535)
  840.         throw new UTFDataFormatException();          
  841.  
  842.     write((utflen >>> 8) & 0xFF);
  843.     write((utflen >>> 0) & 0xFF);
  844.     for (int i = 0 ; i < strlen ; i++) {
  845.         int c = str.charAt(i);
  846.         if ((c >= 0x0001) && (c <= 0x007F)) {
  847.         write(c);
  848.         } else if (c > 0x07FF) {
  849.         write(0xE0 | ((c >> 12) & 0x0F));
  850.         write(0x80 | ((c >>  6) & 0x3F));
  851.         write(0x80 | ((c >>  0) & 0x3F));
  852.         //written += 2;
  853.         } else {
  854.         write(0xC0 | ((c >>  6) & 0x1F));
  855.         write(0x80 | ((c >>  0) & 0x3F));
  856.         //written += 1;
  857.         }
  858.     }
  859.     //written += strlen + 2;
  860.     }
  861. }
  862.