src/windows/classes/sun/nio/fs/WindowsChannelFactory.java

Print this page
rev 9260 : imported patch io-events-path


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.nio.channels.*;

















  30 import java.io.FileDescriptor;
  31 import java.io.IOException;
  32 import java.util.*;
  33 
  34 import com.sun.nio.file.ExtendedOpenOption;



  35 


  36 import sun.nio.ch.FileChannelImpl;
  37 import sun.nio.ch.ThreadPool;
  38 import sun.nio.ch.WindowsAsynchronousFileChannelImpl;
  39 import sun.misc.SharedSecrets;
  40 import sun.misc.JavaIOFileDescriptorAccess;
  41 
  42 import static sun.nio.fs.WindowsNativeDispatcher.*;
  43 import static sun.nio.fs.WindowsConstants.*;
  44 
  45 /**
  46  * Factory to create FileChannels and AsynchronousFileChannels.
  47  */
  48 
  49 class WindowsChannelFactory {
  50     private static final JavaIOFileDescriptorAccess fdAccess =
  51         SharedSecrets.getJavaIOFileDescriptorAccess();
  52 
  53     private WindowsChannelFactory() { }
  54 
  55     /**
  56      * Do not follow reparse points when opening an existing file. Do not fail
  57      * if the file is a reparse point.
  58      */
  59     static final OpenOption OPEN_REPARSE_POINT = new OpenOption() { };
  60 
  61     /**
  62      * Represents the flags from a user-supplied set of open options.
  63      */


 140         throws WindowsException
 141     {
 142         Flags flags = Flags.toFlags(options);
 143 
 144         // default is reading; append => writing
 145         if (!flags.read && !flags.write) {
 146             if (flags.append) {
 147                 flags.write = true;
 148             } else {
 149                 flags.read = true;
 150             }
 151         }
 152 
 153         // validation
 154         if (flags.read && flags.append)
 155             throw new IllegalArgumentException("READ + APPEND not allowed");
 156         if (flags.append && flags.truncateExisting)
 157             throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
 158 
 159         FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
 160         return FileChannelImpl.open(fdObj, flags.read, flags.write, flags.append, null);
 161     }
 162 
 163     /**
 164      * Open/creates file, returning AsynchronousFileChannel to access the file
 165      *
 166      * @param   pathForWindows
 167      *          The path of the file to open/create
 168      * @param   pathToCheck
 169      *          The path used for permission checks (if security manager)
 170      * @param   pool
 171      *          The thread pool that the channel is associated with
 172      */
 173     static AsynchronousFileChannel newAsynchronousFileChannel(String pathForWindows,
 174                                                               String pathToCheck,
 175                                                               Set<? extends OpenOption> options,
 176                                                               long pSecurityDescriptor,
 177                                                               ThreadPool pool)
 178         throws IOException
 179     {
 180         Flags flags = Flags.toFlags(options);




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import static sun.nio.fs.WindowsConstants.CREATE_NEW;
  29 import static sun.nio.fs.WindowsConstants.FILE_ATTRIBUTE_NORMAL;
  30 import static sun.nio.fs.WindowsConstants.FILE_FLAG_DELETE_ON_CLOSE;
  31 import static sun.nio.fs.WindowsConstants.FILE_FLAG_OPEN_REPARSE_POINT;
  32 import static sun.nio.fs.WindowsConstants.FILE_FLAG_OVERLAPPED;
  33 import static sun.nio.fs.WindowsConstants.FILE_FLAG_WRITE_THROUGH;
  34 import static sun.nio.fs.WindowsConstants.FILE_SHARE_DELETE;
  35 import static sun.nio.fs.WindowsConstants.FILE_SHARE_READ;
  36 import static sun.nio.fs.WindowsConstants.FILE_SHARE_WRITE;
  37 import static sun.nio.fs.WindowsConstants.GENERIC_READ;
  38 import static sun.nio.fs.WindowsConstants.GENERIC_WRITE;
  39 import static sun.nio.fs.WindowsConstants.OPEN_ALWAYS;
  40 import static sun.nio.fs.WindowsConstants.OPEN_EXISTING;
  41 import static sun.nio.fs.WindowsConstants.TRUNCATE_EXISTING;
  42 import static sun.nio.fs.WindowsNativeDispatcher.CloseHandle;
  43 import static sun.nio.fs.WindowsNativeDispatcher.CreateFile;
  44 import static sun.nio.fs.WindowsNativeDispatcher.DeviceIoControlSetSparse;
  45 import static sun.nio.fs.WindowsNativeDispatcher.SetEndOfFile;
  46 
  47 import java.io.FileDescriptor;
  48 import java.io.IOException;
  49 import java.nio.channels.AsynchronousFileChannel;
  50 import java.nio.channels.FileChannel;
  51 import java.nio.file.LinkOption;
  52 import java.nio.file.OpenOption;
  53 import java.nio.file.StandardOpenOption;
  54 import java.util.Set;
  55 
  56 import sun.misc.JavaIOFileDescriptorAccess;
  57 import sun.misc.SharedSecrets;
  58 import sun.nio.ch.FileChannelImpl;
  59 import sun.nio.ch.ThreadPool;
  60 import sun.nio.ch.WindowsAsynchronousFileChannelImpl;


  61 
  62 import com.sun.nio.file.ExtendedOpenOption;

  63 
  64 /**
  65  * Factory to create FileChannels and AsynchronousFileChannels.
  66  */
  67 
  68 class WindowsChannelFactory {
  69     private static final JavaIOFileDescriptorAccess fdAccess =
  70         SharedSecrets.getJavaIOFileDescriptorAccess();
  71 
  72     private WindowsChannelFactory() { }
  73 
  74     /**
  75      * Do not follow reparse points when opening an existing file. Do not fail
  76      * if the file is a reparse point.
  77      */
  78     static final OpenOption OPEN_REPARSE_POINT = new OpenOption() { };
  79 
  80     /**
  81      * Represents the flags from a user-supplied set of open options.
  82      */


 159         throws WindowsException
 160     {
 161         Flags flags = Flags.toFlags(options);
 162 
 163         // default is reading; append => writing
 164         if (!flags.read && !flags.write) {
 165             if (flags.append) {
 166                 flags.write = true;
 167             } else {
 168                 flags.read = true;
 169             }
 170         }
 171 
 172         // validation
 173         if (flags.read && flags.append)
 174             throw new IllegalArgumentException("READ + APPEND not allowed");
 175         if (flags.append && flags.truncateExisting)
 176             throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
 177 
 178         FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
 179         return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, flags.append, null);
 180     }
 181 
 182     /**
 183      * Open/creates file, returning AsynchronousFileChannel to access the file
 184      *
 185      * @param   pathForWindows
 186      *          The path of the file to open/create
 187      * @param   pathToCheck
 188      *          The path used for permission checks (if security manager)
 189      * @param   pool
 190      *          The thread pool that the channel is associated with
 191      */
 192     static AsynchronousFileChannel newAsynchronousFileChannel(String pathForWindows,
 193                                                               String pathToCheck,
 194                                                               Set<? extends OpenOption> options,
 195                                                               long pSecurityDescriptor,
 196                                                               ThreadPool pool)
 197         throws IOException
 198     {
 199         Flags flags = Flags.toFlags(options);