< prev index next >

src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java

Print this page
*** 76,10 ***
--- 76,11 ---
      private final FileDescriptor fd;
  
      // File access mode (immutable)
      private final boolean writable;
      private final boolean readable;
+     private final boolean sync;  // O_SYNC or O_DSYNC
  
      // Required to prevent finalization of creating stream (immutable)
      private final Closeable parent;
  
      // The path of the referenced file

*** 120,16 ***
              }
          }
      }
  
      private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
!                             boolean writable, boolean direct, Closeable parent)
      {
          this.fd = fd;
          this.path = path;
          this.readable = readable;
          this.writable = writable;
          this.direct = direct;
          this.parent = parent;
          if (direct) {
              assert path != null;
              this.alignment = nd.setDirectIO(fd, path);
--- 121,18 ---
              }
          }
      }
  
      private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
!                             boolean writable, boolean sync, boolean direct,
+                             Closeable parent)
      {
          this.fd = fd;
          this.path = path;
          this.readable = readable;
          this.writable = writable;
+         this.sync = sync;
          this.direct = direct;
          this.parent = parent;
          if (direct) {
              assert path != null;
              this.alignment = nd.setDirectIO(fd, path);

*** 148,13 ***
  
      // Used by FileInputStream::getChannel, FileOutputStream::getChannel,
      // and RandomAccessFile::getChannel
      public static FileChannel open(FileDescriptor fd, String path,
                                     boolean readable, boolean writable,
!                                    boolean direct, Closeable parent)
      {
!         return new FileChannelImpl(fd, path, readable, writable, direct, parent);
      }
  
      private void ensureOpen() throws IOException {
          if (!isOpen())
              throw new ClosedChannelException();
--- 151,13 ---
  
      // Used by FileInputStream::getChannel, FileOutputStream::getChannel,
      // and RandomAccessFile::getChannel
      public static FileChannel open(FileDescriptor fd, String path,
                                     boolean readable, boolean writable,
!                                    boolean sync, boolean direct, Closeable parent)
      {
!         return new FileChannelImpl(fd, path, readable, writable, sync, direct, parent);
      }
  
      private void ensureOpen() throws IOException {
          if (!isOpen())
              throw new ClosedChannelException();

*** 228,15 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     long comp = Blocker.begin();
                      try {
                          n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
                      } finally {
!                         Blocker.end(comp);
                      }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
--- 231,15 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     boolean attempted = Blocker.begin(direct);
                      try {
                          n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
                      } finally {
!                         Blocker.end(attempted);
                      }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);

*** 263,15 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     long comp = Blocker.begin();
                      try {
                          n = IOUtil.read(fd, dsts, offset, length, direct, alignment, nd);
                      } finally {
!                         Blocker.end(comp);
                      }
  
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
--- 266,15 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     boolean attempted = Blocker.begin(direct);
                      try {
                          n = IOUtil.read(fd, dsts, offset, length, direct, alignment, nd);
                      } finally {
!                         Blocker.end(attempted);
                      }
  
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {

*** 296,15 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     long comp = Blocker.begin();
                      try {
                          n = IOUtil.write(fd, src, -1, direct, alignment, nd);
                      } finally {
!                         Blocker.end(comp);
                      }
  
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
--- 299,15 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     boolean attempted = Blocker.begin(sync | direct);
                      try {
                          n = IOUtil.write(fd, src, -1, direct, alignment, nd);
                      } finally {
!                         Blocker.end(attempted);
                      }
  
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {

*** 332,15 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     long comp = Blocker.begin();
                      try {
                          n = IOUtil.write(fd, srcs, offset, length, direct, alignment, nd);
                      } finally {
!                         Blocker.end(comp);
                      }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);
--- 335,15 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  do {
!                     boolean attempted = Blocker.begin(sync | direct);
                      try {
                          n = IOUtil.write(fd, srcs, offset, length, direct, alignment, nd);
                      } finally {
!                         Blocker.end(attempted);
                      }
                  } while ((n == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(n);
              } finally {
                  threads.remove(ti);

*** 363,17 ***
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  boolean append = fdAccess.getAppend(fd);
                  do {
!                     long comp = Blocker.begin();
!                     try {
-                         // in append-mode then position is advanced to end before writing
-                         p = (append) ? nd.size(fd) : nd.seek(fd, -1);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(p);
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);
--- 366,12 ---
                  ti = threads.add();
                  if (!isOpen())
                      return 0;
                  boolean append = fdAccess.getAppend(fd);
                  do {
!                     // in append-mode then position is advanced to end before writing
!                     p = (append) ? nd.size(fd) : nd.seek(fd, -1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(p);
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);

*** 394,16 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return null;
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         p = nd.seek(fd, newPosition);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);
--- 392,11 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return null;
                  do {
!                     p = nd.seek(fd, newPosition);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(p > -1);

*** 422,16 ***
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return -1;
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         s = nd.size(fd);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((s == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(s);
              } finally {
                  threads.remove(ti);
                  endBlocking(s > -1);
--- 415,11 ---
                  beginBlocking();
                  ti = threads.add();
                  if (!isOpen())
                      return -1;
                  do {
!                     s = nd.size(fd);
                  } while ((s == IOStatus.INTERRUPTED) && isOpen());
                  return IOStatus.normalize(s);
              } finally {
                  threads.remove(ti);
                  endBlocking(s > -1);

*** 459,57 ***
                      return null;
  
                  // get current size
                  long size;
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         size = nd.size(fd);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((size == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  // get current position
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         p = nd.seek(fd, -1);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
                  assert p >= 0;
  
                  // truncate file if given size is less than the current size
                  if (newSize < size) {
                      do {
!                         long comp = Blocker.begin();
-                         try {
-                             rv = nd.truncate(fd, newSize);
-                         } finally {
-                             Blocker.end(comp);
-                         }
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
                  // if position is beyond new size then adjust it
                  if (p > newSize)
                      p = newSize;
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         rp = nd.seek(fd, p);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((rp == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(rv > -1);
--- 447,37 ---
                      return null;
  
                  // get current size
                  long size;
                  do {
!                     size = nd.size(fd);
                  } while ((size == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  // get current position
                  do {
!                     p = nd.seek(fd, -1);
                  } while ((p == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
                  assert p >= 0;
  
                  // truncate file if given size is less than the current size
                  if (newSize < size) {
                      do {
!                         rv = nd.truncate(fd, newSize);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
                  // if position is beyond new size then adjust it
                  if (p > newSize)
                      p = newSize;
                  do {
!                     rp = nd.seek(fd, p);
                  } while ((rp == IOStatus.INTERRUPTED) && isOpen());
                  return this;
              } finally {
                  threads.remove(ti);
                  endBlocking(rv > -1);

*** 527,15 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return;
              do {
!                 long comp = Blocker.begin();
                  try {
                      rv = nd.force(fd, metaData);
                  } finally {
!                     Blocker.end(comp);
                  }
              } while ((rv == IOStatus.INTERRUPTED) && isOpen());
          } finally {
              threads.remove(ti);
              endBlocking(rv > -1);
--- 495,15 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return;
              do {
!                 boolean attempted = Blocker.begin();
                  try {
                      rv = nd.force(fd, metaData);
                  } finally {
!                     Blocker.end(attempted);
                  }
              } while ((rv == IOStatus.INTERRUPTED) && isOpen());
          } finally {
              threads.remove(ti);
              endBlocking(rv > -1);

*** 622,16 ***
       */
      private long transferToFileDescriptor(long position, int count, FileDescriptor targetFD) {
          long n;
          boolean append = fdAccess.getAppend(targetFD);
          do {
!             long comp = Blocker.begin();
-             try {
-                 n = nd.transferTo(fd, position, count, targetFD, append);
-             } finally {
-                 Blocker.end(comp);
-             }
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return n;
      }
  
      /**
--- 590,11 ---
       */
      private long transferToFileDescriptor(long position, int count, FileDescriptor targetFD) {
          long n;
          boolean append = fdAccess.getAppend(targetFD);
          do {
!             n = nd.transferTo(fd, position, count, targetFD, append);
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return n;
      }
  
      /**

*** 893,16 ***
       */
      private long transferFromFileDescriptor(FileDescriptor srcFD, long position, long count) {
          long n;
          boolean append = fdAccess.getAppend(fd);
          do {
!             long comp = Blocker.begin();
-             try {
-                 n = nd.transferFrom(srcFD, fd, position, count, append);
-             } finally {
-                 Blocker.end(comp);
-             }
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return n;
      }
  
      /**
--- 856,11 ---
       */
      private long transferFromFileDescriptor(FileDescriptor srcFD, long position, long count) {
          long n;
          boolean append = fdAccess.getAppend(fd);
          do {
!             n = nd.transferFrom(srcFD, fd, position, count, append);
          } while ((n == IOStatus.INTERRUPTED) && isOpen());
          return n;
      }
  
      /**

*** 1086,15 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 long comp = Blocker.begin();
                  try {
                      n = IOUtil.read(fd, dst, position, direct, alignment, nd);
                  } finally {
!                     Blocker.end(comp);
                  }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
--- 1044,15 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 boolean attempted = Blocker.begin(direct);
                  try {
                      n = IOUtil.read(fd, dst, position, direct, alignment, nd);
                  } finally {
!                     Blocker.end(attempted);
                  }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);

*** 1131,15 ***
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 long comp = Blocker.begin();
                  try {
                      n = IOUtil.write(fd, src, position, direct, alignment, nd);
                  } finally {
!                     Blocker.end(comp);
                  }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);
--- 1089,15 ---
              beginBlocking();
              ti = threads.add();
              if (!isOpen())
                  return -1;
              do {
!                 boolean attempted = Blocker.begin(sync | direct);
                  try {
                      n = IOUtil.write(fd, src, position, direct, alignment, nd);
                  } finally {
!                     Blocker.end(attempted);
                  }
              } while ((n == IOStatus.INTERRUPTED) && isOpen());
              return IOStatus.normalize(n);
          } finally {
              threads.remove(ti);

*** 1360,16 ***
              long mapSize;
              int pagePosition;
              synchronized (positionLock) {
                  long filesize;
                  do {
!                     long comp = Blocker.begin();
-                     try {
-                         filesize = nd.size(fd);
-                     } finally {
-                         Blocker.end(comp);
-                     }
                  } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  if (filesize < position + size) { // Extend file size
--- 1318,11 ---
              long mapSize;
              int pagePosition;
              synchronized (positionLock) {
                  long filesize;
                  do {
!                     filesize = nd.size(fd);
                  } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
                  if (!isOpen())
                      return null;
  
                  if (filesize < position + size) { // Extend file size

*** 1377,16 ***
                          throw new IOException("Channel not open for writing " +
                              "- cannot extend file to required size");
                      }
                      int rv;
                      do {
!                         long comp = Blocker.begin();
-                         try {
-                             rv = nd.truncate(fd, position + size);
-                         } finally {
-                             Blocker.end(comp);
-                         }
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  
--- 1330,11 ---
                          throw new IOException("Channel not open for writing " +
                              "- cannot extend file to required size");
                      }
                      int rv;
                      do {
!                         rv = nd.truncate(fd, position + size);
                      } while ((rv == IOStatus.INTERRUPTED) && isOpen());
                      if (!isOpen())
                          return null;
                  }
  

*** 1573,15 ***
              ti = threads.add();
              if (!isOpen())
                  return null;
              int n;
              do {
!                 long comp = Blocker.begin();
                  try {
                      n = nd.lock(fd, true, position, size, shared);
                  } finally {
!                     Blocker.end(comp);
                  }
              } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
              if (isOpen()) {
                  if (n == FileDispatcher.RET_EX_LOCK) {
                      assert shared;
--- 1521,15 ---
              ti = threads.add();
              if (!isOpen())
                  return null;
              int n;
              do {
!                 boolean attempted = Blocker.begin();
                  try {
                      n = nd.lock(fd, true, position, size, shared);
                  } finally {
!                     Blocker.end(attempted);
                  }
              } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
              if (isOpen()) {
                  if (n == FileDispatcher.RET_EX_LOCK) {
                      assert shared;
< prev index next >