< prev index next >

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

Print this page
@@ -26,10 +26,11 @@
  package java.io;
  
  import java.nio.channels.FileChannel;
  import jdk.internal.access.SharedSecrets;
  import jdk.internal.access.JavaIOFileDescriptorAccess;
+ import jdk.internal.misc.Blocker;
  import sun.nio.ch.FileChannelImpl;
  
  
  /**
   * A file output stream is an output stream for writing data to a

@@ -286,13 +287,16 @@
      /**
       * Opens a file, with the specified name, for overwriting or appending.
       * @param name name of file to be opened
       * @param append whether the file is to be opened in append mode
       */
-     private void open(String name, boolean append)
-         throws FileNotFoundException {
-         open0(name, append);
+     private void open(String name, boolean append) throws FileNotFoundException {
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> open0(name, append));
+         } else {
+             open0(name, append);
+         }
      }
  
      /**
       * Writes the specified byte to this file output stream.
       *

@@ -308,11 +312,16 @@
       *
       * @param      b   the byte to be written.
       * @throws     IOException  if an I/O error occurs.
       */
      public void write(int b) throws IOException {
-         write(b, fdAccess.getAppend(fd));
+         boolean append = fdAccess.getAppend(fd);
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> write(b, append));
+         } else {
+             write(b, append);
+         }
      }
  
      /**
       * Writes a sub array as a sequence of bytes.
       * @param b the data to be written

@@ -331,11 +340,16 @@
       *
       * @param      b   the data.
       * @throws     IOException  if an I/O error occurs.
       */
      public void write(byte[] b) throws IOException {
-         writeBytes(b, 0, b.length, fdAccess.getAppend(fd));
+         boolean append = fdAccess.getAppend(fd);
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> writeBytes(b, 0, b.length, append));
+         } else {
+             writeBytes(b, 0, b.length, append);
+         }
      }
  
      /**
       * Writes {@code len} bytes from the specified byte array
       * starting at offset {@code off} to this file output stream.

@@ -344,11 +358,16 @@
       * @param      off   the start offset in the data.
       * @param      len   the number of bytes to write.
       * @throws     IOException  if an I/O error occurs.
       */
      public void write(byte[] b, int off, int len) throws IOException {
-         writeBytes(b, off, len, fdAccess.getAppend(fd));
+         boolean append = fdAccess.getAppend(fd);
+         if (Thread.currentThread().isVirtual()) {
+             Blocker.managedBlock(() -> writeBytes(b, off, len, append));
+         } else {
+             writeBytes(b, off, len, append);
+         }
      }
  
      /**
       * Closes this file output stream and releases any system resources
       * associated with this stream. This file output stream may no longer
< prev index next >