java.io.RandomAccessFile(String name, String mode) doesn't allow to open a file in a read-only mode ---------------------------------------------------------------------------------------------------
Key: HARMONY-69 URL: http://issues.apache.org/jira/browse/HARMONY-69 Project: Harmony Type: Bug Components: Classlib Reporter: Vladimir Ivanov java.io.RandomAccessFile(String name, String mode) doesn't allow to open a file in a read-only mode. Looks like the mode is always "rw". Note, java.io.RandomAccessFile(File file, String mode) works OK. Code to reproduce: import java.io.*; import java.nio.channels.*; public class test29 { private static void testMode(RandomAccessFile raf, int index) { try { FileChannel fcr = raf.getChannel(); // fcr.lock() should throw NonWritableChannelException for "r" mode fcr.lock(0L, Long.MAX_VALUE, false); fcr.close(); System.out.println("Error " + index); } catch (NonWritableChannelException e) { System.out.println("Success" + index + ": " + e); } catch (Throwable e) { System.out.println("Exception" + index + ": " + e); e.printStackTrace(); } } public static void testRandomAccessFile_File_String(File f, int index) { try { System.out.println("Create RAF(File, Sting): " + f); RandomAccessFile raf = new RandomAccessFile(f, "r"); testMode(raf, index); raf.close(); } catch (Throwable e) { System.out.println("Exception" + index + ": " + e); e.printStackTrace(); } } public static void testRandomAccessFile_String_String(String f, int index) { try { System.out.println("Create RAF(File, Sting): " + f); RandomAccessFile raf = new RandomAccessFile(f, "r"); testMode(raf, index); raf.close(); } catch (Throwable e) { System.out.println("Exception" + index + ": " + e); e.printStackTrace(); } } public static void main(String[] args) { try { File f = File.createTempFile("temp", ".txt"); f.deleteOnExit(); testRandomAccessFile_File_String(f, 1); testRandomAccessFile_String_String(f.getAbsolutePath(), 2); testRandomAccessFile_File_String(f, 3); } catch (Throwable e) { System.out.println("Unexpected exception: " + e); e.printStackTrace(); } } } Steps to Reproduce: 1. Build Harmony (check-out on 2006-01-30) j2se subset as described in README.txt. 2. Compile test29.java using BEA 1.4 javac > javac -d . test29.java 3. Run java using compatible VM (J9) > java -showversion test29 Output: C:\tmp\tmp17>C:\jrockit-j2sdk1.4.2_04\bin\java.exe -showversion test29 java version "1.4.2_04" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05) BEA WebLogic JRockit(TM) 1.4.2_04 JVM (build ari-31788-20040616-1132-win-ia32, Native Threads, GC strategy: parallel) Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp23838.txt Success1: java.nio.channels.NonWritableChannelException Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp23838.txt Success2: java.nio.channels.NonWritableChannelException Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp23838.txt Success3: java.nio.channels.NonWritableChannelException C:\tmp\tmp17>C:\harmony\trunk\deploy\jre\bin\java -showversion test29 java version 1.4.2 (subset) (c) Copyright 1991, 2005 The Apache Software Foundation or its licensors, as applicable. Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp24535.txt Success1: java.nio.channels.NonWritableChannelException Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp24535.txt Error 2 Create RAF(File, Sting): C:\DOCUME~1\vivanov1\LOCALS~1\Temp\temp24535.txt Success3: java.nio.channels.NonWritableChannelException Suggested fix: Add the string to set up current mode like the string in the RandomAccessFile(File, String) method in the class RandomAccessFile.java sting #122: 122: isReadOnly = mode.equals("r"); junit test: ------------------------ RandomAccessFileTest.java ------------------------------------------------- import java.io.*; import java.nio.channels.*; import junit.framework.*; public class RandomAccessFileTest extends TestCase { public void testRandomAccessFile_String_String() { RandomAccessFile raf = null; FileChannel fcr = null; try { File f = File.createTempFile("temp", ".txt"); f.deleteOnExit(); raf = new RandomAccessFile(f.getAbsolutePath(), "r"); fcr = raf.getChannel(); fcr.lock(0L, Long.MAX_VALUE, false); fail("NonWritableChannelException expected!"); } catch (NonWritableChannelException e) { } catch (Throwable e) { fail("Unexpected exception: " + e); } finally { try { if (fcr != null) fcr.close(); if (raf != null) raf.close(); } catch (Exception e) { } } } public static void main(String[] args) { junit.textui.TestRunner.run(RandomAccessFileTest.class); } } -- This message is automatically generated by JIRA. - If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa - For more information on JIRA, see: http://www.atlassian.com/software/jira