/[classpath]/classpath/java/io/RandomAccessFile.java
ViewVC logotype

Diff of /classpath/java/io/RandomAccessFile.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.31 by mkoch, Sat May 10 06:14:46 2003 UTC revision 1.32 by mkoch, Thu May 15 13:12:02 2003 UTC
# Line 62  public class RandomAccessFile implements Line 62  public class RandomAccessFile implements
62    
63    // The underlying file.    // The underlying file.
64    private FileDescriptor fd;    private FileDescriptor fd;
65      // The corresponding input and output streams.
66      private DataOutputStream out;
67      private DataInputStream in;
68        
69    private FileChannel ch; /* cached associated file-channel */    private FileChannel ch; /* cached associated file-channel */
70        
   // Used for DataOutput methods writing values to the underlying file  
   private byte[] buf = new byte[8];  
     
71    /**    /**
72     * This method initializes a new instance of <code>RandomAccessFile</code>     * This method initializes a new instance of <code>RandomAccessFile</code>
73     * to read from the specified <code>File</code> object with the specified     * to read from the specified <code>File</code> object with the specified
# Line 122  public class RandomAccessFile implements Line 122  public class RandomAccessFile implements
122        fdmode = FileDescriptor.READ;        fdmode = FileDescriptor.READ;
123      else if (mode.equals("rw"))      else if (mode.equals("rw"))
124        fdmode = FileDescriptor.READ | FileDescriptor.WRITE;        fdmode = FileDescriptor.READ | FileDescriptor.WRITE;
125      else if (mode.equals("rws") || mode.equals("rwd"))      else if (mode.equals("rws"))
126        {        {
         // FIXME: for now we treat rws and rwd and synonyms.  
127          fdmode = (FileDescriptor.READ | FileDescriptor.WRITE          fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
128                    | FileDescriptor.SYNC);                    | FileDescriptor.SYNC);
129        }        }
130        else if (mode.equals("rwd"))
131          {
132            fdmode = (FileDescriptor.READ | FileDescriptor.WRITE
133                      | FileDescriptor.DSYNC);
134          }
135      else      else
136        throw new IllegalArgumentException ("invalid mode: " + mode);        throw new IllegalArgumentException ("invalid mode: " + mode);
137    
# Line 142  public class RandomAccessFile implements Line 146  public class RandomAccessFile implements
146        }        }
147    
148      fd = new FileDescriptor (fileName, fdmode);      fd = new FileDescriptor (fileName, fdmode);
149        out = new DataOutputStream (new FileOutputStream (fd));
150        in = new DataInputStream (new FileInputStream (fd));
151    }    }
152    
153    /**    /**
# Line 225  public class RandomAccessFile implements Line 231  public class RandomAccessFile implements
231     */     */
232    public int read () throws IOException    public int read () throws IOException
233    {    {
234      return fd.read();      return in.read();
235    }    }
236    
237    /**    /**
# Line 258  public class RandomAccessFile implements Line 264  public class RandomAccessFile implements
264     */     */
265    public int read (byte[] buffer, int offset, int len) throws IOException    public int read (byte[] buffer, int offset, int len) throws IOException
266    {    {
267      return fd.read (buffer, offset, len);      return in.read (buffer, offset, len);
268    }    }
269    
270    /**    /**
# Line 280  public class RandomAccessFile implements Line 286  public class RandomAccessFile implements
286     */     */
287    public final boolean readBoolean () throws IOException    public final boolean readBoolean () throws IOException
288    {    {
289      int byte_read = read();      return in.readBoolean ();
     
     if (byte_read == -1)  
       throw new EOFException("Unexpected end of stream");  
     
     return byte_read != 0;  
290    }    }
291    
292    /**    /**
# Line 305  public class RandomAccessFile implements Line 306  public class RandomAccessFile implements
306     */     */
307    public final byte readByte () throws IOException    public final byte readByte () throws IOException
308    {    {
309      int byte_read = read ();      return in.readByte ();
     
     if (byte_read == -1)  
       throw new EOFException ("Unexpected end of stream");  
     
     return (byte) byte_read;  
310    }    }
311    
312    /**    /**
# Line 338  public class RandomAccessFile implements Line 334  public class RandomAccessFile implements
334     *     *
335     * @see DataOutput     * @see DataOutput
336     */     */
337    public final synchronized char readChar () throws IOException    public final char readChar () throws IOException
338    {    {
339      readFully (buf, 0, 2);      return in.readChar();
     
     return (char) ((buf[0] << 8) | (buf[1] & 0xff));  
340    }    }
341    
342    /**    /**
# Line 369  public class RandomAccessFile implements Line 363  public class RandomAccessFile implements
363     */     */
364    public final double readDouble () throws IOException    public final double readDouble () throws IOException
365    {    {
366      long val = readLong();      return in.readDouble ();
     
     return Double.longBitsToDouble(val);  
367    }    }
368    
369    /**    /**
# Line 396  public class RandomAccessFile implements Line 388  public class RandomAccessFile implements
388     */     */
389    public final float readFloat () throws IOException    public final float readFloat () throws IOException
390    {    {
391      int val = readInt();      return in.readFloat();
     
     return Float.intBitsToFloat(val);  
392    }    }
393    
394    /**    /**
# Line 415  public class RandomAccessFile implements Line 405  public class RandomAccessFile implements
405     */     */
406    public final void readFully (byte[] buffer) throws IOException    public final void readFully (byte[] buffer) throws IOException
407    {    {
408      readFully (buffer, 0, buffer.length);      in.readFully(buffer);
409    }    }
410    
411    /**    /**
# Line 435  public class RandomAccessFile implements Line 425  public class RandomAccessFile implements
425     * the buffer     * the buffer
426     * @exception IOException If any other error occurs     * @exception IOException If any other error occurs
427     */     */
428    public synchronized final void readFully (byte[] buffer, int offset, int len)    public final void readFully (byte[] buffer, int offset, int count)
429      throws IOException      throws IOException
430    {    {
431      int total_read = 0;      in.readFully (buffer, offset, count);
     
     while (total_read < len)  
       {  
         int bytes_read = read (buffer, offset + total_read, len - total_read);  
         if (bytes_read == -1)  
           throw new EOFException("Unexpected end of stream");  
     
         total_read += bytes_read;  
       }  
432    }    }
433    
434    /**    /**
# Line 478  public class RandomAccessFile implements Line 459  public class RandomAccessFile implements
459     *     *
460     * @see DataOutput     * @see DataOutput
461     */     */
462    public final synchronized int readInt() throws IOException    public final int readInt () throws IOException
463    {    {
464      readFully(buf, 0, 4);      return in.readInt();
     
     return (((buf[0] & 0xff) << 24) | ((buf[1] & 0xff) << 16) |  
             ((buf[2] & 0xff) << 8) | (buf[3] & 0xff));  
465    }    }
466    
467    /**    /**
# Line 513  public class RandomAccessFile implements Line 491  public class RandomAccessFile implements
491     *     *
492     * @deprecated     * @deprecated
493     */     */
494    public synchronized final String readLine () throws IOException    public final String readLine () throws IOException
495    {    {
496      StringBuffer sb = new StringBuffer ("");      return in.readLine ();
     
     for (;;)  
       {  
         int byte_read = read ();  
     
         if (byte_read == -1)  
           return sb.toString();  
     
         char c = (char) byte_read;  
     
         if (c == '\r')  
           {  
             byte_read = read();  
             if (((char)byte_read) != '\n')  
               seek (getFilePointer() - 1);  
     
             return sb.toString();  
           }  
     
         if (c == '\n')  
           return sb.toString();  
     
         sb.append (c);  
       }  
497    }    }
498    
499    /**    /**
# Line 573  public class RandomAccessFile implements Line 527  public class RandomAccessFile implements
527     *     *
528     * @see DataOutput     * @see DataOutput
529     */     */
530    public final synchronized long readLong() throws IOException    public final long readLong () throws IOException
531    {    {
532      readFully(buf, 0, 8);      return in.readLong();
     
     return (((long)(buf[0] & 0xff) << 56) |  
             ((long)(buf[1] & 0xff) << 48) |  
             ((long)(buf[2] & 0xff) << 40) |  
             ((long)(buf[3] & 0xff) << 32) |  
             ((long)(buf[4] & 0xff) << 24) |  
             ((long)(buf[5] & 0xff) << 16) |  
             ((long)(buf[6] & 0xff) <<  8) |  
             ((long)(buf[7] & 0xff)));  
533    }    }
534    
535    /**    /**
# Line 614  public class RandomAccessFile implements Line 559  public class RandomAccessFile implements
559     *     *
560     * @see DataOutput     * @see DataOutput
561     */     */
562    public final synchronized short readShort () throws IOException    public final short readShort () throws IOException
563    {    {
564      readFully (buf, 0, 2);      return in.readShort();
       
     return (short) ((buf[0] << 8) | (buf[1] & 0xff));  
565    }    }
566    
567    /**    /**
# Line 639  public class RandomAccessFile implements Line 582  public class RandomAccessFile implements
582     */     */
583    public final int readUnsignedByte () throws IOException    public final int readUnsignedByte () throws IOException
584    {    {
585      int byte_read = read ();      return in.readUnsignedByte();
     
     if (byte_read == -1)  
       throw new EOFException ("Unexpected end of stream");  
     
     return byte_read & 0xFF;  
586    }    }
587    
588    /**    /**
# Line 672  public class RandomAccessFile implements Line 610  public class RandomAccessFile implements
610     * @exception EOFException If end of file is reached before reading the value     * @exception EOFException If end of file is reached before reading the value
611     * @exception IOException If any other error occurs     * @exception IOException If any other error occurs
612     */     */
613    public final synchronized int readUnsignedShort ()    public final int readUnsignedShort () throws IOException
     throws IOException  
