aboutsummaryrefslogtreecommitdiff
path: root/libjava/java
diff options
context:
space:
mode:
authorTom Tromey <tromey@gcc.gnu.org>2000-03-15 22:03:19 +0000
committerTom Tromey <tromey@gcc.gnu.org>2000-03-15 22:03:19 +0000
commit878885b41142dd76e858b1f705bd31f357a5acc5 (patch)
tree6ff411e4ce4a6ce8717905d98f2d63b7479a6539 /libjava/java
parent1a7b4c697c6ea7b132db9fdb84d3c9ff97c48e19 (diff)
downloadgcc-878885b41142dd76e858b1f705bd31f357a5acc5.zip
gcc-878885b41142dd76e858b1f705bd31f357a5acc5.tar.gz
gcc-878885b41142dd76e858b1f705bd31f357a5acc5.tar.bz2
[multiple changes]
2000-03-15 Tom Tromey <tromey@cygnus.com> * java/io/natFileDescriptorWin32.cc (winerr): Now static. * prims.cc (win32_exception_handler): Reformatted. * include/win32-threads.h (_Jv_HaveCondDestroy): New define. (_Jv_HaveMutexDestroy): Likewise. 2000-03-15 Jon Beniston <jb7216@bristol.ac.uk> * java/io/natFileDescriptorWin32.cc: New file. * java/io/natFileWin32.cc: New file. * java/net/natInetAddress.cc: Added conditional inclusion of Windows / Winsock headers. * java/net/natPlainDatagramSocketImpl.cc: Added conditional inclusion of Windows / Winsock headers. * java/net/natPlainSocketImpl.cc: Added conditional inclusion of Windows / Winsock headers. * include/win32-signal.h: New file. * include/win32-threads.h: New file. * win32-threads.cc: New file. * exception.cc (win32_get_restart_frame): New function. * prims.cc (win32_exception_handler): New function. (main_init) Performs Winsock initialisation. (main_init) Installs exeception handler. From-SVN: r32567
Diffstat (limited to 'libjava/java')
-rw-r--r--libjava/java/io/natFileDescriptorWin32.cc250
-rw-r--r--libjava/java/io/natFileWin32.cc210
-rw-r--r--libjava/java/net/natInetAddress.cc13
-rw-r--r--libjava/java/net/natPlainDatagramSocketImpl.cc10
-rw-r--r--libjava/java/net/natPlainSocketImpl.cc11
5 files changed, 494 insertions, 0 deletions
diff --git a/libjava/java/io/natFileDescriptorWin32.cc b/libjava/java/io/natFileDescriptorWin32.cc
new file mode 100644
index 0000000..0bfd924
--- /dev/null
+++ b/libjava/java/io/natFileDescriptorWin32.cc
@@ -0,0 +1,250 @@
+// natFileDescriptorWin32.cc - Native part of FileDescriptor class.
+
+/* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
+
+ 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. */
+
+// FIXME: In order to support interrupting of IO operations, we
+// need to change to use the windows asynchronous IO functions
+
+#include <config.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include <windows.h>
+
+#include <gcj/cni.h>
+#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>
+#include <java/lang/String.h>
+#include <java/lang/Thread.h>
+#include <java/io/FileNotFoundException.h>
+
+static char *
+winerr (void)
+{
+ static LPVOID last = NULL;
+ LPVOID old = NULL;
+
+ if (last)
+ old = last;
+
+ FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL,
+ GetLastError(),
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ (LPTSTR) &last,
+ 0,
+ NULL);
+
+ if (old)
+ LocalFree (old);
+
+ return (char *)last;
+}
+
+jboolean
+java::io::FileDescriptor::valid (void) {
+ BY_HANDLE_FILE_INFORMATION info;
+ return GetFileInformationByHandle ((HANDLE)fd, &info) != 0;
+}
+
+void
+java::io::FileDescriptor::sync (void) {
+ if (! FlushFileBuffers ((HANDLE)fd))
+ JvThrow (new SyncFailedException (JvNewStringLatin1 (winerr ())));
+}
+
+jint
+java::io::FileDescriptor::open (jstring path, jint jflags) {
+
+ HANDLE handle = NULL;
+ DWORD access = 0;
+ DWORD share = FILE_SHARE_READ;
+ DWORD create = OPEN_EXISTING;
+ char buf[MAX_PATH] = "";
+
+ jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
+ buf[total] = '\0';
+
+ JvAssert((jflags & READ) || (jflags & WRITE));
+
+ if ((jflags & READ) && (jflags & WRITE))
+ {
+ access = GENERIC_READ | GENERIC_WRITE;
+ share = 0;
+ if (jflags & APPEND)
+ create = OPEN_ALWAYS;
+ else
+ create = CREATE_ALWAYS;
+ }
+ else if(jflags & READ)
+ access = GENERIC_READ;
+ else
+ {
+ access = GENERIC_WRITE;
+ share = 0;
+ if (jflags & APPEND)
+ create = OPEN_ALWAYS;
+ else
+ create = CREATE_ALWAYS;
+ }
+
+ handle = CreateFile(buf, access, share, NULL, create, 0, NULL);
+
+ if (handle == INVALID_HANDLE_VALUE)
+ {
+ char msg[MAX_PATH + 1000];
+ sprintf (msg, "%s: %s", buf, winerr ());
+ JvThrow (new FileNotFoundException (JvNewStringLatin1 (msg)));
+ }
+
+ return (jint)handle;
+}
+
+void
+java::io::FileDescriptor::write (jint b)
+{
+ DWORD bytesWritten;
+ jbyte buf = (jbyte)b;
+
+ if (WriteFile ((HANDLE)fd, &buf, 1, &bytesWritten, NULL))
+ {
+ if (java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = bytesWritten;
+ JvThrow (iioe);
+ }
+ if (bytesWritten != 1)
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+ }
+ else
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+ // FIXME: loop until bytesWritten == 1
+}
+
+void
+java::io::FileDescriptor::write(jbyteArray b, jint offset, jint len)
+{
+ if (! b)
+ JvThrow (new java::lang::NullPointerException);
+ if(offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
+ JvThrow (new java::lang::ArrayIndexOutOfBoundsException);
+
+ jbyte *buf = elements (b) + offset;
+ DWORD bytesWritten;
+ if (WriteFile ((HANDLE)fd, buf, len, &bytesWritten, NULL))
+ {
+ if (java::lang::Thread::interrupted())
+ {
+ InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
+ iioe->bytesTransferred = bytesWritten;
+ JvThrow (iioe);
+ }
+ }
+ else
+ JvThrow(new IOException (JvNewStringLatin1 (winerr ())));
+ // FIXME: loop until bytesWritten == len
+}
+
+void
+java::io::FileDescriptor::close (void)
+{
+ HANDLE save = (HANDLE)fd;
+ fd = (jint)INVALID_HANDLE_VALUE;
+ if (! CloseHandle (save))
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+}
+
+jint
+java::io::FileDescriptor::seek (jlong pos, jint whence)
+{
+ JvAssert (whence == SET || whence == CUR);
+
+ jlong len = length();
+ jlong here = getFilePointer();
+
+ if ((whence == SET && pos > len) || (whence == CUR && here + pos > len))
+ JvThrow (new EOFException);
+
+ LONG high = pos >> 32;
+ DWORD low = SetFilePointer ((HANDLE)fd, (DWORD)(0xffffffff & pos), &high, whence == SET ? FILE_BEGIN : FILE_CURRENT);
+ if ((low == 0xffffffff) && (GetLastError () != NO_ERROR))
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+ return low;
+}
+
+jlong
+java::io::FileDescriptor::getFilePointer(void)
+{
+ LONG high = 0;
+ DWORD low = SetFilePointer ((HANDLE)fd, 0, &high, FILE_CURRENT);
+ if ((low == 0xffffffff) && (GetLastError() != NO_ERROR))
+ JvThrow(new IOException (JvNewStringLatin1 (winerr ())));
+ return (((jlong)high) << 32L) | (jlong)low;
+}
+
+jlong
+java::io::FileDescriptor::length(void)
+{
+ DWORD high;
+ DWORD low;
+
+ low = GetFileSize ((HANDLE)fd, &high);
+ // FIXME: Error checking
+ return (((jlong)high) << 32L) | (jlong)low;
+}
+
+jint
+java::io::FileDescriptor::read(void)
+{
+ CHAR buf;
+ DWORD read;
+
+ if (! ReadFile ((HANDLE)fd, &buf, 1, &read, NULL))
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+ if (! read)
+ return -1;
+ else
+ return (jint)(buf & 0xff);
+}
+
+jint
+java::io::FileDescriptor::read(jbyteArray buffer, jint offset, jint count)
+{
+ if (! buffer)
+ JvThrow(new java::lang::NullPointerException);
+
+ jsize bsize = JvGetArrayLength (buffer);
+ if (offset < 0 || count < 0 || offset + count > bsize)
+ JvThrow (new java::lang::ArrayIndexOutOfBoundsException);
+
+ jbyte *bytes = elements (buffer) + offset;
+
+ DWORD read;
+ if (! ReadFile((HANDLE)fd, bytes, count, &read, NULL))
+ JvThrow (new IOException (JvNewStringLatin1 (winerr ())));
+
+ return (jint)read;
+}
+
+jint
+java::io::FileDescriptor::available(void)
+{
+ // FIXME:
+ return length() - getFilePointer();
+}
diff --git a/libjava/java/io/natFileWin32.cc b/libjava/java/io/natFileWin32.cc
new file mode 100644
index 0000000..a56845a
--- /dev/null
+++ b/libjava/java/io/natFileWin32.cc
@@ -0,0 +1,210 @@
+// natFileWin32.cc - Native part of File class.
+
+/* Copyright (C) 1998, 1999 Red Hat, Inc.
+
+ 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>
+
+#include <stdio.h>
+#include <string.h>
+
+#include <windows.h>
+
+#include <gcj/cni.h>
+#include <jvm.h>
+#include <java/io/File.h>
+#include <java/io/IOException.h>
+#include <java/util/Vector.h>
+#include <java/lang/String.h>
+#include <java/io/FilenameFilter.h>
+#include <java/lang/System.h>
+
+jboolean
+java::io::File::access (jstring canon, jint query)
+{
+ if (! canon)
+ return false;
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ JvAssert (query == READ || query == WRITE || query == EXISTS);
+
+ // FIXME: Is it possible to differentiate between existing and reading?
+ // If the file exists but cannot be read because of the secuirty attributes
+ // on an NTFS disk this wont work (it reports it can be read but cant)
+ // Could we use something from the security API?
+ DWORD attributes = GetFileAttributes (buf);
+ if ((query == EXISTS) || (query == READ))
+ return (attributes == 0xffffffff) ? false : true;
+ else
+ return ((attributes != 0xffffffff) && ((attributes & FILE_ATTRIBUTE_READONLY) == 0)) ? true : false;
+}
+
+jboolean
+java::io::File::stat (jstring canon, jint query)
+{
+ if (! canon)
+ return false;
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ JvAssert (query == DIRECTORY || query == ISFILE);
+
+ DWORD attributes = GetFileAttributes (buf);
+ if (attributes == 0xffffffff)
+ return false;
+
+ if (query == DIRECTORY)
+ return attributes & FILE_ATTRIBUTE_DIRECTORY ? true : false;
+ else
+ return attributes & FILE_ATTRIBUTE_DIRECTORY ? false : true;
+}
+
+jlong
+java::io::File::attr (jstring canon, jint query)
+{
+ if (! canon)
+ return false;
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ JvAssert (query == MODIFIED || query == LENGTH);
+
+ WIN32_FILE_ATTRIBUTE_DATA info;
+ if (! GetFileAttributesEx(buf, GetFileExInfoStandard, &info))
+ return 0;
+
+ if (query == LENGTH)
+ return ((long long)info.nFileSizeHigh) << 32 | (unsigned long long)info.nFileSizeLow;
+ else {
+ // FIXME? This is somewhat compiler dependant (the LL constant suffix)
+ // The file time as return by windows is the number of 100-nanosecond intervals since January 1, 1601
+ return (((((long long)info.ftLastWriteTime.dwHighDateTime) << 32) | ((unsigned long long)info.ftLastWriteTime.dwLowDateTime)) - 116444736000000000LL) / 10000LL;
+ }
+}
+
+jstring
+java::io::File::getCanonicalPath (void)
+{
+ char buf[MAX_PATH], buf2[MAX_PATH];
+ jsize total = JvGetStringUTFRegion (path, 0, path->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ LPTSTR unused;
+ if(!GetFullPathName(buf, MAX_PATH, buf2, &unused))
+ _Jv_Throw (new IOException (JvNewStringLatin1 ("GetFullPathName failed")));
+
+ // FIXME: what encoding to assume for file names? This affects many
+ // calls.
+ return JvNewStringUTF(buf2);
+}
+
+jboolean
+java::io::File::isAbsolute (void)
+{
+ if (path->charAt(0) == '/' || path->charAt(0) == '\\')
+ return true;
+ if (path->length() < 3)
+ return false;
+ // Hard-code A-Za-z because Windows (I think) can't use non-ASCII
+ // letters as drive names.
+ if ((path->charAt(0) < 'a' || path->charAt(0) > 'z')
+ && (path->charAt(0) < 'A' || path->charAt(0) > 'Z'))
+ return false;
+ return (path->charAt(1) == ':'
+ && (path->charAt(2) == '/' || path->charAt(2) == '\\'));
+}
+
+jstringArray
+java::io::File::performList (jstring canon, FilenameFilter *filter)
+{
+ if (! canon)
+ return NULL;
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion (canon, 0, canon->length(), buf);
+ // FIXME?
+ strcpy(&buf[total], "\\*.*");
+
+ WIN32_FIND_DATA data;
+ HANDLE handle = FindFirstFile (buf, &data);
+ if (handle == INVALID_HANDLE_VALUE)
+ return NULL;
+
+ java::util::Vector *vec = new java::util::Vector ();
+
+ do
+ {
+ if (strcmp (data.cFileName, ".") && strcmp (data.cFileName, ".."))
+ {
+ jstring name = JvNewStringUTF (data.cFileName);
+ if (! filter || (filter && filter->accept(this, name)))
+ vec->addElement (name);
+ }
+ }
+ while (FindNextFile (handle, &data));
+
+ if (GetLastError () != ERROR_NO_MORE_FILES)
+ return NULL;
+
+ FindClose (handle);
+
+ jobjectArray ret = JvNewObjectArray (vec->size(), canon->getClass(), NULL);
+ vec->copyInto (ret);
+ return reinterpret_cast<jstringArray> (ret);
+}
+
+jboolean
+java::io::File::performMkdir (void)
+{
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ return (CreateDirectory(buf, NULL)) ? true : false;
+}
+
+jboolean
+java::io::File::performRenameTo (File *dest)
+{
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+ char buf2[MAX_PATH];
+ total = JvGetStringUTFRegion(dest->path, 0, dest->path->length(), buf2);
+ // FIXME?
+ buf2[total] = '\0';
+
+ return (MoveFile(buf, buf2)) ? true : false;
+}
+
+jboolean
+java::io::File::performDelete (jstring canon)
+{
+ char buf[MAX_PATH];
+ jsize total = JvGetStringUTFRegion(canon, 0, canon->length(), buf);
+ // FIXME?
+ buf[total] = '\0';
+
+ DWORD attributes = GetFileAttributes (buf);
+ if (attributes == 0xffffffff)
+ return false;
+
+ if (attributes & FILE_ATTRIBUTE_DIRECTORY)
+ return (RemoveDirectory (buf)) ? true : false;
+ else
+ return (DeleteFile (buf)) ? true : false;
+}
diff --git a/libjava/java/net/natInetAddress.cc b/libjava/java/net/natInetAddress.cc
index f7a4b44..a25e643 100644
--- a/libjava/java/net/natInetAddress.cc
+++ b/libjava/java/net/natInetAddress.cc
@@ -14,6 +14,17 @@ details. */
# define _REENTRANT 1
#endif
+#ifdef USE_WINSOCK
+
+#include <windows.h>
+#include <winsock.h>
+
+#ifndef MAXHOSTNAMELEN
+#define MAXHOSTNAMELEN 64
+#endif /* MAXHOSTNAMELEN */
+
+#else
+
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
@@ -35,6 +46,8 @@ details. */
#include <netdb.h>
#endif
+#endif /* USE_WINSOCK */
+
#include <gcj/cni.h>
#include <jvm.h>
#include <java/net/InetAddress.h>
diff --git a/libjava/java/net/natPlainDatagramSocketImpl.cc b/libjava/java/net/natPlainDatagramSocketImpl.cc
index dfa13dc..94383f8 100644
--- a/libjava/java/net/natPlainDatagramSocketImpl.cc
+++ b/libjava/java/net/natPlainDatagramSocketImpl.cc
@@ -8,6 +8,15 @@ details. */
#include <config.h>
+#ifdef USE_WINSOCK
+#include <windows.h>
+#include <winsock.h>
+#include <errno.h>
+#include <string.h>
+#ifndef ENOPROTOOPT
+#define ENOPROTOOPT 109
+#endif
+#else /* USE_WINSOCK */
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
@@ -26,6 +35,7 @@ details. */
#endif
#include <errno.h>
#include <string.h>
+#endif /* USE_WINSOCK */
#if HAVE_BSTRING_H
// Needed for bzero, implicitly used by FD_ZERO on IRIX 5.2
diff --git a/libjava/java/net/natPlainSocketImpl.cc b/libjava/java/net/natPlainSocketImpl.cc
index 459ad7e..5218746 100644
--- a/libjava/java/net/natPlainSocketImpl.cc
+++ b/libjava/java/net/natPlainSocketImpl.cc
@@ -8,7 +8,17 @@ details. */
#include <config.h>
+
#ifndef DISABLE_JAVA_NET
+#ifdef USE_WINSOCK
+#include <windows.h>
+#include <winsock.h>
+#include <errno.h>
+#include <string.h>
+#ifndef ENOPROTOOPT
+#define ENOPROTOOPT 109
+#endif
+#else /* USE_WINSOCK */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
@@ -19,6 +29,7 @@ details. */
#include <netinet/tcp.h>
#include <errno.h>
#include <string.h>
+#endif /* USE_WINSOCK */
#endif /* DISABLE_JAVA_NET */
#if HAVE_BSTRING_H