45 * method.
46 * <p>
47 * It is generally true of all the reading routines in this class that
48 * if end-of-file is reached before the desired number of bytes has been
49 * read, an {@code EOFException} (which is a kind of
50 * {@code IOException}) is thrown. If any byte cannot be read for
51 * any reason other than end-of-file, an {@code IOException} other
52 * than {@code EOFException} is thrown. In particular, an
53 * {@code IOException} may be thrown if the stream has been closed.
54 *
55 * @author unascribed
56 * @since JDK1.0
57 */
58
59 public class RandomAccessFile implements DataOutput, DataInput, Closeable {
60
61 private FileDescriptor fd;
62 private FileChannel channel = null;
63 private boolean rw;
64
65 private Object closeLock = new Object();
66 private volatile boolean closed = false;
67
68 private static final int O_RDONLY = 1;
69 private static final int O_RDWR = 2;
70 private static final int O_SYNC = 4;
71 private static final int O_DSYNC = 8;
72
73 /**
74 * Creates a random access file stream to read from, and optionally
75 * to write to, a file with the specified name. A new
76 * {@link FileDescriptor} object is created to represent the
77 * connection to the file.
78 *
79 * <p> The <tt>mode</tt> argument specifies the access mode with which the
80 * file is to be opened. The permitted values and their meanings are as
81 * specified for the <a
82 * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
83 *
84 * <p>
216 if (imode < 0)
217 throw new IllegalArgumentException("Illegal mode \"" + mode
218 + "\" must be one of "
219 + "\"r\", \"rw\", \"rws\","
220 + " or \"rwd\"");
221 SecurityManager security = System.getSecurityManager();
222 if (security != null) {
223 security.checkRead(name);
224 if (rw) {
225 security.checkWrite(name);
226 }
227 }
228 if (name == null) {
229 throw new NullPointerException();
230 }
231 if (file.isInvalid()) {
232 throw new FileNotFoundException("Invalid file path");
233 }
234 fd = new FileDescriptor();
235 fd.attach(this);
236 open(name, imode);
237 }
238
239 /**
240 * Returns the opaque file descriptor object associated with this
241 * stream.
242 *
243 * @return the file descriptor object associated with this stream.
244 * @exception IOException if an I/O error occurs.
245 * @see java.io.FileDescriptor
246 */
247 public final FileDescriptor getFD() throws IOException {
248 if (fd != null) {
249 return fd;
250 }
251 throw new IOException();
252 }
253
254 /**
255 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
256 * object associated with this file.
257 *
258 * <p> The {@link java.nio.channels.FileChannel#position()
259 * position} of the returned channel will always be equal to
260 * this object's file-pointer offset as returned by the {@link
261 * #getFilePointer getFilePointer} method. Changing this object's
262 * file-pointer offset, whether explicitly or by reading or writing bytes,
263 * will change the position of the channel, and vice versa. Changing the
264 * file's length via this object will change the length seen via the file
265 * channel, and vice versa.
266 *
267 * @return the file channel associated with this file
268 *
269 * @since 1.4
270 * @spec JSR-51
271 */
272 public final FileChannel getChannel() {
273 synchronized (this) {
274 if (channel == null) {
275 channel = FileChannelImpl.open(fd, true, rw, this);
276 }
277 return channel;
278 }
279 }
280
281 /**
282 * Opens a file and returns the file descriptor. The file is
283 * opened in read-write mode if the O_RDWR bit in {@code mode}
284 * is true, else the file is opened as read-only.
285 * If the {@code name} refers to a directory, an IOException
286 * is thrown.
287 *
288 * @param name the name of the file
289 * @param mode the mode flags, a combination of the O_ constants
290 * defined above
291 */
292 private native void open(String name, int mode)
293 throws FileNotFoundException;
294
295 // 'Read' primitives
|
45 * method.
46 * <p>
47 * It is generally true of all the reading routines in this class that
48 * if end-of-file is reached before the desired number of bytes has been
49 * read, an {@code EOFException} (which is a kind of
50 * {@code IOException}) is thrown. If any byte cannot be read for
51 * any reason other than end-of-file, an {@code IOException} other
52 * than {@code EOFException} is thrown. In particular, an
53 * {@code IOException} may be thrown if the stream has been closed.
54 *
55 * @author unascribed
56 * @since JDK1.0
57 */
58
59 public class RandomAccessFile implements DataOutput, DataInput, Closeable {
60
61 private FileDescriptor fd;
62 private FileChannel channel = null;
63 private boolean rw;
64
65 /**
66 * The path of the referenced file
67 * (null if the stream is created with a file descriptor)
68 */
69 private final String path;
70
71 private Object closeLock = new Object();
72 private volatile boolean closed = false;
73
74 private static final int O_RDONLY = 1;
75 private static final int O_RDWR = 2;
76 private static final int O_SYNC = 4;
77 private static final int O_DSYNC = 8;
78
79 /**
80 * Creates a random access file stream to read from, and optionally
81 * to write to, a file with the specified name. A new
82 * {@link FileDescriptor} object is created to represent the
83 * connection to the file.
84 *
85 * <p> The <tt>mode</tt> argument specifies the access mode with which the
86 * file is to be opened. The permitted values and their meanings are as
87 * specified for the <a
88 * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor.
89 *
90 * <p>
222 if (imode < 0)
223 throw new IllegalArgumentException("Illegal mode \"" + mode
224 + "\" must be one of "
225 + "\"r\", \"rw\", \"rws\","
226 + " or \"rwd\"");
227 SecurityManager security = System.getSecurityManager();
228 if (security != null) {
229 security.checkRead(name);
230 if (rw) {
231 security.checkWrite(name);
232 }
233 }
234 if (name == null) {
235 throw new NullPointerException();
236 }
237 if (file.isInvalid()) {
238 throw new FileNotFoundException("Invalid file path");
239 }
240 fd = new FileDescriptor();
241 fd.attach(this);
242 path = name;
243 open(name, imode);
244 }
245
246 /**
247 * Returns the opaque file descriptor object associated with this
248 * stream.
249 *
250 * @return the file descriptor object associated with this stream.
251 * @exception IOException if an I/O error occurs.
252 * @see java.io.FileDescriptor
253 */
254 public final FileDescriptor getFD() throws IOException {
255 if (fd != null) {
256 return fd;
257 }
258 throw new IOException();
259 }
260
261 /**
262 * Returns the unique {@link java.nio.channels.FileChannel FileChannel}
263 * object associated with this file.
264 *
265 * <p> The {@link java.nio.channels.FileChannel#position()
266 * position} of the returned channel will always be equal to
267 * this object's file-pointer offset as returned by the {@link
268 * #getFilePointer getFilePointer} method. Changing this object's
269 * file-pointer offset, whether explicitly or by reading or writing bytes,
270 * will change the position of the channel, and vice versa. Changing the
271 * file's length via this object will change the length seen via the file
272 * channel, and vice versa.
273 *
274 * @return the file channel associated with this file
275 *
276 * @since 1.4
277 * @spec JSR-51
278 */
279 public final FileChannel getChannel() {
280 synchronized (this) {
281 if (channel == null) {
282 channel = FileChannelImpl.open(fd, path, true, rw, this);
283 }
284 return channel;
285 }
286 }
287
288 /**
289 * Opens a file and returns the file descriptor. The file is
290 * opened in read-write mode if the O_RDWR bit in {@code mode}
291 * is true, else the file is opened as read-only.
292 * If the {@code name} refers to a directory, an IOException
293 * is thrown.
294 *
295 * @param name the name of the file
296 * @param mode the mode flags, a combination of the O_ constants
297 * defined above
298 */
299 private native void open(String name, int mode)
300 throws FileNotFoundException;
301
302 // 'Read' primitives
|