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 <johannes.schindelin@gmx.de>
This commit is contained in:
Johannes Schindelin 2013-10-17 14:40:21 -05:00
parent b3d4f33522
commit 18f6f7881a
2 changed files with 10 additions and 7 deletions

View File

@ -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) {

View File

@ -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;
}