Commit 1d5c46dc by Mark Wielaard Committed by Mark Wielaard

FileDescriptor.java (position): New private field.

        * 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.

From-SVN: r60869
parent 3aa0cc4e
2003-01-03 Mark Wielaard <mark@klomp.org> 2003-01-03 Mark Wielaard <mark@klomp.org>
* 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.
2003-01-03 Mark Wielaard <mark@klomp.org>
Merge with Classpath: Merge with Classpath:
* java/io/ObjectStreamClass.java (lookup): Split method and call * java/io/ObjectStreamClass.java (lookup): Split method and call
lookupForClassObject(). lookupForClassObject().
......
// FileDescriptor.java - Open file or device // 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. This file is part of libgcj.
...@@ -101,4 +101,6 @@ public final class FileDescriptor ...@@ -101,4 +101,6 @@ public final class FileDescriptor
// we want to make sure this has the value -1. This is the most // we want to make sure this has the value -1. This is the most
// efficient way to accomplish that. // efficient way to accomplish that.
private int fd = -1; private int fd = -1;
private long position = 0;
} }
// natFileDescriptor.cc - Native part of FileDescriptor class. // 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. This file is part of libgcj.
...@@ -147,6 +147,7 @@ java::io::FileDescriptor::write (jint b) ...@@ -147,6 +147,7 @@ java::io::FileDescriptor::write (jint b)
throw new IOException (JvNewStringLatin1 (strerror (errno))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
} }
} }
position++;
} }
void void
...@@ -177,6 +178,7 @@ java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len) ...@@ -177,6 +178,7 @@ java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
written += r; written += r;
len -= r; len -= r;
bytes += r; bytes += r;
position += r;
} }
} }
...@@ -193,7 +195,6 @@ void ...@@ -193,7 +195,6 @@ void
java::io::FileDescriptor::setLength (jlong pos) java::io::FileDescriptor::setLength (jlong pos)
{ {
struct stat sb; struct stat sb;
off_t orig;
#ifdef HAVE_FTRUNCATE #ifdef HAVE_FTRUNCATE
if (::fstat (fd, &sb)) if (::fstat (fd, &sb))
...@@ -202,10 +203,6 @@ java::io::FileDescriptor::setLength (jlong pos) ...@@ -202,10 +203,6 @@ java::io::FileDescriptor::setLength (jlong pos)
if ((jlong) sb.st_size == pos) if ((jlong) sb.st_size == pos)
return; 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 // 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 // ftruncate() extending the file. So we lseek() to 1 byte less
// than we want, and then we write a single byte at the end. // than we want, and then we write a single byte at the end.
...@@ -215,11 +212,15 @@ java::io::FileDescriptor::setLength (jlong pos) ...@@ -215,11 +212,15 @@ java::io::FileDescriptor::setLength (jlong pos)
throw new IOException (JvNewStringLatin1 (strerror (errno))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
char out = '\0'; char out = '\0';
int r = ::write (fd, &out, 1); 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))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
} }
else if (::ftruncate (fd, (off_t) pos)) else
throw new IOException (JvNewStringLatin1 (strerror (errno))); {
if (::ftruncate (fd, (off_t) pos))
throw new IOException (JvNewStringLatin1 (strerror (errno)));
position = pos;
}
#else /* HAVE_FTRUNCATE */ #else /* HAVE_FTRUNCATE */
throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented")); throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented"));
#endif /* HAVE_FTRUNCATE */ #endif /* HAVE_FTRUNCATE */
...@@ -230,19 +231,29 @@ java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc) ...@@ -230,19 +231,29 @@ java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
{ {
JvAssert (whence == SET || whence == CUR); JvAssert (whence == SET || whence == CUR);
jlong len = length (); if (eof_trunc)
jlong here = getFilePointer ();
if (eof_trunc
&& ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
{ {
whence = SET; jlong len = length ();
pos = len; 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); off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
if (r == -1) if (r == -1)
throw new IOException (JvNewStringLatin1 (strerror (errno))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
position = r;
return r; return r;
} }
...@@ -258,10 +269,7 @@ java::io::FileDescriptor::length (void) ...@@ -258,10 +269,7 @@ java::io::FileDescriptor::length (void)
jlong jlong
java::io::FileDescriptor::getFilePointer (void) java::io::FileDescriptor::getFilePointer (void)
{ {
off_t r = ::lseek (fd, 0, SEEK_CUR); return position;
if (r == -1)
throw new IOException (JvNewStringLatin1 (strerror (errno)));
return r;
} }
jint jint
...@@ -282,6 +290,7 @@ java::io::FileDescriptor::read (void) ...@@ -282,6 +290,7 @@ java::io::FileDescriptor::read (void)
} }
throw new IOException (JvNewStringLatin1 (strerror (errno))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
} }
position++;
return b & 0xFF; return b & 0xFF;
} }
...@@ -313,6 +322,7 @@ java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count) ...@@ -313,6 +322,7 @@ java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
} }
throw new IOException (JvNewStringLatin1 (strerror (errno))); throw new IOException (JvNewStringLatin1 (strerror (errno)));
} }
position += r;
return r; return r;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment