< prev index next >

src/java.base/share/classes/java/io/RandomAccessFile.java

Print this page
@@ -27,10 +27,11 @@
  
  import java.nio.channels.FileChannel;
  
  import jdk.internal.access.JavaIORandomAccessFileAccess;
  import jdk.internal.access.SharedSecrets;
+ import jdk.internal.misc.Blocker;
  import sun.nio.ch.FileChannelImpl;
  
  
  /**
   * Instances of this class support both reading and writing to a

@@ -337,13 +338,16 @@
       *
       * @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);
+     private void open(String name, int mode) throws FileNotFoundException {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> open0(name, mode));
+         } else {
+             open0(name, mode);
+         }
      }
  
      // 'Read' primitives
  
      /**

@@ -360,11 +364,15 @@
       *             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();
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> read0());
+         } else {
+             return read0();
+         }
      }
  
      private native int read0() throws IOException;
  
      /**

@@ -372,11 +380,19 @@
       * @param     b the buffer into which the data is read.
       * @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 native int readBytes(byte[] b, int off, int len) throws IOException;
+     private int readBytes(byte[] b, int off, int len) throws IOException {
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> readBytes0(b, off, len));
+         } else {
+             return readBytes0(b, off, len);
+         }
+     }
+ 
+     private native int readBytes0(byte[] b, int off, int len) throws IOException;
  
      /**
       * Reads up to {@code len} bytes of data from this file into an
       * array of bytes. This method blocks until at least one byte of input
       * is available.

@@ -400,11 +416,15 @@
       * @throws     IndexOutOfBoundsException If {@code off} is negative,
       *             {@code len} is negative, or {@code len} is greater than
       *             {@code b.length - off}
       */
      public int read(byte[] b, int off, int len) throws IOException {
-         return readBytes(b, off, len);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> readBytes(b, off, len));
+         } else {
+             return readBytes(b, off, len);
+         }
      }
  
      /**
       * Reads up to {@code b.length} bytes of data from this file
       * into an array of bytes. This method blocks until at least one byte

@@ -423,11 +443,11 @@
       *             other than end of file, or if the random access file has been closed,
       *             or if some other I/O error occurs.
       * @throws     NullPointerException If {@code b} is {@code null}.
       */
      public int read(byte[] b) throws IOException {
-         return readBytes(b, 0, b.length);
+         return read(b, 0, b.length);
      }
  
      /**
       * Reads {@code b.length} bytes from this file into the byte
       * array, starting at the current file pointer. This method reads

@@ -517,11 +537,15 @@
       *
       * @param      b   the {@code byte} to be written.
       * @throws     IOException  if an I/O error occurs.
       */
      public void write(int b) throws IOException {
-         write0(b);
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> write0(b));
+         } else {
+             write0(b);
+         }
      }
  
      private native void write0(int b) throws IOException;
  
      /**

@@ -530,21 +554,29 @@
       * @param     b the data to be written
       * @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 native void writeBytes(byte[] b, int off, int len) throws IOException;
+     private void writeBytes(byte[] b, int off, int len) throws IOException {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> writeBytes0(b, off, len));
+         } else {
+             writeBytes0(b, off, len);
+         }
+     }
+ 
+     private native void writeBytes0(byte[] b, int off, int len) throws IOException;
  
      /**
       * Writes {@code b.length} bytes from the specified byte array
       * to this file, starting at the current file pointer.
       *
       * @param      b   the data.
       * @throws     IOException  if an I/O error occurs.
       */
      public void write(byte[] b) throws IOException {
-         writeBytes(b, 0, b.length);
+         write(b, 0, b.length);
      }
  
      /**
       * Writes {@code len} bytes from the specified byte array
       * starting at offset {@code off} to this file.

@@ -584,10 +616,13 @@
       *                          {@code 0} or if an I/O error occurs.
       */
      public void seek(long pos) throws IOException {
          if (pos < 0) {
              throw new IOException("Negative seek offset");
+         }
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> seek0(pos));
          } else {
              seek0(pos);
          }
      }
  

@@ -597,11 +632,19 @@
       * Returns the length of this file.
       *
       * @return     the length of this file, measured in bytes.
       * @throws     IOException  if an I/O error occurs.
       */
-     public native long length() throws IOException;
+     public long length() throws IOException {
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> length0());
+         } else {
+             return length0();
+         }
+     }
+ 
+     private native long length0() throws IOException;
  
      /**
       * Sets the length of this file.
       *
       * <p> If the present length of the file as returned by the

@@ -618,11 +661,19 @@
       *
       * @param      newLength    The desired length of the file
       * @throws     IOException  If an I/O error occurs
       * @since      1.2
       */
-     public native void setLength(long newLength) throws IOException;
+     public void setLength(long newLength) throws IOException {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> setLength0(newLength));
+         } else {
+             setLength0(newLength);
+         }
+     }
+ 
+     private native void setLength0(long newLength) throws IOException;
  
      /**
       * Closes this random access file stream and releases any system
       * resources associated with the stream. A closed random access
       * file cannot perform input or output operations and cannot be
< prev index next >