JDK-4462298 : java.nio.FileChannel.map() "Access is denied" exception
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-05-23
  • Updated: 2001-11-09
  • Resolved: 2001-05-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.0 beta2Fixed
Related Reports
Relates :  
Description
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;
   }
}


Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: generic merlin-beta2 FIXED IN: merlin-beta2 INTEGRATED IN: merlin-beta2 VERIFIED IN: merlin-beta2 merlin-beta3
14-06-2004

EVALUATION The test code is erroneous because the channel was obtained from a FileOutputStream and hence is only capable of doing output, not input. There is a small bug here, however: FileChannel.map should throw a NonReadableChannelException in this case, rather than a generic IOException. (The current spec doesn't actually say this, about which a separate bug will be filed.) -- mr@eng 2001/5/23
05-09-0189