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

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