| 1 | // RandomAccessFile.java
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
|
|---|
| 4 |
|
|---|
| 5 | This file is part of libgcj.
|
|---|
| 6 |
|
|---|
| 7 | This software is copyrighted work licensed under the terms of the
|
|---|
| 8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|---|
| 9 | details. */
|
|---|
| 10 |
|
|---|
| 11 | package java.io;
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * @author Tom Tromey <[email protected]>
|
|---|
| 15 | * @date September 25, 1998
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 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.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | public class RandomAccessFile implements DataOutput, DataInput
|
|---|
| 24 | {
|
|---|
| 25 | public void close () throws IOException
|
|---|
| 26 | {
|
|---|
| 27 | if (fd.valid())
|
|---|
| 28 | fd.close();
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | public final FileDescriptor getFD () throws IOException
|
|---|
| 32 | {
|
|---|
| 33 | if (! fd.valid())
|
|---|
| 34 | throw new IOException ();
|
|---|
| 35 | return fd;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public long getFilePointer () throws IOException
|
|---|
| 39 | {
|
|---|
| 40 | return fd.getFilePointer();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | public void setLength (long pos) throws IOException
|
|---|
| 44 | {
|
|---|
| 45 | fd.setLength(pos);
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | public long length () throws IOException
|
|---|
| 49 | {
|
|---|
| 50 | return fd.length();
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public RandomAccessFile (String fileName, String mode) throws IOException
|
|---|
| 54 | {
|
|---|
| 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);
|
|---|
| 62 |
|
|---|
| 63 | SecurityManager s = System.getSecurityManager();
|
|---|
| 64 | if (s != null)
|
|---|
| 65 | {
|
|---|
| 66 | s.checkRead(fileName);
|
|---|
| 67 | if ((fdmode & FileDescriptor.WRITE) != 0)
|
|---|
| 68 | s.checkWrite(fileName);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | fd = new FileDescriptor (fileName, fdmode);
|
|---|
| 72 | out = new DataOutputStream (new FileOutputStream (fd));
|
|---|
| 73 | in = new DataInputStream (new FileInputStream (fd));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | public RandomAccessFile (File file, String mode) throws IOException
|
|---|
| 77 | {
|
|---|
| 78 | this (file.getPath(), mode);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | public int read () throws IOException
|
|---|
| 82 | {
|
|---|
| 83 | return in.read();
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | public int read (byte[] buffer) throws IOException
|
|---|
| 87 | {
|
|---|
| 88 | return in.read(buffer);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public int read (byte[] buffer, int offset, int count) throws IOException
|
|---|
| 92 | {
|
|---|
| 93 | return in.read(buffer, offset, count);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | public final boolean readBoolean () throws IOException
|
|---|
| 97 | {
|
|---|
| 98 | return in.readBoolean();
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | public final byte readByte () throws IOException
|
|---|
| 102 | {
|
|---|
| 103 | return in.readByte();
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | public final char readChar () throws IOException
|
|---|
| 107 | {
|
|---|
| 108 | return in.readChar();
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | public final double readDouble () throws IOException
|
|---|
| 112 | {
|
|---|
| 113 | return in.readDouble();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public final float readFloat () throws IOException
|
|---|
| 117 | {
|
|---|
| 118 | return in.readFloat();
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | public final void readFully (byte[] buffer) throws IOException
|
|---|
| 122 | {
|
|---|
| 123 | in.readFully(buffer);
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | public final void readFully (byte[] buffer, int offset, int count)
|
|---|
| 127 | throws IOException
|
|---|
| 128 | {
|
|---|
| 129 | in.readFully(buffer, offset, count);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public final int readInt () throws IOException
|
|---|
| 133 | {
|
|---|
| 134 | return in.readInt();
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | public final String readLine () throws IOException
|
|---|
| 138 | {
|
|---|
| 139 | return in.readLine();
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | public final long readLong () throws IOException
|
|---|
| 143 | {
|
|---|
| 144 | return in.readLong();
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | public final short readShort () throws IOException
|
|---|
| 148 | {
|
|---|
| 149 | return in.readShort();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | public final int readUnsignedByte () throws IOException
|
|---|
| 153 | {
|
|---|
| 154 | return in.readUnsignedByte();
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | public final int readUnsignedShort () throws IOException
|
|---|
| 158 | {
|
|---|
| 159 | return in.readUnsignedShort();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | public final String readUTF () throws IOException
|
|---|
| 163 | {
|
|---|
| 164 | return in.readUTF();
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | public void seek (long pos) throws IOException
|
|---|
| 168 | {
|
|---|
| 169 | fd.seek(pos, FileDescriptor.SET, false);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | public int skipBytes (int count) throws IOException
|
|---|
| 173 | {
|
|---|
| 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);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | public void write (int oneByte) throws IOException
|
|---|
| 182 | {
|
|---|
| 183 | out.write(oneByte);
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | public void write (byte[] buffer) throws IOException
|
|---|
| 187 | {
|
|---|
| 188 | out.write(buffer);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | public void write (byte[] buffer, int offset, int count) throws IOException
|
|---|
| 192 | {
|
|---|
| 193 | out.write(buffer, offset, count);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | public final void writeBoolean (boolean val) throws IOException
|
|---|
| 197 | {
|
|---|
| 198 | out.writeBoolean(val);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | public final void writeByte (int v) throws IOException
|
|---|
| 202 | {
|
|---|
| 203 | out.writeByte(v);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | public final void writeShort (int v) throws IOException
|
|---|
| 207 | {
|
|---|
| 208 | out.writeShort(v);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | public final void writeChar (int v) throws IOException
|
|---|
| 212 | {
|
|---|
| 213 | out.writeChar(v);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | public final void writeInt (int v) throws IOException
|
|---|
| 217 | {
|
|---|
| 218 | out.writeInt(v);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | public final void writeLong (long v) throws IOException
|
|---|
| 222 | {
|
|---|
| 223 | out.writeLong(v);
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | public final void writeFloat (float v) throws IOException
|
|---|
| 227 | {
|
|---|
| 228 | out.writeFloat(v);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | public final void writeDouble (double v) throws IOException
|
|---|
| 232 | {
|
|---|
| 233 | out.writeDouble(v);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | public final void writeBytes (String s) throws IOException
|
|---|
| 237 | {
|
|---|
| 238 | out.writeBytes(s);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | public final void writeChars (String s) throws IOException
|
|---|
| 242 | {
|
|---|
| 243 | out.writeChars(s);
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | public final void writeUTF (String s) throws IOException
|
|---|
| 247 | {
|
|---|
| 248 | out.writeUTF(s);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | // The underlying file.
|
|---|
| 253 | private FileDescriptor fd;
|
|---|
| 254 | // The corresponding input and output streams.
|
|---|
| 255 | private DataOutputStream out;
|
|---|
| 256 | private DataInputStream in;
|
|---|
| 257 | }
|
|---|