java.io fix and speedup
Mark Wielaard
mark@klomp.org
Thu Jan 2 23:48:00 GMT 2003
Hi,
On Fri, 2003-01-03 at 00:13, Mark Wielaard wrote:
> 2003-01-02 Mark Wielaard <mark@klomp.org>
>
> * java/io/FileInputStream.java (finalize): Don't explicitly
> finalize FileDescriptor.
> * java/io/FileDescriptor.java (position): New private field.
> * java/io/natFileDescriptorPosix.cc (write): Up position.
> (setLength): Use and set position.
> (seek): Set position.
> (getFilePointer): Return position.
> (read): Up position.
>
> OK for branch and mainline?
And now with patch.
Sorry,
Mark
-------------- next part --------------
? java/io/ObjectStreamField.java.merged
Index: java/io/FileDescriptor.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileDescriptor.java,v
retrieving revision 1.10
diff -u -r1.10 FileDescriptor.java
--- java/io/FileDescriptor.java 24 Jul 2002 17:48:41 -0000 1.10
+++ java/io/FileDescriptor.java 2 Jan 2003 23:06:09 -0000
@@ -1,6 +1,6 @@
// FileDescriptor.java - Open file or device
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
This file is part of libgcj.
@@ -101,4 +101,6 @@
// we want to make sure this has the value -1. This is the most
// efficient way to accomplish that.
private int fd = -1;
+
+ private long position = 0;
}
Index: java/io/FileInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/FileInputStream.java,v
retrieving revision 1.8
diff -u -r1.8 FileInputStream.java
--- java/io/FileInputStream.java 20 Nov 2002 16:19:05 -0000 1.8
+++ java/io/FileInputStream.java 2 Jan 2003 23:06:09 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001, 2002, 2003 Free Software Foundation
This file is part of libgcj.
@@ -61,8 +61,8 @@
protected void finalize() throws IOException
{
- if (fd != null)
- fd.finalize();
+ // We don't actually need this, but we include it because it is
+ // mentioned in the JCL.
}
public final FileDescriptor getFD() throws IOException
Index: java/io/natFileDescriptorPosix.cc
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/natFileDescriptorPosix.cc,v
retrieving revision 1.23
diff -u -r1.23 natFileDescriptorPosix.cc
--- java/io/natFileDescriptorPosix.cc 10 Nov 2002 22:23:53 -0000 1.23
+++ java/io/natFileDescriptorPosix.cc 2 Jan 2003 23:06:09 -0000
@@ -1,6 +1,6 @@
// natFileDescriptor.cc - Native part of FileDescriptor class.
-/* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation
+/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
This file is part of libgcj.
@@ -147,6 +147,7 @@
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
}
+ position++;
}
void
@@ -177,6 +178,7 @@
written += r;
len -= r;
bytes += r;
+ position += r;
}
}
@@ -193,7 +195,6 @@
java::io::FileDescriptor::setLength (jlong pos)
{
struct stat sb;
- off_t orig;
#ifdef HAVE_FTRUNCATE
if (::fstat (fd, &sb))
@@ -202,10 +203,6 @@
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.
@@ -215,11 +212,15 @@
throw new IOException (JvNewStringLatin1 (strerror (errno)));
char out = '\0';
int r = ::write (fd, &out, 1);
- if (r <= 0 || ::lseek (fd, orig, SEEK_SET) == -1)
+ if (r <= 0 || ::lseek (fd, position, SEEK_SET) == -1)
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
- else if (::ftruncate (fd, (off_t) pos))
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ else
+ {
+ if (::ftruncate (fd, (off_t) pos))
+ throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ position = pos;
+ }
#else /* HAVE_FTRUNCATE */
throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented"));
#endif /* HAVE_FTRUNCATE */
@@ -230,19 +231,29 @@
{
JvAssert (whence == SET || whence == CUR);
- jlong len = length ();
- jlong here = getFilePointer ();
-
- if (eof_trunc
- && ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
+ if (eof_trunc)
{
- whence = SET;
- pos = len;
+ jlong len = length ();
+ if (whence == SET)
+ {
+ if (pos > len)
+ pos = len;
+ }
+ else
+ {
+ jlong here = getFilePointer ();
+ if (here + pos > len)
+ {
+ pos = len;
+ whence = SET;
+ }
+ }
}
off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
if (r == -1)
throw new IOException (JvNewStringLatin1 (strerror (errno)));
+ position = r;
return r;
}
@@ -258,10 +269,7 @@
jlong
java::io::FileDescriptor::getFilePointer (void)
{
- off_t r = ::lseek (fd, 0, SEEK_CUR);
- if (r == -1)
- throw new IOException (JvNewStringLatin1 (strerror (errno)));
- return r;
+ return position;
}
jint
@@ -282,6 +290,7 @@
}
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
+ position++;
return b & 0xFF;
}
@@ -313,6 +322,7 @@
}
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
+ position += r;
return r;
}
More information about the Java-patches
mailing list