This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: file truncation
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Cc: Mohan Embar <gnustuff at thisiscool dot com>
- Date: Sat, 17 Jul 2004 15:51:50 +0200
- Subject: Patch: file truncation
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi list,
I just commited the attached patch which is partly a merge from
classpath (changes done by Mark Wielaared) and some changes in our
native code. It fixes some mauve testcases. I only changed the native
code for posix as this is the only I can test.
Mohan: Can you please check the win32 counterpart ?
Michael
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.
- --
Homepage: http://www.worldforge.org/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFA+S79WSOgCCdjSDsRAkMwAKCHf8HUwOdqJEx8b8XlowwtTjoyRgCeMHXZ
FcAhzoS7vxSLPc/+Lqv7S0g=
=Ux2A
-----END PGP SIGNATURE-----
Index: gnu/java/nio/channels/FileChannelImpl.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/channels/FileChannelImpl.java,v
retrieving revision 1.5
diff -u -b -B -r1.5 FileChannelImpl.java
--- gnu/java/nio/channels/FileChannelImpl.java 17 Jul 2004 08:48:31 -0000 1.5
+++ gnu/java/nio/channels/FileChannelImpl.java 17 Jul 2004 13:45:43 -0000
@@ -422,7 +422,9 @@
if ((mode & WRITE) == 0)
throw new NonWritableChannelException ();
+ if (size < size ())
implTruncate (size);
+
return this;
}
}
Index: gnu/java/nio/channels/natFileChannelPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/nio/channels/natFileChannelPosix.cc,v
retrieving revision 1.3
diff -u -b -B -r1.3 natFileChannelPosix.cc
--- gnu/java/nio/channels/natFileChannelPosix.cc 18 Mar 2004 17:23:53 -0000 1.3
+++ gnu/java/nio/channels/natFileChannelPosix.cc 17 Jul 2004 13:45:43 -0000
@@ -274,7 +274,10 @@
}
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;
}
Index: java/io/RandomAccessFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/RandomAccessFile.java,v
retrieving revision 1.19
diff -u -b -B -r1.19 RandomAccessFile.java
--- java/io/RandomAccessFile.java 20 Apr 2004 11:37:41 -0000 1.19
+++ java/io/RandomAccessFile.java 17 Jul 2004 13:45:43 -0000
@@ -194,11 +194,13 @@
}
/**
- * 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.
+ * 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.
*
@@ -208,7 +210,19 @@
*/
public void setLength (long newLen) throws IOException
{
+ // 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);
+ }
}
/**