aboutsummaryrefslogtreecommitdiff
path: root/libjava
diff options
context:
space:
mode:
authorMichael Koch <mkoch@gcc.gnu.org>2004-07-17 13:46:02 +0000
committerMichael Koch <mkoch@gcc.gnu.org>2004-07-17 13:46:02 +0000
commitbaa61e09ef6f62433f337ccac6c99845e6fe2322 (patch)
treee3b46bce712fc96cd55d5375844c9ae031cde8eb /libjava
parent3e25b3a8f80447b0ce191d1742b292b7b1fd2e5c (diff)
downloadgcc-baa61e09ef6f62433f337ccac6c99845e6fe2322.zip
gcc-baa61e09ef6f62433f337ccac6c99845e6fe2322.tar.gz
gcc-baa61e09ef6f62433f337ccac6c99845e6fe2322.tar.bz2
[multiple changes]
2004-07-17 Mark Wielaard <mark@klomp.org> * gnu/java/nio/channels/FileChannelImpl.java (truncate): Only truncate when size is smaller. * java/io/RandomAccessFile.java (setLength): Use truncate for shrinking the file and seek plus write for expanding the file. 2004-07-17 Michael Koch <konqueror@gmx.de> * gnu/java/nio/channels/natFileChannelPosix.cc (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. From-SVN: r84868
Diffstat (limited to 'libjava')
-rw-r--r--libjava/ChangeLog14
-rw-r--r--libjava/gnu/java/nio/channels/FileChannelImpl.java4
-rw-r--r--libjava/gnu/java/nio/channels/natFileChannelPosix.cc5
-rw-r--r--libjava/java/io/RandomAccessFile.java28
4 files changed, 42 insertions, 9 deletions
diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index 375f109..b3e347f 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,5 +1,19 @@
2004-07-17 Mark Wielaard <mark@klomp.org>
+ * gnu/java/nio/channels/FileChannelImpl.java (truncate): Only truncate
+ when size is smaller.
+ * java/io/RandomAccessFile.java (setLength): Use truncate for
+ shrinking the file and seek plus write for expanding the file.
+
+2004-07-17 Michael Koch <konqueror@gmx.de>
+
+ * gnu/java/nio/channels/natFileChannelPosix.cc
+ (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.
+
+2004-07-17 Mark Wielaard <mark@klomp.org>
+
* javax/swing/Box.java: Put FIXME comment above class declaration.
* javax/swing/JButton.java: Remove illegal L&F HTML from comments.
* javax/swing/JCheckBox.java: Likewise.
diff --git a/libjava/gnu/java/nio/channels/FileChannelImpl.java b/libjava/gnu/java/nio/channels/FileChannelImpl.java
index 8b3d0fc..678e10f 100644
--- a/libjava/gnu/java/nio/channels/FileChannelImpl.java
+++ b/libjava/gnu/java/nio/channels/FileChannelImpl.java
@@ -422,7 +422,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/libjava/gnu/java/nio/channels/natFileChannelPosix.cc b/libjava/gnu/java/nio/channels/natFileChannelPosix.cc
index b8f9937..a2c1c79 100644
--- a/libjava/gnu/java/nio/channels/natFileChannelPosix.cc
+++ b/libjava/gnu/java/nio/channels/natFileChannelPosix.cc
@@ -274,7 +274,10 @@ FileChannelImpl::implTruncate (jlong size)
}
else
{
- if (::ftruncate (fd, (off_t) pos))
+ if (::ftruncate (fd, (off_t) size))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ if (pos > size
+ && ::lseek (fd, (off_t) size, SEEK_SET) == -1)
throw new IOException (JvNewStringLatin1 (strerror (errno)));
pos = size;
}
diff --git a/libjava/java/io/RandomAccessFile.java b/libjava/java/io/RandomAccessFile.java
index c20549c..7907d46 100644
--- a/libjava/java/io/RandomAccessFile.java
+++ b/libjava/java/io/RandomAccessFile.java
@@ -194,12 +194,14 @@ public class RandomAccessFile implements DataOutput, DataInput
}
/**
- * This method sets the length of the file to the specified length. If
- * the currently length of the file is longer than the specified length,
- * then the file is truncated to the specified length. If the current
- * length of the file is shorter than the specified length, the file
- * is extended with bytes of an undefined value.
- * <p>
+ * This method sets the length of the file to the specified length.
+ * If the currently length of the file is longer than the specified
+ * length, then the file is truncated to the specified length (the
+ * file position is set to the end of file in this case). If the
+ * current length of the file is shorter than the specified length,
+ * the file is extended with bytes of an undefined value (the file
+ * position is unchanged in this case).
+ * <p>
* The file must be open for write access for this operation to succeed.
*
* @param newlen The new length of the file
@@ -208,7 +210,19 @@ public class RandomAccessFile implements DataOutput, DataInput
*/
public void setLength (long newLen) throws IOException
{
- ch.truncate (newLen);
+ // FIXME: Extending a file should probably be done by one method call.
+
+ // 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 if (newLen > length())
+ {
+ long pos = getFilePointer();
+ seek(newLen - 1);
+ write(0);
+ seek(pos);
+ }
}
/**