< prev index next > src/java.base/share/classes/java/io/RandomAccessFile.java
Print this page
private static final int O_TEMPORARY = 16;
private final FileDescriptor fd;
private final boolean rw;
+ private final boolean sync; // O_SYNC or O_DSYNC
/**
* The path of the referenced file
* (null if the stream is created with a file descriptor)
*/
{
String name = (file != null ? file.getPath() : null);
int imode = -1;
boolean rw = false;
if (mode.equals("r"))
imode = O_RDONLY;
else if (mode.startsWith("rw")) {
imode = O_RDWR;
rw = true;
if (mode.length() > 2) {
! if (mode.equals("rws"))
imode |= O_SYNC;
! else if (mode.equals("rwd"))
imode |= O_DSYNC;
! else
imode = -1;
}
}
this.rw = rw;
if (openAndDelete)
imode |= O_TEMPORARY;
if (imode < 0)
throw new IllegalArgumentException("Illegal mode \"" + mode
{
String name = (file != null ? file.getPath() : null);
int imode = -1;
boolean rw = false;
+ boolean sync = false;
if (mode.equals("r"))
imode = O_RDONLY;
else if (mode.startsWith("rw")) {
imode = O_RDWR;
rw = true;
if (mode.length() > 2) {
! if (mode.equals("rws")) {
imode |= O_SYNC;
! sync = true;
+ } else if (mode.equals("rwd")) {
imode |= O_DSYNC;
! sync = true;
+ } else
imode = -1;
}
}
this.rw = rw;
+ this.sync = sync;
if (openAndDelete)
imode |= O_TEMPORARY;
if (imode < 0)
throw new IllegalArgumentException("Illegal mode \"" + mode
FileChannel fc = this.channel;
if (fc == null) {
synchronized (this) {
fc = this.channel;
if (fc == null) {
! this.channel = fc = FileChannelImpl.open(fd, path, true,
! rw, false, this);
if (closed) {
try {
fc.close();
} catch (IOException ioe) {
throw new InternalError(ioe); // should not happen
FileChannel fc = this.channel;
if (fc == null) {
synchronized (this) {
fc = this.channel;
if (fc == null) {
! fc = FileChannelImpl.open(fd, path, true, rw, sync, false, this);
! this.channel = fc;
if (closed) {
try {
fc.close();
} catch (IOException ioe) {
throw new InternalError(ioe); // should not happen
* @param name the name of the file
* @param mode the mode flags, a combination of the O_ constants
* defined above
*/
private void open(String name, int mode) throws FileNotFoundException {
! long comp = Blocker.begin();
- try {
- open0(name, mode);
- } finally {
- Blocker.end(comp);
- }
}
// 'Read' primitives
/**
* @param name the name of the file
* @param mode the mode flags, a combination of the O_ constants
* defined above
*/
private void open(String name, int mode) throws FileNotFoundException {
! open0(name, mode);
}
// 'Read' primitives
/**
* file has been reached.
* @throws IOException if an I/O error occurs. Not thrown if
* end-of-file has been reached.
*/
public int read() throws IOException {
! long comp = Blocker.begin();
- try {
- return read0();
- } finally {
- Blocker.end(comp);
- }
}
private native int read0() throws IOException;
/**
* file has been reached.
* @throws IOException if an I/O error occurs. Not thrown if
* end-of-file has been reached.
*/
public int read() throws IOException {
! return read0();
}
private native int read0() throws IOException;
/**
* @param off the start offset of the data.
* @param len the number of bytes to read.
* @throws IOException If an I/O error has occurred.
*/
private int readBytes(byte[] b, int off, int len) throws IOException {
! long comp = Blocker.begin();
- try {
- return readBytes0(b, off, len);
- } finally {
- Blocker.end(comp);
- }
}
private native int readBytes0(byte[] b, int off, int len) throws IOException;
/**
* @param off the start offset of the data.
* @param len the number of bytes to read.
* @throws IOException If an I/O error has occurred.
*/
private int readBytes(byte[] b, int off, int len) throws IOException {
! return readBytes0(b, off, len);
}
private native int readBytes0(byte[] b, int off, int len) throws IOException;
/**
*
* @param b the {@code byte} to be written.
* @throws IOException if an I/O error occurs.
*/
public void write(int b) throws IOException {
! long comp = Blocker.begin();
try {
write0(b);
} finally {
! Blocker.end(comp);
}
}
private native void write0(int b) throws IOException;
*
* @param b the {@code byte} to be written.
* @throws IOException if an I/O error occurs.
*/
public void write(int b) throws IOException {
! boolean attempted = Blocker.begin(sync);
try {
write0(b);
} finally {
! Blocker.end(attempted);
}
}
private native void write0(int b) throws IOException;
* @param off the start offset in the data
* @param len the number of bytes that are written
* @throws IOException If an I/O error has occurred.
*/
private void writeBytes(byte[] b, int off, int len) throws IOException {
! long comp = Blocker.begin();
try {
writeBytes0(b, off, len);
} finally {
! Blocker.end(comp);
}
}
private native void writeBytes0(byte[] b, int off, int len) throws IOException;
* @param off the start offset in the data
* @param len the number of bytes that are written
* @throws IOException If an I/O error has occurred.
*/
private void writeBytes(byte[] b, int off, int len) throws IOException {
! boolean attempted = Blocker.begin(sync);
try {
writeBytes0(b, off, len);
} finally {
! Blocker.end(attempted);
}
}
private native void writeBytes0(byte[] b, int off, int len) throws IOException;
*/
public void seek(long pos) throws IOException {
if (pos < 0) {
throw new IOException("Negative seek offset");
}
! long comp = Blocker.begin();
- try {
- seek0(pos);
- } finally {
- Blocker.end(comp);
- }
}
private native void seek0(long pos) throws IOException;
/**
*/
public void seek(long pos) throws IOException {
if (pos < 0) {
throw new IOException("Negative seek offset");
}
! seek0(pos);
}
private native void seek0(long pos) throws IOException;
/**
*
* @return the length of this file, measured in bytes.
* @throws IOException if an I/O error occurs.
*/
public long length() throws IOException {
! long comp = Blocker.begin();
- try {
- return length0();
- } finally {
- Blocker.end(comp);
- }
}
private native long length0() throws IOException;
/**
*
* @return the length of this file, measured in bytes.
* @throws IOException if an I/O error occurs.
*/
public long length() throws IOException {
! return length0();
}
private native long length0() throws IOException;
/**
* @throws IOException If the argument is negative or
* if some other I/O error occurs
* @since 1.2
*/
public void setLength(long newLength) throws IOException {
! long comp = Blocker.begin();
- try {
- setLength0(newLength);
- } finally {
- Blocker.end(comp);
- }
}
private native void setLength0(long newLength) throws IOException;
/**
* @throws IOException If the argument is negative or
* if some other I/O error occurs
* @since 1.2
*/
public void setLength(long newLength) throws IOException {
! setLength0(newLength);
}
private native void setLength0(long newLength) throws IOException;
/**
< prev index next >