diff options
author | Tom Tromey <tromey@redhat.com> | 2002-07-24 17:48:41 +0000 |
---|---|---|
committer | Tom Tromey <tromey@gcc.gnu.org> | 2002-07-24 17:48:41 +0000 |
commit | ce05139c56abbf21a0c8f81150542ed213d07581 (patch) | |
tree | e662d090c42dfe798a34225b7ec886fdde1d8f2e /libjava/java/io/natFileDescriptorPosix.cc | |
parent | 8a3ad73714d047e5c183761197ef21e565c2210e (diff) | |
download | gcc-ce05139c56abbf21a0c8f81150542ed213d07581.zip gcc-ce05139c56abbf21a0c8f81150542ed213d07581.tar.gz gcc-ce05139c56abbf21a0c8f81150542ed213d07581.tar.bz2 |
natFileDescriptorWin32.cc (setLength): New method.
2002-07-24 Tom Tromey <tromey@redhat.com>
Tony Kimball <alk@pobox.com>
* java/io/natFileDescriptorWin32.cc (setLength): New method.
* java/io/natFileDescriptorPosix.cc (setLength): New method.
* java/io/RandomAccessFile.java (setLength): New method.
* java/io/natFileDescriptorEcos.cc (setLength): New method.
* java/io/FileDescriptor.java (setLength): New method.
Co-Authored-By: Tony Kimball <alk@pobox.com>
From-SVN: r55715
Diffstat (limited to 'libjava/java/io/natFileDescriptorPosix.cc')
-rw-r--r-- | libjava/java/io/natFileDescriptorPosix.cc | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libjava/java/io/natFileDescriptorPosix.cc b/libjava/java/io/natFileDescriptorPosix.cc index bfe0009..fb11d62 100644 --- a/libjava/java/io/natFileDescriptorPosix.cc +++ b/libjava/java/io/natFileDescriptorPosix.cc @@ -17,6 +17,8 @@ details. */ #include <string.h> #include <sys/stat.h> #include <sys/param.h> +#include <sys/socket.h> +#include <fcntl.h> #ifdef HAVE_SYS_IOCTL_H #define BSD_COMP /* Get FIONREAD on Solaris2. */ @@ -189,6 +191,38 @@ java::io::FileDescriptor::close (void) throw new IOException (JvNewStringLatin1 (strerror (errno))); } +void +java::io::FileDescriptor::setLength (jlong pos) +{ + struct stat sb; + off_t orig; + + if (::fstat (fd, &sb)) + throw new IOException (JvNewStringLatin1 (strerror (errno))); + + if ((jlong) sb.st_size == pos) + return; + + orig = ::lseek (fd, (off_t) 0, SEEK_CUR); + if (orig == -1) + throw new IOException (JvNewStringLatin1 (strerror (errno))); + + // If the file is too short, we extend it. We can't rely on + // ftruncate() extending the file. So we lseek() to 1 byte less + // than we want, and then we write a single byte at the end. + if ((jlong) sb.st_size < pos) + { + if (::lseek (fd, (off_t) (pos - 1), SEEK_SET) == -1) + throw new IOException (JvNewStringLatin1 (strerror (errno))); + char out = '\0'; + int r = ::write (fd, &out, 1); + if (r <= 0 || ::lseek (fd, orig, SEEK_SET) == -1) + throw new IOException (JvNewStringLatin1 (strerror (errno))); + } + else if (::ftruncate (fd, (off_t) pos)) + throw new IOException (JvNewStringLatin1 (strerror (errno))); +} + jint java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc) { |