FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / io / RandomAccessFile.java
blob1d30f1f3772887180ce51c7c2adedb5500065e4a
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
9 details. */
11 package java.io;
13 /**
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
27 if (fd.valid())
28 fd.close();
31 public final FileDescriptor getFD () throws IOException
33 if (! fd.valid())
34 throw new IOException ();
35 return fd;
38 public long getFilePointer () throws IOException
40 return fd.getFilePointer();
43 public void setLength (long pos) throws IOException
45 fd.setLength(pos);
48 public long length () throws IOException
50 return fd.length();
53 public RandomAccessFile (String fileName, String mode) throws IOException
55 int fdmode;
56 if (mode.compareTo ("r") == 0)
57 fdmode = FileDescriptor.READ;
58 else if (mode.compareTo ("rw") == 0)
59 fdmode = FileDescriptor.READ | FileDescriptor.WRITE;
60 else
61 throw new IllegalArgumentException ("invalid mode: " + mode);
63 SecurityManager s = System.getSecurityManager();
64 if (s != null)
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
83 return in.read();
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)
127 throws IOException
129 in.readFully(buffer, offset, count);
132 public final int readInt () throws IOException
134 return in.readInt();
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
164 return in.readUTF();
167 public void seek (long pos) throws IOException
169 fd.seek(pos, FileDescriptor.SET, false);
172 public int skipBytes (int count) throws IOException
174 if (count <= 0)
175 return 0;
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
183 out.write(oneByte);
186 public void write (byte[] buffer) throws IOException
188 out.write(buffer);
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
203 out.writeByte(v);
206 public final void writeShort (int v) throws IOException
208 out.writeShort(v);
211 public final void writeChar (int v) throws IOException
213 out.writeChar(v);
216 public final void writeInt (int v) throws IOException
218 out.writeInt(v);
221 public final void writeLong (long v) throws IOException
223 out.writeLong(v);
226 public final void writeFloat (float v) throws IOException
228 out.writeFloat(v);
231 public final void writeDouble (double v) throws IOException
233 out.writeDouble(v);
236 public final void writeBytes (String s) throws IOException
238 out.writeBytes(s);
241 public final void writeChars (String s) throws IOException
243 out.writeChars(s);
246 public final void writeUTF (String s) throws IOException
248 out.writeUTF(s);
252 // The underlying file.
253 private FileDescriptor fd;
254 // The corresponding input and output streams.
255 private DataOutputStream out;
256 private DataInputStream in;