1 // RandomAccessFile.java
3 /* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
14 * @author Tom Tromey <tromey@cygnus.com>
15 * @date September 25, 1998
18 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
19 * "The Java Language Specification", ISBN 0-201-63451-1
20 * Status: Believe complete and correct to 1.1.
23 public class RandomAccessFile
implements DataOutput
, DataInput
25 public void close () throws IOException
31 public final FileDescriptor
getFD () throws IOException
34 throw new IOException ();
38 public long getFilePointer () throws IOException
40 return fd
.getFilePointer();
43 public void setLength (long pos
) throws IOException
48 public long length () throws IOException
53 public RandomAccessFile (String fileName
, String mode
) throws IOException
56 if (mode
.compareTo ("r") == 0)
57 fdmode
= FileDescriptor
.READ
;
58 else if (mode
.compareTo ("rw") == 0)
59 fdmode
= FileDescriptor
.READ
| FileDescriptor
.WRITE
;
61 throw new IllegalArgumentException ("invalid mode: " + mode
);
63 SecurityManager s
= System
.getSecurityManager();
66 s
.checkRead(fileName
);
67 if ((fdmode
& FileDescriptor
.WRITE
) != 0)
68 s
.checkWrite(fileName
);
71 fd
= new FileDescriptor (fileName
, fdmode
);
72 out
= new DataOutputStream (new FileOutputStream (fd
));
73 in
= new DataInputStream (new FileInputStream (fd
));
76 public RandomAccessFile (File file
, String mode
) throws IOException
78 this (file
.getPath(), mode
);
81 public int read () throws IOException
86 public int read (byte[] buffer
) throws IOException
88 return in
.read(buffer
);
91 public int read (byte[] buffer
, int offset
, int count
) throws IOException
93 return in
.read(buffer
, offset
, count
);
96 public final boolean readBoolean () throws IOException
98 return in
.readBoolean();
101 public final byte readByte () throws IOException
103 return in
.readByte();
106 public final char readChar () throws IOException
108 return in
.readChar();
111 public final double readDouble () throws IOException
113 return in
.readDouble();
116 public final float readFloat () throws IOException
118 return in
.readFloat();
121 public final void readFully (byte[] buffer
) throws IOException
123 in
.readFully(buffer
);
126 public final void readFully (byte[] buffer
, int offset
, int count
)
129 in
.readFully(buffer
, offset
, count
);
132 public final int readInt () throws IOException
137 public final String
readLine () throws IOException
139 return in
.readLine();
142 public final long readLong () throws IOException
144 return in
.readLong();
147 public final short readShort () throws IOException
149 return in
.readShort();
152 public final int readUnsignedByte () throws IOException
154 return in
.readUnsignedByte();
157 public final int readUnsignedShort () throws IOException
159 return in
.readUnsignedShort();
162 public final String
readUTF () throws IOException
167 public void seek (long pos
) throws IOException
169 fd
.seek(pos
, FileDescriptor
.SET
, false);
172 public int skipBytes (int count
) throws IOException
176 long startPos
= fd
.getFilePointer();
177 long endPos
= fd
.seek(count
, FileDescriptor
.CUR
, true);
178 return (int) (endPos
- startPos
);
181 public void write (int oneByte
) throws IOException
186 public void write (byte[] buffer
) throws IOException
191 public void write (byte[] buffer
, int offset
, int count
) throws IOException
193 out
.write(buffer
, offset
, count
);
196 public final void writeBoolean (boolean val
) throws IOException
198 out
.writeBoolean(val
);
201 public final void writeByte (int v
) throws IOException
206 public final void writeShort (int v
) throws IOException
211 public final void writeChar (int v
) throws IOException
216 public final void writeInt (int v
) throws IOException
221 public final void writeLong (long v
) throws IOException
226 public final void writeFloat (float v
) throws IOException
231 public final void writeDouble (double v
) throws IOException
236 public final void writeBytes (String s
) throws IOException
241 public final void writeChars (String s
) throws IOException
246 public final void writeUTF (String s
) throws IOException
252 // The underlying file.
253 private FileDescriptor fd
;
254 // The corresponding input and output streams.
255 private DataOutputStream out
;
256 private DataInputStream in
;