jdk/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
changeset 27263 819f5f87d485
parent 25859 3317bb8137f4
child 27937 0c9f63e42e91
equal deleted inserted replaced
27262:82895f3a728a 27263:819f5f87d485
    42 import java.security.AccessController;
    42 import java.security.AccessController;
    43 import java.util.ArrayList;
    43 import java.util.ArrayList;
    44 import java.util.List;
    44 import java.util.List;
    45 
    45 
    46 import sun.misc.Cleaner;
    46 import sun.misc.Cleaner;
       
    47 import sun.misc.JavaIOFileDescriptorAccess;
       
    48 import sun.misc.SharedSecrets;
    47 import sun.security.action.GetPropertyAction;
    49 import sun.security.action.GetPropertyAction;
    48 
    50 
    49 public class FileChannelImpl
    51 public class FileChannelImpl
    50     extends FileChannel
    52     extends FileChannel
    51 {
    53 {
    52     // Memory allocation size for mapping buffers
    54     // Memory allocation size for mapping buffers
    53     private static final long allocationGranularity;
    55     private static final long allocationGranularity;
    54 
    56 
       
    57     // Access to FileDispatcher internals
       
    58     private static final JavaIOFileDescriptorAccess fdAccess =
       
    59         SharedSecrets.getJavaIOFileDescriptorAccess();
       
    60 
    55     // Used to make native read and write calls
    61     // Used to make native read and write calls
    56     private final FileDispatcher nd;
    62     private final FileDispatcher nd;
    57 
    63 
    58     // File descriptor
    64     // File descriptor
    59     private final FileDescriptor fd;
    65     private final FileDescriptor fd;
    60 
    66 
    61     // File access mode (immutable)
    67     // File access mode (immutable)
    62     private final boolean writable;
    68     private final boolean writable;
    63     private final boolean readable;
    69     private final boolean readable;
    64     private final boolean append;
       
    65 
    70 
    66     // Required to prevent finalization of creating stream (immutable)
    71     // Required to prevent finalization of creating stream (immutable)
    67     private final Object parent;
    72     private final Object parent;
    68 
    73 
    69     // The path of the referenced file
    74     // The path of the referenced file
    75 
    80 
    76     // Lock for operations involving position and size
    81     // Lock for operations involving position and size
    77     private final Object positionLock = new Object();
    82     private final Object positionLock = new Object();
    78 
    83 
    79     private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
    84     private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
    80                             boolean writable, boolean append, Object parent)
    85                             boolean writable, Object parent)
    81     {
    86     {
    82         this.fd = fd;
    87         this.fd = fd;
    83         this.readable = readable;
    88         this.readable = readable;
    84         this.writable = writable;
    89         this.writable = writable;
    85         this.append = append;
       
    86         this.parent = parent;
    90         this.parent = parent;
    87         this.path = path;
    91         this.path = path;
    88         this.nd = new FileDispatcherImpl(append);
    92         this.nd = new FileDispatcherImpl();
    89     }
    93     }
    90 
    94 
    91     // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
    95     // Used by FileInputStream.getChannel(), FileOutputStream.getChannel
       
    96     // and RandomAccessFile.getChannel()
    92     public static FileChannel open(FileDescriptor fd, String path,
    97     public static FileChannel open(FileDescriptor fd, String path,
    93                                    boolean readable, boolean writable,
    98                                    boolean readable, boolean writable,
    94                                    Object parent)
    99                                    Object parent)
    95     {
   100     {
    96         return new FileChannelImpl(fd, path, readable, writable, false, parent);
   101         return new FileChannelImpl(fd, path, readable, writable, parent);
    97     }
       
    98 
       
    99     // Used by FileOutputStream.getChannel
       
   100     public static FileChannel open(FileDescriptor fd, String path,
       
   101                                    boolean readable, boolean writable,
       
   102                                    boolean append, Object parent)
       
   103     {
       
   104         return new FileChannelImpl(fd, path, readable, writable, append, parent);
       
   105     }
   102     }
   106 
   103 
   107     private void ensureOpen() throws IOException {
   104     private void ensureOpen() throws IOException {
   108         if (!isOpen())
   105         if (!isOpen())
   109             throw new ClosedChannelException();
   106             throw new ClosedChannelException();
   110     }
   107     }
   111 
       
   112 
   108 
   113     // -- Standard channel operations --
   109     // -- Standard channel operations --
   114 
   110 
   115     protected void implCloseChannel() throws IOException {
   111     protected void implCloseChannel() throws IOException {
   116         // Release and invalidate any locks that we still hold
   112         // Release and invalidate any locks that we still hold
   256             try {
   252             try {
   257                 begin();
   253                 begin();
   258                 ti = threads.add();
   254                 ti = threads.add();
   259                 if (!isOpen())
   255                 if (!isOpen())
   260                     return 0;
   256                     return 0;
       
   257                 boolean append = fdAccess.getAppend(fd);
   261                 do {
   258                 do {
   262                     // in append-mode then position is advanced to end before writing
   259                     // in append-mode then position is advanced to end before writing
   263                     p = (append) ? nd.size(fd) : position0(fd, -1);
   260                     p = (append) ? nd.size(fd) : position0(fd, -1);
   264                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   261                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   265                 return IOStatus.normalize(p);
   262                 return IOStatus.normalize(p);
   282                 begin();
   279                 begin();
   283                 ti = threads.add();
   280                 ti = threads.add();
   284                 if (!isOpen())
   281                 if (!isOpen())
   285                     return null;
   282                     return null;
   286                 do {
   283                 do {
   287                     p  = position0(fd, newPosition);
   284                     p = position0(fd, newPosition);
   288                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   285                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   289                 return this;
   286                 return this;
   290             } finally {
   287             } finally {
   291                 threads.remove(ti);
   288                 threads.remove(ti);
   292                 end(p > -1);
   289                 end(p > -1);