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]

Re: RandomAccessFile.seek


>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

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

Tony> RandomAccessFile.seek should be able to extend files, instead of
Tony> throwing EOFException, to be like JDK 1.3.  This patch moves the
Tony> check on FileDescriptor.seek up into FileInputStream, so that it
Tony> does not occur when RandomAccessFile.seek calls
Tony> FileDescriptor.seek.

Tom> I finally looked at this.  I think we need to do more here.  For
Tom> instance, the other natFileDescriptor* files need to be updated.
Tom> Also RandomAccessFile.skipBytes needs to be updated.  What if we
Tom> change FileDescriptor.seek to take a boolean argument which tells
Tom> it what to do?

I went ahead and implemented this today.  The patch is appended.
I'm checking this in on the trunk.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	* java/io/RandomAccessFile.java (seek): Let seek go past end of
	file.
	(skipBytes): Don't fail if seeking past end of file.
	* java/io/FileInputStream.java (skip): Don't fail if seeking past
	end of file.
	* java/io/natFileDescriptorWin32.cc (seek): Handle `eof_trunc'
	argument.
	* java/io/natFileDescriptorEcos.cc (seek): Handle `eof_trunc'
	argument.
	* java/io/natFileDescriptorPosix.cc (seek): Handle `eof_trunc'
	argument.
	* java/io/FileDescriptor.java (seek): Added `eof_trunc' argument.

Index: java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.6
diff -u -r1.6 FileDescriptor.java
--- java/io/FileDescriptor.java 2000/12/26 00:24:01 1.6
+++ java/io/FileDescriptor.java 2001/08/02 23:45:18
@@ -1,6 +1,6 @@
 // FileDescriptor.java - Open file or device
 
-/* Copyright (C) 1998, 1999, 2000  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -54,7 +54,11 @@
   native void write (byte[] b, int offset, int len)
     throws IOException, NullPointerException, IndexOutOfBoundsException;
   native void close () throws IOException;
-  native int seek (long pos, int whence) 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
+  // will grow).
+  native int seek (long pos, int whence, boolean eof_trunc) throws IOException;
   native long length () throws IOException;
   native long getFilePointer () throws IOException;
   native int read () throws IOException;
Index: java/io/FileInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileInputStream.java,v
retrieving revision 1.5
diff -u -r1.5 FileInputStream.java
--- java/io/FileInputStream.java 2000/12/08 10:28:32 1.5
+++ java/io/FileInputStream.java 2001/08/02 23:45:18
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -88,6 +88,6 @@
 
   public long skip(long n) throws IOException
   {
-    return fd.seek(n, FileDescriptor.CUR);
+    return n <= 0 ? 0 : fd.seek(n, FileDescriptor.CUR, true);
   }
 }
Index: java/io/RandomAccessFile.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/RandomAccessFile.java,v
retrieving revision 1.5
diff -u -r1.5 RandomAccessFile.java
--- java/io/RandomAccessFile.java 2000/12/08 10:28:32 1.5
+++ java/io/RandomAccessFile.java 2001/08/02 23:45:19
@@ -1,6 +1,6 @@
 // RandomAccessFile.java
 
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -161,12 +161,12 @@
 
   public void seek (long pos) throws IOException
   {
-    fd.seek(pos, FileDescriptor.SET);
+    fd.seek(pos, FileDescriptor.SET, false);
   }
 
   public int skipBytes (int count) throws IOException
   {
-    return fd.seek(count, FileDescriptor.CUR);
+    return count <= 0 ? 0 : fd.seek(count, FileDescriptor.CUR, true);
   }
 
   public void write (int oneByte) throws IOException
Index: java/io/natFileDescriptorEcos.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorEcos.cc,v
retrieving revision 1.5
diff -u -r1.5 natFileDescriptorEcos.cc
--- java/io/natFileDescriptorEcos.cc 2001/03/26 07:05:32 1.5
+++ java/io/natFileDescriptorEcos.cc 2001/08/02 23:45:19
@@ -1,6 +1,6 @@
 // natFileDescriptor.cc - Native part of FileDescriptor class.
 
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -88,16 +88,9 @@
 }
 
 jint
-java::io::FileDescriptor::seek (jlong pos, jint whence)
+java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean)
 {
   JvAssert (whence == SET || whence == CUR);
-
-  jlong len = length ();
-  jlong here = getFilePointer ();
-
-  if ((whence == SET && pos > len) || (whence == CUR && here + pos > len))
-    throw new EOFException;
-
   return 0;
 }
 
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.14
diff -u -r1.14 natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc 2001/06/26 03:27:57 1.14
+++ java/io/natFileDescriptorPosix.cc 2001/08/02 23:45:19
@@ -177,15 +177,19 @@
 }
 
 jint
-java::io::FileDescriptor::seek (jlong pos, jint whence)
+java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
 {
   JvAssert (whence == SET || whence == CUR);
 
   jlong len = length ();
   jlong here = getFilePointer ();
 
-  if ((whence == SET && pos > len) || (whence == CUR && here + pos > len))
-    throw new EOFException;
+  if (eof_trunc
+      && ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
+    {
+      whence = SET;
+      pos = len;
+    }
 
   off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
   if (r == -1)
Index: java/io/natFileDescriptorWin32.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorWin32.cc,v
retrieving revision 1.2
diff -u -r1.2 natFileDescriptorWin32.cc
--- java/io/natFileDescriptorWin32.cc 2001/03/26 07:05:32 1.2
+++ java/io/natFileDescriptorWin32.cc 2001/08/02 23:45:19
@@ -1,6 +1,6 @@
 // natFileDescriptorWin32.cc - Native part of FileDescriptor class.
 
-/* Copyright (C) 1998, 1999, 2000  Red Hat, Inc.
+/* Copyright (C) 1998, 1999, 2000, 2001  Red Hat, Inc.
 
    This file is part of libgcj.
 
@@ -171,15 +171,19 @@
 }
 
 jint
-java::io::FileDescriptor::seek (jlong pos, jint whence)
+java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
 {
   JvAssert (whence == SET || whence == CUR);
 
   jlong len = length();
   jlong here = getFilePointer();
 
-  if ((whence == SET && pos > len) || (whence == CUR && here + pos > len))
-    throw new EOFException;
+  if (eof_trunc
+      && ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
+    {
+      whence = SET;
+      pos = len;
+    }
 
   LONG high = pos >> 32;
   DWORD low = SetFilePointer ((HANDLE)fd, (DWORD)(0xffffffff & pos), &high, whence == SET ? FILE_BEGIN : FILE_CURRENT);


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