This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: unidiff version of the last patch I submitted


>>>>> "Tony" == Tony Kimball <alk@pobox.com> writes:

Tony> The only differences from the previous patch are in
Tony> PlainSocketImpl.java and natPlainSocketImpl.cc.

I think parts of this are still unnecessary.  There's no reason to add
a new unused method to FileDescriptor.

I'm checking this in.  This is half of your patch.  I made some
changes to it:

* Coding style cleanups and ChangeLog entry
  (Please do these next time.)

* Fixed setLength on POSIX to actually work.  According to the glibc
  manual, lseek() won't actually change the file's length.

I don't think my changes to the Windows part of the patch will hurt,
as I believe they are purely cosmetic.  Hopefully that's really true :-)
If not I'll fix whatever problems arise; just email them to me.

I tested this using a simple test program on x86 Red Hat Linux 7.3.

I'll look at the java.net patch next.

Tom

Index: ChangeLog
from  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.

Index: java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.9
diff -u -r1.9 FileDescriptor.java
--- java/io/FileDescriptor.java 6 Mar 2002 22:37:26 -0000 1.9
+++ java/io/FileDescriptor.java 24 Jul 2002 17:41:21 -0000
@@ -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 @@
   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: java/io/RandomAccessFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/RandomAccessFile.java,v
retrieving revision 1.6
diff -u -r1.6 RandomAccessFile.java
--- java/io/RandomAccessFile.java 2 Aug 2001 23:46:39 -0000 1.6
+++ java/io/RandomAccessFile.java 24 Jul 2002 17:41:21 -0000
@@ -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.
 
@@ -38,6 +38,11 @@
   public long getFilePointer () throws IOException
   {
     return fd.getFilePointer();
+  }
+
+  public void setLength (long pos) throws IOException
+  {
+    fd.setLength(pos);
   }
 
   public long length () throws IOException
Index: java/io/natFileDescriptorEcos.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorEcos.cc,v
retrieving revision 1.8
diff -u -r1.8 natFileDescriptorEcos.cc
--- java/io/natFileDescriptorEcos.cc 6 Mar 2002 23:23:34 -0000 1.8
+++ java/io/natFileDescriptorEcos.cc 24 Jul 2002 17:41:21 -0000
@@ -95,6 +95,11 @@
 {
 }
 
+void
+java::io::FileDescriptor::setLength (long)
+{
+}
+
 jint
 java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean)
 {
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.19
diff -u -r1.19 natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc 10 Mar 2002 17:59:23 -0000 1.19
+++ java/io/natFileDescriptorPosix.cc 24 Jul 2002 17:41:22 -0000
@@ -17,6 +17,8 @@
 #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. */
@@ -186,6 +188,38 @@
   jint save = fd;
   fd = -1;
   if (::close (save))
+    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)));
 }
 
Index: java/io/natFileDescriptorWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorWin32.cc,v
retrieving revision 1.9
diff -u -r1.9 natFileDescriptorWin32.cc
--- java/io/natFileDescriptorWin32.cc 6 Jun 2002 20:39:37 -0000 1.9
+++ java/io/natFileDescriptorWin32.cc 24 Jul 2002 17:41:22 -0000
@@ -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.
 
@@ -184,6 +184,58 @@
   fd = (jint)INVALID_HANDLE_VALUE;
   if (! CloseHandle (save))
     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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]