From 18f6f7881ac92a45262aa63e1d2c75ae08adcc79 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Thu, 17 Oct 2013 14:40:21 -0500 Subject: [PATCH] RandomAccessFile: support opening for read/write So far, we only allowed opening in read-only mode. Now, we also support read/write mode in addition. Signed-off-by: Johannes Schindelin --- classpath/java-io.cpp | 7 ++++--- classpath/java/io/RandomAccessFile.java | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/classpath/java-io.cpp b/classpath/java-io.cpp index c8a0c8419f..462bed8ada 100644 --- a/classpath/java-io.cpp +++ b/classpath/java-io.cpp @@ -803,17 +803,18 @@ Java_java_io_FileOutputStream_close(JNIEnv* e, jclass, jint fd) extern "C" JNIEXPORT void JNICALL Java_java_io_RandomAccessFile_open(JNIEnv* e, jclass, jstring path, - jlongArray result) + jboolean allowWrite, jlongArray result) { string_t chars = getChars(e, path); if (chars) { jlong peer = 0; jlong length = 0; + int flags = (allowWrite ? O_RDWR | O_CREAT : O_RDONLY) | OPEN_MASK; #if !defined(WINAPI_FAMILY) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) #if defined(PLATFORM_WINDOWS) - int fd = ::_wopen(chars, O_RDONLY | OPEN_MASK); + int fd = ::_wopen(chars, flags); #else - int fd = ::open((const char*)chars, O_RDONLY | OPEN_MASK); + int fd = ::open((const char*)chars, flags, 0644); #endif releaseChars(e, path, chars); if (fd == -1) { diff --git a/classpath/java/io/RandomAccessFile.java b/classpath/java/io/RandomAccessFile.java index aeedbd460c..2ab7d6e96f 100644 --- a/classpath/java/io/RandomAccessFile.java +++ b/classpath/java/io/RandomAccessFile.java @@ -17,23 +17,25 @@ public class RandomAccessFile { private File file; private long position = 0; private long length; + private boolean allowWrite; public RandomAccessFile(String name, String mode) throws FileNotFoundException { - if (! mode.equals("r")) throw new IllegalArgumentException(); file = new File(name); + if (mode.equals("rw")) allowWrite = true; + else if (! mode.equals("r")) throw new IllegalArgumentException(); open(); } private void open() throws FileNotFoundException { long[] result = new long[2]; - open(file.getPath(), result); + open(file.getPath(), allowWrite, result); peer = result[0]; length = result[1]; } - private static native void open(String name, long[] result) + private static native void open(String name, boolean allowWrite, long[] result) throws FileNotFoundException; private void refresh() throws IOException { @@ -53,7 +55,7 @@ public class RandomAccessFile { } public void seek(long position) throws IOException { - if (position < 0 || position > length()) throw new IOException(); + if (position < 0 || (!allowWrite && position > length())) throw new IOException(); this.position = position; }