< prev index next >

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

Print this page
@@ -25,10 +25,11 @@
  
  package java.io;
  
  import java.nio.channels.FileChannel;
  import java.util.Arrays;
+ import jdk.internal.misc.Blocker;
  import jdk.internal.util.ArraysSupport;
  import sun.nio.ch.FileChannelImpl;
  
  /**
   * A {@code FileInputStream} obtains input bytes

@@ -211,11 +212,15 @@
      /**
       * Opens the specified file for reading.
       * @param name the name of the file
       */
      private void open(String name) throws FileNotFoundException {
-         open0(name);
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> open0(name));
+         } else {
+             open0(name);
+         }
      }
  
      /**
       * Reads a byte of data from this input stream. This method blocks
       * if no input is yet available.

@@ -223,11 +228,15 @@
       * @return     the next byte of data, or {@code -1} if the end of the
       *             file is reached.
       * @throws     IOException  if an I/O error occurs.
       */
      public int read() throws IOException {
-         return read0();
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> read0());
+         } else {
+             return read0();
+         }
      }
  
      private native int read0() throws IOException;
  
      /**

@@ -249,11 +258,15 @@
       *             {@code -1} if there is no more data because the end of
       *             the file has been reached.
       * @throws     IOException  if an I/O error occurs.
       */
      public int read(byte[] b) throws IOException {
-         return readBytes(b, 0, b.length);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> readBytes(b, 0, b.length));
+         } else {
+             return readBytes(b, 0, b.length);
+         }
      }
  
      /**
       * Reads up to {@code len} bytes of data from this input stream
       * into an array of bytes. If {@code len} is not zero, the method

@@ -271,11 +284,15 @@
       *             {@code len} is negative, or {@code len} is greater than
       *             {@code b.length - off}
       * @throws     IOException  if an I/O error occurs.
       */
      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);
+         }
      }
  
      public byte[] readAllBytes() throws IOException {
          long length = length();
          long position = position();

@@ -371,16 +388,24 @@
          }
          return transferred + super.transferTo(out);
      }
  
      private long length() throws IOException {
-         return length0();
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> length0());
+         } else {
+             return length0();
+         }
      }
      private native long length0() throws IOException;
  
      private long position() throws IOException {
-         return position0();
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> position0());
+         } else {
+             return position0();
+         }
      }
      private native long position0() throws IOException;
  
      /**
       * Skips over and discards {@code n} bytes of data from the

@@ -405,11 +430,15 @@
       * @return     the actual number of bytes skipped.
       * @throws     IOException  if n is negative, if the stream does not
       *             support seek, or if an I/O error occurs.
       */
      public long skip(long n) throws IOException {
-         return skip0(n);
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> skip0(n));
+         } else {
+             return skip0(n);
+         }
      }
  
      private native long skip0(long n) throws IOException;
  
      /**

@@ -428,11 +457,15 @@
       *             (or skipped over) from this input stream without blocking.
       * @throws     IOException  if this file input stream has been closed by calling
       *             {@code close} or an I/O error occurs.
       */
      public int available() throws IOException {
-         return available0();
+         if (Thread.currentThread().isVirtual()) {
+             return Blocker.managedBlock(() -> available0());
+         } else {
+             return available0();
+         }
      }
  
      private native int available0() throws IOException;
  
      /**
< prev index next >