diff options
author | Mark Wielaard <mark@klomp.org> | 2004-05-01 10:40:54 +0000 |
---|---|---|
committer | Mark Wielaard <mark@klomp.org> | 2004-05-01 10:40:54 +0000 |
commit | 3744808d57980d1fd9e1cda963dace3aeb9d4e52 (patch) | |
tree | 959df368b927cc347cc105654b826b03abf39afd | |
parent | 6189c0c0fefda9766401916198740bca276e9b34 (diff) | |
download | classpath-3744808d57980d1fd9e1cda963dace3aeb9d4e52.tar.gz |
* gnu/java/nio/channels/FileChannelImpl.java (truncate): Only truncate
when size is smaller.
* native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c
(implTruncate): Always save current position. Only reposition file
pointer to where we started if not beyond new lenght. Reposition file
pointer to file length if it points beyond the end of file.
* java/io/RandomAccessFile.java (setLength): Use truncate for
shrinking the file and seek plus write for expanding the file.
-rw-r--r-- | ChangeLog | 11 | ||||
-rw-r--r-- | gnu/java/nio/channels/FileChannelImpl.java | 4 | ||||
-rw-r--r-- | java/io/RandomAccessFile.java | 16 | ||||
-rw-r--r-- | native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c | 50 |
4 files changed, 58 insertions, 23 deletions
@@ -1,3 +1,14 @@ +2004-04-30 Mark Wielaard <mark@klomp.org> + + * gnu/java/nio/channels/FileChannelImpl.java (truncate): Only truncate + when size is smaller. + * native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c + (implTruncate): Always save current position. Only reposition file + pointer to where we started if not beyond new lenght. Reposition file + pointer to file length if it points beyond the end of file. + * java/io/RandomAccessFile.java (setLength): Use truncate for + shrinking the file and seek plus write for expanding the file. + 2004-04-30 Tom Tromey <tromey@redhat.com> Mark Wielaard <mark@klomp.org> diff --git a/gnu/java/nio/channels/FileChannelImpl.java b/gnu/java/nio/channels/FileChannelImpl.java index 2aeadfc3f..2a27e1dd1 100644 --- a/gnu/java/nio/channels/FileChannelImpl.java +++ b/gnu/java/nio/channels/FileChannelImpl.java @@ -413,7 +413,9 @@ public final class FileChannelImpl extends FileChannel if ((mode & WRITE) == 0) throw new NonWritableChannelException (); - implTruncate (size); + if (size < size ()) + implTruncate (size); + return this; } } diff --git a/java/io/RandomAccessFile.java b/java/io/RandomAccessFile.java index b6133d0d6..f48d11d7c 100644 --- a/java/io/RandomAccessFile.java +++ b/java/io/RandomAccessFile.java @@ -210,11 +210,17 @@ public class RandomAccessFile implements DataOutput, DataInput */ public void setLength (long newLen) throws IOException { - ch.truncate (newLen); - - long position = getFilePointer(); - if (position > newLen) - seek(newLen); + // FileChannel.truncate() can only shrink a file. + // To expand it we need to seek forward and write at least one byte. + if (newLen < length()) + ch.truncate (newLen); + else + { + long pos = getFilePointer(); + seek(newLen - 1); + write(0); + seek(pos); + } } /** diff --git a/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c b/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c index 2ff6891d0..c3c7bdec4 100644 --- a/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c +++ b/native/jni/java-nio/gnu_java_nio_channels_FileChannelImpl.c @@ -394,20 +394,20 @@ Java_gnu_java_nio_channels_FileChannelImpl_implTruncate (JNIEnv *env, jobject ob return; } + /* Save off current position */ + TARGET_NATIVE_FILE_TELL(native_fd, save_offset, result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, + TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } + if (TARGET_NATIVE_MATH_INT_INT64_LT(file_size,len)) { /* File is too short -- seek to one byte short of where we want, * then write a byte */ - /* Save off current position */ - TARGET_NATIVE_FILE_TELL(native_fd, save_offset, result); - if (result != TARGET_NATIVE_OK) - { - JCL_ThrowException(env, IO_EXCEPTION, - TARGET_NATIVE_LAST_ERROR_STRING()); - return; - } - /* move to position n-1 */ TARGET_NATIVE_FILE_SEEK_BEGIN(native_fd, TARGET_NATIVE_MATH_INT_INT64_SUB(len,1), new_offset, result); if (result != TARGET_NATIVE_OK) @@ -429,14 +429,18 @@ Java_gnu_java_nio_channels_FileChannelImpl_implTruncate (JNIEnv *env, jobject ob return; } - /* Reposition file pointer to where we started */ - TARGET_NATIVE_FILE_SEEK_BEGIN(native_fd, save_offset, new_offset, result); - if (result != TARGET_NATIVE_OK) - { - JCL_ThrowException(env, IO_EXCEPTION, - TARGET_NATIVE_LAST_ERROR_STRING()); - return; - } + /* Reposition file pointer to where we started if not beyond new len. */ + if (TARGET_NATIVE_MATH_INT_INT64_LT(save_offset, len)) + { + TARGET_NATIVE_FILE_SEEK_BEGIN(native_fd, save_offset, + new_offset, result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, + TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } + } } else if (TARGET_NATIVE_MATH_INT_INT64_GT(file_size,len)) { @@ -458,6 +462,18 @@ Java_gnu_java_nio_channels_FileChannelImpl_implTruncate (JNIEnv *env, jobject ob JCL_ThrowException(env, IO_EXCEPTION, "Unable to shorten file length"); #endif /* HAVE_FTRUNCATE */ + + /* Reposition file pointer when it now is beyond the end of file. */ + if (TARGET_NATIVE_MATH_INT_INT64_GT(save_offset, len)) + { + TARGET_NATIVE_FILE_SEEK_BEGIN(native_fd, len, new_offset, result); + if (result != TARGET_NATIVE_OK) + { + JCL_ThrowException(env, IO_EXCEPTION, + TARGET_NATIVE_LAST_ERROR_STRING()); + return; + } + } } } |