614    {    {
615      readFully(buf, 0, 2);      return in.readUnsignedShort();
       
     return (((buf[0] & 0xff) << 8) | (buf[1] & 0xff));  
616    }    }
617    
618    /**    /**
# Line 752  public class RandomAccessFile implements Line 687  public class RandomAccessFile implements
687     *     *
688     * @see DataOutput     * @see DataOutput
689     */     */
690    public synchronized final String readUTF () throws IOException    public final String readUTF () throws IOException
691    {    {
692      StringBuffer sb = new StringBuffer("");      return in.readUTF();
     
     int num_bytes = readUnsignedShort();  
     byte[] buf = new byte[num_bytes];  
     readFully(buf);  
     
     // FIXME: Look to migrate to new String(buf, "UTF-8") if performance ok  
     return DataInputStream.convertFromUTF(buf);  
693    }    }
694    
695    /**    /**
# Line 816  public class RandomAccessFile implements Line 744  public class RandomAccessFile implements
744     */     */
745    public void write (int oneByte) throws IOException    public void write (int oneByte) throws IOException
746    {    {
747      fd.write (oneByte);      out.write(oneByte);
748    }    }
749    
750    /**    /**
# Line 827  public class RandomAccessFile implements Line 755  public class RandomAccessFile implements
755     */     */
756    public void write (byte[] buffer) throws IOException    public void write (byte[] buffer) throws IOException
757    {    {
758      write (buffer, 0, buffer.length);      out.write(buffer);
759    }    }
760    
761    /**    /**
# Line 842  public class RandomAccessFile implements Line 770  public class RandomAccessFile implements
770     */     */
771    public void write (byte[] buffer, int offset, int len) throws IOException    public void write (byte[] buffer, int offset, int len) throws IOException
772    {    {
773      fd.write (buffer, offset, len);      out.write (buffer, offset, len);
774    }    }
775    
776    /**    /**
# Line 856  public class RandomAccessFile implements Line 784  public class RandomAccessFile implements
784     */     */
785    public final void writeBoolean (boolean val) throws IOException    public final void writeBoolean (boolean val) throws IOException
786    {    {
787      write (val ? 1 : 0);      out.writeBoolean(val);
788    }    }
789    
790    /**    /**
# Line 870  public class RandomAccessFile implements Line 798  public class RandomAccessFile implements
798     */     */
799    public final void writeByte (int v) throws IOException    public final void writeByte (int v) throws IOException
800    {    {
801      write (v & 0xFF);      out.writeByte(v);
802    }    }
803    
804    /**    /**
# Line 882  public class RandomAccessFile implements Line 810  public class RandomAccessFile implements
810     *     *
811     * @exception IOException If an error occurs     * @exception IOException If an error occurs
812     */     */
813    public final synchronized void writeShort (int s) throws IOException    public final void writeShort (int v) throws IOException
814    {    {
815      buf[0] = (byte)((s & 0xFF00) >> 8);      out.writeShort(v);
     buf[1] = (byte)(s & 0x00FF);  
     
     write(buf, 0, 2);  
816    }    }
817    
818    /**    /**
# Line 899  public class RandomAccessFile implements Line 824  public class RandomAccessFile implements
824     *     *
825     * @exception IOException If an error occurs     * @exception IOException If an error occurs
826     */     */
827    public final synchronized void writeChar (int v) throws IOException    public final void writeChar (int v) throws IOException
828    {    {
829      buf[0] = (byte)((v & 0xFF00) >> 8);      out.writeChar(v);
     buf[1] = (byte)((int)v & 0x00FF);  
     
     write(buf, 0, 2);  
830    }    }
831    
832    /**    /**
# Line 915  public class RandomAccessFile implements Line 837  public class RandomAccessFile implements
837     *     *
838     * @exception IOException If an error occurs     * @exception IOException If an error occurs
839     */     */
840    public final synchronized void writeInt (int v) throws IOException    public final void writeInt (int v) throws IOException
841    {    {
842      buf[0] = (byte)((v & 0xFF000000) >> 24);      out.writeInt(v);
     buf[1] = (byte)((v & 0x00FF0000) >> 16);  
     buf[2] = (byte)((v & 0x0000FF00) >> 8);  
     buf[3] = (byte)(v & 0x000000FF);  
     
     write (buf, 0, 4);  
843    }    }
844    
845    /**    /**
# Line 933  public class RandomAccessFile implements Line 850  public class RandomAccessFile implements
850     *     *
851     * @exception IOException If an error occurs     * @exception IOException If an error occurs
852     */     */
853    public final synchronized void writeLong (long v) throws IOException    public final void writeLong (long v) throws IOException
854    {    {
855      buf[0] = (byte)((v & 0xFF00000000000000L) >> 56);      out.writeLong(v);
     buf[1] = (byte)((v & 0x00FF000000000000L) >> 48);  
     buf[2] = (byte)((v & 0x0000FF0000000000L) >> 40);  
     buf[3] = (byte)((v & 0x000000FF00000000L) >> 32);  
     buf[4] = (byte)((v & 0x00000000FF000000L) >> 24);  
     buf[5] = (byte)((v & 0x0000000000FF0000L) >> 16);  
     buf[6] = (byte)((v & 0x000000000000FF00L) >> 8);  
     buf[7] = (byte)(v & 0x00000000000000FFL);  
     
     write (buf, 0, 8);  
856    }    }
857    
858    /**    /**
# Line 963  public class RandomAccessFile implements Line 871  public class RandomAccessFile implements
871     */     */
872    public final void writeFloat (float v) throws IOException    public final void writeFloat (float v) throws IOException
873    {    {
874      int i = Float.floatToIntBits (v);      out.writeFloat(v);
     writeInt (i);  
875    }    }
876    
877    /**    /**
# Line 984  public class RandomAccessFile implements Line 891  public class RandomAccessFile implements
891     */     */
892    public final void writeDouble (double v) throws IOException    public final void writeDouble (double v) throws IOException
893    {    {
894      long l = Double.doubleToLongBits (v);      out.writeDouble(v);
     writeLong (l);  
895    }    }
896    
897    /**    /**
# Line 999  public class RandomAccessFile implements Line 905  public class RandomAccessFile implements
905     */     */
906    public final void writeBytes (String s) throws IOException    public final void writeBytes (String s) throws IOException
907    {    {
908      int len = s.length();      out.writeBytes(s);
   
     if (len == 0)  
       return;  
     
     byte[] buf = new byte[len];  
     
     for (int i = 0; i < len; i++)  
       buf[i] = (byte)(s.charAt(i) & 0xFF);  
     
     write(buf);  
909    }    }
910        
911    /**    /**
# Line 1023  public class RandomAccessFile implements Line 919  public class RandomAccessFile implements
919     */     */
920    public final void writeChars (String s) throws IOException    public final void writeChars (String s) throws IOException
921    {    {
922      int len = s.length();      out.writeChars(s);
     if (len == 0)  
       return;  
     
     byte[] buf = new byte[len * 2];  
     
     for (int i = 0; i < len; i++)  
       {  
         buf[i * 2] = (byte)((s.charAt(i) & 0xFF00) >> 8);  
         buf[(i * 2) + 1] = (byte)(s.charAt(i) & 0x00FF);  
       }  
     
     write(buf, 0, buf.length);  
923    }    }
924        
925    /**    /**
# Line 1069  public class RandomAccessFile implements Line 953  public class RandomAccessFile implements
953     */     */
954    public final void writeUTF (String s) throws IOException    public final void writeUTF (String s) throws IOException
955    {    {
956      // FIXME:  Look to migrate to s.getBytes("UTF-8") if performance ok      out.writeUTF(s);
     byte[] buf = DataOutputStream.convertToUTF(s);  
     
     writeShort(buf.length);  
     write(buf);  
957    }    }
958        
959    /**    /**

Legend:
Removed from v.1.31  
changed lines
  Added in v.1.32

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26