]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
natFileDescriptorWin32.cc (setLength): New method.
authorTom Tromey <tromey@redhat.com>
Wed, 24 Jul 2002 17:48:41 +0000 (17:48 +0000)
committerTom Tromey <tromey@gcc.gnu.org>
Wed, 24 Jul 2002 17:48:41 +0000 (17:48 +0000)
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

libjava/ChangeLog
libjava/java/io/FileDescriptor.java
libjava/java/io/RandomAccessFile.java
libjava/java/io/natFileDescriptorEcos.cc
libjava/java/io/natFileDescriptorPosix.cc
libjava/java/io/natFileDescriptorWin32.cc

index ff3c72fa08c59f4e97d386336f6822ff36fc1693..75ec4b78a0070208cf1847192aa259f44f14d6a2 100644 (file)
@@ -1,3 +1,12 @@
+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.
+
 2002-07-24  Mark Wielaard  <mark@klomp.org>
 
        * java/lang/reflect/natField.cc (setAddr): Check isAccessible().
index 427a26a6e3a96e2899031b987e7251dfdea5274a..08f8edfaa0d7cd0b14dd7d67b040efddb0351729 100644 (file)
@@ -1,6 +1,6 @@
 // FileDescriptor.java - Open file or device
 
-/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -65,6 +65,7 @@ public final class FileDescriptor
   native void write (byte[] b, int offset, int len)
     throws IOException, NullPointerException, IndexOutOfBoundsException;
   native void close () throws IOException;
+  native void setLength (long pos) throws IOException;
   // EOF_TRUNC is true if a request to seek past the end of file
   // should actually stop at the end of file.  If false, then a seek
   // past the end is ok (and if a subsequent write occurs the file
index 418974c091e2d0f19376d897d48cb06509aa8fe4..81b7050f62dd2acc1bd8df6815719e8d9afba1e6 100644 (file)
@@ -1,6 +1,6 @@
 // RandomAccessFile.java
 
-/* Copyright (C) 1998, 1999, 2001  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001, 2002  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -40,6 +40,11 @@ public class RandomAccessFile implements DataOutput, DataInput
     return fd.getFilePointer();
   }
 
+  public void setLength (long pos) throws IOException
+  {
+    fd.setLength(pos);
+  }
+
   public long length () throws IOException
   {
     return fd.length();
index 8e1dd950b8f3927a5377fe604c30bcc0028096ba..96744f42e44eda5676030aeb8e9a20f13e3e9dd8 100644 (file)
@@ -95,6 +95,11 @@ java::io::FileDescriptor::close (void)
 {
 }
 
+void
+java::io::FileDescriptor::setLength (long)
+{
+}
+
 jint
 java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean)
 {
index bfe00093f4051a53fbd375b380cbad07862896b0..fb11d6262d1fca6a02ed4aef430df554c9c84826 100644 (file)
@@ -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)
 {
index f72e39ce127040428d4f782216090af8306faa87..e004057ef9140e3339f01c5a7c589139a504965e 100644 (file)
@@ -1,6 +1,6 @@
 // natFileDescriptorWin32.cc - Native part of FileDescriptor class.
 
-/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002  Free Software Foundation, Inc.
 
    This file is part of libgcj.
 
@@ -186,6 +186,58 @@ java::io::FileDescriptor::close (void)
     throw new IOException (JvNewStringLatin1 (winerr ()));
 }
 
+void
+java::io::FileDescriptor::setLength(jlong pos)
+{
+  LONG liOrigFilePointer;
+  LONG liNewFilePointer;
+  LONG liEndFilePointer;
+
+  // Get the original file pointer.
+  if (SetFilePointer((HANDLE) fd, (LONG) 0, &liOrigFilePointer,
+                    FILE_CURRENT) != (BOOL) 0
+      && (GetLastError() != NO_ERROR))
+    throw new IOException (JvNewStringLatin1 (winerr ()));
+
+  // Get the length of the file.
+  if (SetFilePointer((HANDLE) fd, (LONG) 0, &liEndFilePointer,
+                    FILE_END) != (BOOL) 0
+      && (GetLastError() != NO_ERROR))
+    throw new IOException (JvNewStringLatin1 (winerr ()));
+
+  if ((jlong)liEndFilePointer == pos)
+    {
+      // Restore the file pointer.
+      if (liOrigFilePointer != liEndFilePointer)
+       {
+         if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
+                            FILE_BEGIN) != (BOOL) 0
+             && (GetLastError() != NO_ERROR))
+           throw new IOException (JvNewStringLatin1 (winerr ()));
+       }
+      return;
+    }
+
+  // Seek to the new end of file.
+  if (SetFilePointer((HANDLE) fd, (LONG) pos, &liNewFilePointer,
+                    FILE_BEGIN) != (BOOL) 0
+      && (GetLastError() != NO_ERROR))
+    throw new IOException (JvNewStringLatin1 (winerr ()));
+
+  // Truncate the file at this point.
+  if (SetEndOfFile((HANDLE) fd) != (BOOL) 0 && (GetLastError() != NO_ERROR))
+    throw new IOException (JvNewStringLatin1 (winerr ()));
+
+  if (liOrigFilePointer < liNewFilePointer)
+    {
+      // Restore the file pointer.
+      if (SetFilePointer((HANDLE) fd, liOrigFilePointer, &liNewFilePointer,
+                        FILE_BEGIN) != (BOOL) 0
+         && (GetLastError() != NO_ERROR))
+       throw new IOException (JvNewStringLatin1 (winerr ()));
+    }
+}
+
 jint
 java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
 {