Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4462298

java.nio.FileChannel.map() "Access is denied" exception

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • x86
    • windows_2000
    • Verified


      ingrid.yao@Eng 2001-05-23

      J2SE Version (please include all output from java -version flag):

        java version "1.4.0-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b64)
        Java HotSpot(TM) Client VM (build 1.4.0-beta-b64, mixed mode)

      Does this problem occur on J2SE 1.3? Yes / No (pick one)

        Feature Not exists

      Operating System Configuration Information (be specific):

          Win 2000 5.00.2195 Service pack 1

      Hardware Configuration Information (be specific):

         x86 Family 6 Model 7 Stepping 3

      Bug Description:

         FileChannel.map() fails if opened on a FileOutputStream. works ok on a RandomAccessFile.

         java.io.IOException: Access is denied
      at sun.nio.ch.FileChannelImpl.map0(Native Method)
      at sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:386)
      at test.jdk14.Nio.writeNIOSmap(Nio.java:434)
      at test.jdk14.Nio.main(Nio.java

      Test program
      =============
      // test.java use FileOutPutStream and fail

      import java.nio.channels.FileChannel;
      import java.nio.MappedByteBuffer;
      import java.io.*;


      public class test {
         private static double ret=0.0;
         private static File testFile;

         public static void main(String[] args) throws Exception {
      testFile = File.createTempFile("testFile", null);
      testFile.deleteOnExit();
      ret = writeNIOSmap(20);
         }
         private static double writeNIOSmap(int n) throws IOException {
           long startTime = System.currentTimeMillis();

           FileOutputStream is = new FileOutputStream(testFile);
           FileChannel channel = is.getChannel();
           // fails on this line
           MappedByteBuffer buff = channel.map(FileChannel.MAP_RW, 0, n*8);

           double sum = 0.0;
           for (int i=0; i<n; i++) {
             buff.putDouble( (double) i);
             sum += (double) i;
           }

           channel.write( buff);
           is.close();

           long tookTime = System.currentTimeMillis() - startTime;
           System.out.println(" write NIO Stream mapped("+ n+") "+
      (tookTime*.001) + " seconds");
           return sum;
         }
      }

      //test1.java use RandomAccessFile and OK

      import java.nio.channels.FileChannel;
      import java.nio.MappedByteBuffer;
      import java.io.*;


      public class test1 {
         private static double ret=0.0;
         private static File testFile;

         public static void main(String[] args) throws Exception {
      testFile = File.createTempFile("testFile", null);
      testFile.deleteOnExit();
      ret = writeNIOSmap(20);
         }

         private static double writeNIOSmap(int n) throws IOException {
           long startTime = System.currentTimeMillis();

           RandomAccessFile raf = new RandomAccessFile(testFile, "rw");
           FileChannel channel = raf.getChannel();
           MappedByteBuffer buff = channel.map(FileChannel.MAP_RW, 0, n*8);

           double sum = 0.0;
           for (int i=0; i<n; i++) {
             buff.putDouble((double) i);
             sum += (double) i;
           }

           channel.write( buff);
           raf.close();

           long tookTime = System.currentTimeMillis() - startTime;
           System.out.println(" write NIO Stream mapped("+ n+") "+
      (tookTime*.001) + " seconds");
           return sum;
         }
      }

            mmcclosksunw Michael Mccloskey (Inactive)
            tyao Ting-Yun Ingrid Yao (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: