1999-04-07 22:42:40 +08:00
|
|
|
// natFileDescriptor.cc - Native part of FileDescriptor class.
|
|
|
|
|
2003-01-04 11:53:00 +08:00
|
|
|
/* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
This file is part of libgcj.
|
|
|
|
|
|
|
|
This software is copyrighted work licensed under the terms of the
|
|
|
|
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-08-03 05:54:04 +08:00
|
|
|
#include "posix.h"
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#define BSD_COMP /* Get FIONREAD on Solaris2. */
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Pick up FIONREAD on Solaris 2.5.
|
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
#include <sys/filio.h>
|
|
|
|
#endif
|
|
|
|
|
1999-09-11 06:03:10 +08:00
|
|
|
#include <gcj/cni.h>
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <jvm.h>
|
|
|
|
#include <java/io/FileDescriptor.h>
|
|
|
|
#include <java/io/SyncFailedException.h>
|
|
|
|
#include <java/io/IOException.h>
|
|
|
|
#include <java/io/InterruptedIOException.h>
|
|
|
|
#include <java/io/EOFException.h>
|
|
|
|
#include <java/lang/ArrayIndexOutOfBoundsException.h>
|
|
|
|
#include <java/lang/NullPointerException.h>
|
2000-06-28 20:24:10 +08:00
|
|
|
#include <java/lang/System.h>
|
1999-04-07 22:42:40 +08:00
|
|
|
#include <java/lang/String.h>
|
|
|
|
#include <java/lang/Thread.h>
|
|
|
|
#include <java/io/FileNotFoundException.h>
|
|
|
|
|
|
|
|
#define NO_FSYNC_MESSAGE "sync unsupported"
|
|
|
|
|
2002-03-07 06:37:26 +08:00
|
|
|
void
|
2002-03-07 07:23:34 +08:00
|
|
|
java::io::FileDescriptor::init (void)
|
2002-03-07 06:37:26 +08:00
|
|
|
{
|
2002-03-07 07:23:34 +08:00
|
|
|
in = new java::io::FileDescriptor(0);
|
|
|
|
out = new java::io::FileDescriptor(1);
|
|
|
|
err = new java::io::FileDescriptor(2);
|
2002-03-07 06:37:26 +08:00
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
jboolean
|
|
|
|
java::io::FileDescriptor::valid (void)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
2002-03-07 07:23:34 +08:00
|
|
|
return fd >= 0 && ::fstat (fd, &sb) == 0;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
java::io::FileDescriptor::sync (void)
|
|
|
|
{
|
|
|
|
// Some files don't support fsync. We don't bother reporting these
|
|
|
|
// as errors.
|
|
|
|
#ifdef HAVE_FSYNC
|
|
|
|
if (::fsync (fd) && errno != EROFS && errno != EINVAL)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new SyncFailedException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 22:42:40 +08:00
|
|
|
#else
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new SyncFailedException (JvNewStringLatin1 (NO_FSYNC_MESSAGE));
|
1999-04-07 22:42:40 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::io::FileDescriptor::open (jstring path, jint jflags)
|
|
|
|
{
|
2001-06-01 01:33:05 +08:00
|
|
|
char *buf = (char *) _Jv_AllocBytes (_Jv_GetStringUTFLength (path) + 1);
|
1999-04-07 22:42:40 +08:00
|
|
|
jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
|
|
|
|
buf[total] = '\0';
|
|
|
|
int flags = 0;
|
|
|
|
#ifdef O_BINARY
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
JvAssert ((jflags & READ) || (jflags & WRITE));
|
2000-04-16 11:09:27 +08:00
|
|
|
int mode = 0666;
|
1999-04-07 22:42:40 +08:00
|
|
|
if ((jflags & READ) && (jflags & WRITE))
|
2001-03-22 01:23:09 +08:00
|
|
|
flags |= O_RDWR | O_CREAT;
|
1999-04-07 22:42:40 +08:00
|
|
|
else if ((jflags & READ))
|
|
|
|
flags |= O_RDONLY;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= O_WRONLY | O_CREAT;
|
|
|
|
if ((jflags & APPEND))
|
|
|
|
flags |= O_APPEND;
|
|
|
|
else
|
|
|
|
flags |= O_TRUNC;
|
2000-02-12 01:32:52 +08:00
|
|
|
|
|
|
|
if ((jflags & EXCL))
|
|
|
|
{
|
|
|
|
flags |= O_EXCL;
|
|
|
|
// In this case we are making a temp file.
|
|
|
|
mode = 0600;
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2000-02-12 01:32:52 +08:00
|
|
|
int fd = ::open (buf, flags, mode);
|
2000-06-28 20:24:10 +08:00
|
|
|
if (fd == -1 && errno == EMFILE)
|
|
|
|
{
|
|
|
|
// Because finalize () calls close () we might be able to continue.
|
|
|
|
java::lang::System::gc ();
|
|
|
|
java::lang::System::runFinalization ();
|
|
|
|
fd = ::open (buf, flags, mode);
|
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
if (fd == -1)
|
|
|
|
{
|
|
|
|
char msg[MAXPATHLEN + 200];
|
2001-06-26 11:27:57 +08:00
|
|
|
// We choose the formatting here for JDK compatibility, believe
|
|
|
|
// it or not.
|
|
|
|
sprintf (msg, "%s (%s)", buf, strerror (errno));
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new FileNotFoundException (JvNewStringLatin1 (msg));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
2002-03-11 01:59:23 +08:00
|
|
|
|
|
|
|
_Jv_platform_close_on_exec (fd);
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
java::io::FileDescriptor::write (jint b)
|
|
|
|
{
|
|
|
|
jbyte d = (jbyte) b;
|
2001-06-01 01:33:05 +08:00
|
|
|
int r = 0;
|
|
|
|
while (r != 1)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2001-06-01 01:33:05 +08:00
|
|
|
r = ::write (fd, &d, 1);
|
2001-09-21 12:23:31 +08:00
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
if (java::lang::Thread::interrupted())
|
|
|
|
{
|
|
|
|
InterruptedIOException *iioe
|
|
|
|
= new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
iioe->bytesTransferred = r == -1 ? 0 : r;
|
|
|
|
throw iioe;
|
|
|
|
}
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
2001-06-01 01:33:05 +08:00
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
2003-01-04 11:53:00 +08:00
|
|
|
position++;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
java::io::FileDescriptor::write (jbyteArray b, jint offset, jint len)
|
|
|
|
{
|
|
|
|
if (! b)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new java::lang::NullPointerException;
|
1999-04-07 22:42:40 +08:00
|
|
|
if (offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new java::lang::ArrayIndexOutOfBoundsException;
|
1999-04-07 22:42:40 +08:00
|
|
|
jbyte *bytes = elements (b) + offset;
|
2001-06-01 01:33:05 +08:00
|
|
|
|
|
|
|
int written = 0;
|
|
|
|
while (len > 0)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2001-06-01 01:33:05 +08:00
|
|
|
int r = ::write (fd, bytes, len);
|
2001-09-21 12:23:31 +08:00
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
if (java::lang::Thread::interrupted())
|
|
|
|
{
|
|
|
|
InterruptedIOException *iioe
|
|
|
|
= new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
iioe->bytesTransferred = written;
|
|
|
|
throw iioe;
|
|
|
|
}
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
2001-06-01 01:33:05 +08:00
|
|
|
}
|
|
|
|
|
2001-09-21 12:23:31 +08:00
|
|
|
written += r;
|
2001-06-01 01:33:05 +08:00
|
|
|
len -= r;
|
|
|
|
bytes += r;
|
2003-01-04 11:53:00 +08:00
|
|
|
position += r;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
java::io::FileDescriptor::close (void)
|
|
|
|
{
|
|
|
|
jint save = fd;
|
|
|
|
fd = -1;
|
|
|
|
if (::close (save))
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
2002-07-25 01:48:41 +08:00
|
|
|
void
|
|
|
|
java::io::FileDescriptor::setLength (jlong pos)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
2002-08-30 02:05:15 +08:00
|
|
|
#ifdef HAVE_FTRUNCATE
|
2002-07-25 01:48:41 +08:00
|
|
|
if (::fstat (fd, &sb))
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
|
|
|
|
if ((jlong) sb.st_size == pos)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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);
|
2003-01-04 11:53:00 +08:00
|
|
|
if (r <= 0 || ::lseek (fd, position, SEEK_SET) == -1)
|
2002-07-25 01:48:41 +08:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
}
|
2003-01-04 11:53:00 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (::ftruncate (fd, (off_t) pos))
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
position = pos;
|
|
|
|
}
|
2002-08-30 02:05:15 +08:00
|
|
|
#else /* HAVE_FTRUNCATE */
|
|
|
|
throw new IOException (JvNewStringLatin1 ("FileDescriptor.setLength not implemented"));
|
|
|
|
#endif /* HAVE_FTRUNCATE */
|
2002-07-25 01:48:41 +08:00
|
|
|
}
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
jint
|
2001-08-03 07:46:39 +08:00
|
|
|
java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
JvAssert (whence == SET || whence == CUR);
|
|
|
|
|
2003-01-04 11:53:00 +08:00
|
|
|
if (eof_trunc)
|
2001-08-03 07:46:39 +08:00
|
|
|
{
|
2003-01-04 11:53:00 +08:00
|
|
|
jlong len = length ();
|
|
|
|
if (whence == SET)
|
|
|
|
{
|
|
|
|
if (pos > len)
|
|
|
|
pos = len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jlong here = getFilePointer ();
|
|
|
|
if (here + pos > len)
|
|
|
|
{
|
|
|
|
pos = len;
|
|
|
|
whence = SET;
|
|
|
|
}
|
|
|
|
}
|
2001-08-03 07:46:39 +08:00
|
|
|
}
|
1999-04-07 22:42:40 +08:00
|
|
|
|
|
|
|
off_t r = ::lseek (fd, (off_t) pos, whence == SET ? SEEK_SET : SEEK_CUR);
|
|
|
|
if (r == -1)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
2003-01-04 11:53:00 +08:00
|
|
|
position = r;
|
1999-04-07 22:42:40 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
jlong
|
|
|
|
java::io::FileDescriptor::length (void)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
if (::fstat (fd, &sb))
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 22:42:40 +08:00
|
|
|
return sb.st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
jlong
|
|
|
|
java::io::FileDescriptor::getFilePointer (void)
|
|
|
|
{
|
2003-01-04 11:53:00 +08:00
|
|
|
return position;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::io::FileDescriptor::read (void)
|
|
|
|
{
|
|
|
|
jbyte b;
|
|
|
|
int r = ::read (fd, &b, 1);
|
|
|
|
if (r == 0)
|
|
|
|
return -1;
|
2001-09-21 12:23:31 +08:00
|
|
|
if (r == -1)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
2001-09-21 12:23:31 +08:00
|
|
|
if (java::lang::Thread::interrupted())
|
|
|
|
{
|
|
|
|
InterruptedIOException *iioe
|
|
|
|
= new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
iioe->bytesTransferred = r == -1 ? 0 : r;
|
|
|
|
throw iioe;
|
|
|
|
}
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
2003-01-04 11:53:00 +08:00
|
|
|
position++;
|
1999-04-07 22:42:40 +08:00
|
|
|
return b & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::io::FileDescriptor::read (jbyteArray buffer, jint offset, jint count)
|
|
|
|
{
|
|
|
|
if (! buffer)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new java::lang::NullPointerException;
|
1999-04-07 22:42:40 +08:00
|
|
|
jsize bsize = JvGetArrayLength (buffer);
|
|
|
|
if (offset < 0 || count < 0 || offset + count > bsize)
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new java::lang::ArrayIndexOutOfBoundsException;
|
2002-11-11 06:23:53 +08:00
|
|
|
|
|
|
|
// Must return 0 if an attempt is made to read 0 bytes.
|
|
|
|
if (count == 0)
|
|
|
|
return 0;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
jbyte *bytes = elements (buffer) + offset;
|
|
|
|
int r = ::read (fd, bytes, count);
|
|
|
|
if (r == 0)
|
|
|
|
return -1;
|
2001-09-21 12:23:31 +08:00
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
if (java::lang::Thread::interrupted())
|
|
|
|
{
|
|
|
|
InterruptedIOException *iioe
|
|
|
|
= new InterruptedIOException (JvNewStringLatin1 (strerror (errno)));
|
|
|
|
iioe->bytesTransferred = r == -1 ? 0 : r;
|
|
|
|
throw iioe;
|
|
|
|
}
|
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
2003-01-04 11:53:00 +08:00
|
|
|
position += r;
|
1999-04-07 22:42:40 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
jint
|
|
|
|
java::io::FileDescriptor::available (void)
|
|
|
|
{
|
1999-09-03 15:42:40 +08:00
|
|
|
#if defined (FIONREAD) || defined (HAVE_SELECT) || defined (HAVE_FSTAT)
|
|
|
|
long num = 0;
|
|
|
|
int r = 0;
|
|
|
|
bool num_set = false;
|
|
|
|
|
1999-04-07 22:42:40 +08:00
|
|
|
#if defined (FIONREAD)
|
1999-09-03 15:42:40 +08:00
|
|
|
r = ::ioctl (fd, FIONREAD, &num);
|
|
|
|
if (r == -1 && errno == ENOTTY)
|
|
|
|
{
|
|
|
|
// If the ioctl doesn't work, we don't care.
|
|
|
|
r = 0;
|
|
|
|
num = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
num_set = true;
|
1999-04-07 22:42:40 +08:00
|
|
|
#elif defined (HAVE_SELECT)
|
|
|
|
if (fd < 0)
|
1999-09-03 15:42:40 +08:00
|
|
|
{
|
|
|
|
errno = EBADF;
|
|
|
|
r = -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (r == -1)
|
|
|
|
{
|
|
|
|
posix_error:
|
exception.cc (java_eh_info): Make value type jthrowable.
* exception.cc (java_eh_info): Make value type jthrowable.
(_Jv_type_matcher): Remove now unneeded cast.
(_Jv_Throw): Make argument type jthrowable. Munge name
for SJLJ_EXCEPTIONS here ...
* gcj/cni.h: ... not here.
(JvThrow): Remove.
* gcj/javaprims.h (_Jv_Throw, _Jv_Sjlj_Throw): Update declarations.
* defineclass.cc, interpret.cc, jni.cc, posix-threads.cc,
prims.cc, resolve.cc, gnu/gcj/runtime/natFirstThread.cc,
gnu/gcj/xlib/natDrawable.cc, gnu/gcj/xlib/natFont.cc,
gnu/gcj/xlib/natWMSizeHints.cc, gnu/gcj/xlib/natWindowAttributes.cc,
gnu/gcj/xlib/natXImage.cc, java/io/natFile.cc,
java/io/natFileDescriptorEcos.cc, java/io/natFileDescriptorPosix.cc,
java/io/natFileDescriptorWin32.cc, java/io/natFileWin32.cc,
java/lang/natClass.cc, java/lang/natClassLoader.cc,
java/lang/natDouble.cc, java/lang/natObject.cc,
java/lang/natPosixProcess.cc, java/lang/natRuntime.cc,
java/lang/natString.cc, java/lang/natSystem.cc,
java/lang/natThread.cc, java/lang/reflect/natArray.cc,
java/lang/reflect/natConstructor.cc, java/lang/reflect/natField.cc,
java/lang/reflect/natMethod.cc, java/util/zip/natDeflater.cc,
java/util/zip/natInflater.cc:
Use throw, not JvThrow or _Jv_Throw.
From-SVN: r40838
2001-03-26 15:05:32 +08:00
|
|
|
throw new IOException (JvNewStringLatin1 (strerror (errno)));
|
1999-09-03 15:42:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we didn't get anything, and we have fstat, then see if see if
|
|
|
|
// we're reading a regular file. On many systems, FIONREAD does not
|
|
|
|
// work on regular files; select() likewise returns a useless
|
|
|
|
// result. This is run incorrectly when FIONREAD does work on
|
|
|
|
// regular files and we are at the end of the file. However, this
|
|
|
|
// case probably isn't very important.
|
|
|
|
#if defined (HAVE_FSTAT)
|
|
|
|
if (! num_set)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
2000-04-16 11:09:27 +08:00
|
|
|
off_t where = 0;
|
1999-09-03 15:42:40 +08:00
|
|
|
if (fstat (fd, &sb) != -1
|
|
|
|
&& S_ISREG (sb.st_mode)
|
2003-03-10 06:50:02 +08:00
|
|
|
&& (where = lseek (fd, 0, SEEK_CUR)) != (off_t) -1)
|
1999-09-03 15:42:40 +08:00
|
|
|
{
|
|
|
|
num = (long) (sb.st_size - where);
|
|
|
|
num_set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_FSTAT */
|
|
|
|
|
|
|
|
#if defined (HAVE_SELECT)
|
|
|
|
if (! num_set)
|
1999-04-07 22:42:40 +08:00
|
|
|
{
|
|
|
|
fd_set rd;
|
|
|
|
FD_ZERO (&rd);
|
|
|
|
FD_SET (fd, &rd);
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
2000-08-03 05:54:04 +08:00
|
|
|
r = _Jv_select (fd + 1, &rd, NULL, NULL, &tv);
|
1999-09-03 15:42:40 +08:00
|
|
|
if (r == -1)
|
|
|
|
goto posix_error;
|
|
|
|
num = r == 0 ? 0 : 1;
|
1999-04-07 22:42:40 +08:00
|
|
|
}
|
1999-09-03 15:42:40 +08:00
|
|
|
#endif /* HAVE_SELECT */
|
|
|
|
|
|
|
|
return (jint) num;
|
1999-04-07 22:42:40 +08:00
|
|
|
#else
|
2003-03-10 06:50:02 +08:00
|
|
|
return 0;
|
1999-04-07 22:42:40 +08:00
|
|
|
#endif
|
|
|
|
}
|