aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/io/natFileDescriptorPosix.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/io/natFileDescriptorPosix.cc')
-rw-r--r--libjava/java/io/natFileDescriptorPosix.cc54
1 files changed, 32 insertions, 22 deletions
diff --git a/libjava/java/io/natFileDescriptorPosix.cc b/libjava/java/io/natFileDescriptorPosix.cc
index 80e6bad97ae..ad1dcc49c32 100644
--- a/libjava/java/io/natFileDescriptorPosix.cc
+++ b/libjava/java/io/natFileDescriptorPosix.cc
@@ -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 @@ java::io::FileDescriptor::write (jint b)
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
}
+ position++;
}
void
@@ -177,6 +178,7 @@ java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
written += r;
len -= r;
bytes += r;
+ position += r;
}
}
@@ -193,7 +195,6 @@ void
java::io::FileDescriptor::setLength (jlong pos)
{
struct stat sb;
- off_t orig;
#ifdef HAVE_FTRUNCATE
if (::fstat (fd, &sb))
@@ -202,10 +203,6 @@ java::io::FileDescriptor::setLength (jlong pos)
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 @@ java::io::FileDescriptor::setLength (jlong pos)
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 @@ java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
{
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 @@ java::io::FileDescriptor::length (void)
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 @@ java::io::FileDescriptor::read (void)
}
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
+ position++;
return b & 0xFF;
}
@@ -313,6 +322,7 @@ java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
}
throw new IOException (JvNewStringLatin1 (strerror (errno)));
}
+ position += r;
return r;
}
@@ -361,7 +371,7 @@ java::io::FileDescriptor::available (void)
off_t where = 0;
if (fstat (fd, &sb) != -1
&& S_ISREG (sb.st_mode)
- && (where = lseek (fd, SEEK_CUR, 0)) != (off_t) -1)
+ && (where = lseek (fd, 0, SEEK_CUR)) != (off_t) -1)
{
num = (long) (sb.st_size - where);
num_set = true;
@@ -387,6 +397,6 @@ java::io::FileDescriptor::available (void)
return (jint) num;
#else
- throw new IOException (JvNewStringLatin1 ("unimplemented"));
+ return 0;
#endif
}