1 | // natFileDescriptorWin32.cc - Native part of FileDescriptor class.
|
---|
2 |
|
---|
3 | /* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This file is part of libgcj.
|
---|
6 |
|
---|
7 | This software is copyrighted work licensed under the terms of the
|
---|
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
|
---|
9 | details. */
|
---|
10 |
|
---|
11 | // FIXME: In order to support interrupting of IO operations, we
|
---|
12 | // need to change to use the windows asynchronous IO functions
|
---|
13 |
|
---|
14 | #include <config.h>
|
---|
15 |
|
---|
16 | #include <stdio.h>
|
---|
17 | #include <string.h>
|
---|
18 |
|
---|
19 | #include <windows.h>
|
---|
20 | #undef STRICT
|
---|
21 |
|
---|
22 | #include <gcj/cni.h>
|
---|
23 | #include <jvm.h>
|
---|
24 | #include <java/io/FileDescriptor.h>
|
---|
25 | #include <java/io/SyncFailedException.h>
|
---|
26 | #include <java/io/IOException.h>
|
---|
27 | #include <java/io/InterruptedIOException.h>
|
---|
28 | #include <java/io/EOFException.h>
|
---|
29 | #include <java/lang/ArrayIndexOutOfBoundsException.h>
|
---|
30 | #include <java/lang/NullPointerException.h>
|
---|
31 | #include <java/lang/String.h>
|
---|
32 | #include <java/lang/Thread.h>
|
---|
33 | #include <java/io/FileNotFoundException.h>
|
---|
34 |
|
---|
35 | void
|
---|
36 | java::io::FileDescriptor::init(void)
|
---|
37 | {
|
---|
38 | in = new java::io::FileDescriptor((jint)(GetStdHandle (STD_INPUT_HANDLE)));
|
---|
39 | out = new java::io::FileDescriptor((jint)(GetStdHandle (STD_OUTPUT_HANDLE)));
|
---|
40 | err = new java::io::FileDescriptor((jint)(GetStdHandle (STD_ERROR_HANDLE)));
|
---|
41 | }
|
---|
42 |
|
---|
43 | static char *
|
---|
44 | winerr (void)
|
---|
45 | {
|
---|
46 | static LPVOID last = NULL;
|
---|
47 | LPVOID old = NULL;
|
---|
48 |
|
---|
49 | if (last)
|
---|
50 | old = last;
|
---|
51 |
|
---|
52 | FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
53 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
54 | FORMAT_MESSAGE_IGNORE_INSERTS,
|
---|
55 | NULL,
|
---|
56 | GetLastError(),
|
---|
57 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
---|
58 | (LPTSTR) &last,
|
---|
59 | 0,
|
---|
60 | NULL);
|
---|
61 |
|
---|
62 | if (old)
|
---|
63 | LocalFree (old);
|
---|
64 |
|
---|
65 | return (char *)last;
|
---|
66 | }
|
---|
67 |
|
---|
68 | jboolean
|
---|
69 | java::io::FileDescriptor::valid (void) {
|
---|
70 | BY_HANDLE_FILE_INFORMATION info;
|
---|
71 | return GetFileInformationByHandle ((HANDLE)fd, &info) != 0;
|
---|
72 | }
|
---|
73 |
|
---|
74 | void
|
---|
75 | java::io::FileDescriptor::sync (void) {
|
---|
76 | if (! FlushFileBuffers ((HANDLE)fd))
|
---|
77 | throw new SyncFailedException (JvNewStringLatin1 (winerr ()));
|
---|
78 | }
|
---|
79 |
|
---|
80 | jint
|
---|
81 | java::io::FileDescriptor::open (jstring path, jint jflags) {
|
---|
82 |
|
---|
83 | HANDLE handle = NULL;
|
---|
84 | DWORD access = 0;
|
---|
85 | DWORD create = OPEN_EXISTING;
|
---|
86 | char buf[MAX_PATH] = "";
|
---|
87 |
|
---|
88 | jsize total = JvGetStringUTFRegion(path, 0, path->length(), buf);
|
---|
89 | buf[total] = '\0';
|
---|
90 |
|
---|
91 | JvAssert((jflags & READ) || (jflags & WRITE));
|
---|
92 |
|
---|
93 | if ((jflags & READ) && (jflags & WRITE))
|
---|
94 | {
|
---|
95 | access = GENERIC_READ | GENERIC_WRITE;
|
---|
96 | if (jflags & APPEND)
|
---|
97 | create = OPEN_ALWAYS;
|
---|
98 | else
|
---|
99 | create = CREATE_ALWAYS;
|
---|
100 | }
|
---|
101 | else if(jflags & READ)
|
---|
102 | access = GENERIC_READ;
|
---|
103 | else
|
---|
104 | {
|
---|
105 | access = GENERIC_WRITE;
|
---|
106 | if (jflags & APPEND)
|
---|
107 | create = OPEN_ALWAYS;
|
---|
108 | else
|
---|
109 | create = CREATE_ALWAYS;
|
---|
110 | }
|
---|
111 |
|
---|
112 | handle = CreateFile(buf, access, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, create, 0, NULL);
|
---|
113 |
|
---|
114 | if (handle == INVALID_HANDLE_VALUE)
|
---|
115 | {
|
---|
116 | char msg[MAX_PATH + 1000];
|
---|
117 | sprintf (msg, "%s: %s", buf, winerr ());
|
---|
118 | throw new FileNotFoundException (JvNewStringLatin1 (msg));
|
---|
119 | }
|
---|
120 |
|
---|
121 | return (jint)handle;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void
|
---|
125 | java::io::FileDescriptor::write (jint b)
|
---|
126 | {
|
---|
127 | DWORD bytesWritten;
|
---|
128 | jbyte buf = (jbyte)b;
|
---|
129 |
|
---|
130 | if (WriteFile ((HANDLE)fd, &buf, 1, &bytesWritten, NULL))
|
---|
131 | {
|
---|
132 | if (java::lang::Thread::interrupted())
|
---|
133 | {
|
---|
134 | InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
|
---|
135 | iioe->bytesTransferred = bytesWritten;
|
---|
136 | throw iioe;
|
---|
137 | }
|
---|
138 | if (bytesWritten != 1)
|
---|
139 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
140 | }
|
---|
141 | else
|
---|
142 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
143 | // FIXME: loop until bytesWritten == 1
|
---|
144 | }
|
---|
145 |
|
---|
146 | void
|
---|
147 | java::io::FileDescriptor::write(jbyteArray b, jint offset, jint len)
|
---|
148 | {
|
---|
149 | if (! b)
|
---|
150 | throw new java::lang::NullPointerException;
|
---|
151 | if(offset < 0 || len < 0 || offset + len > JvGetArrayLength (b))
|
---|
152 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
---|
153 |
|
---|
154 | jbyte *buf = elements (b) + offset;
|
---|
155 | DWORD bytesWritten;
|
---|
156 | if (WriteFile ((HANDLE)fd, buf, len, &bytesWritten, NULL))
|
---|
157 | {
|
---|
158 | if (java::lang::Thread::interrupted())
|
---|
159 | {
|
---|
160 | InterruptedIOException *iioe = new InterruptedIOException (JvNewStringLatin1 ("write interrupted"));
|
---|
161 | iioe->bytesTransferred = bytesWritten;
|
---|
162 | throw iioe;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | else
|
---|
166 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
167 | // FIXME: loop until bytesWritten == len
|
---|
168 | }
|
---|
169 |
|
---|
170 | void
|
---|
171 | java::io::FileDescriptor::close (void)
|
---|
172 | {
|
---|
173 | HANDLE save = (HANDLE)fd;
|
---|
174 | fd = (jint)INVALID_HANDLE_VALUE;
|
---|
175 | if (! CloseHandle (save))
|
---|
176 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
177 | }
|
---|
178 |
|
---|
179 | jint
|
---|
180 | java::io::FileDescriptor::seek (jlong pos, jint whence, jboolean eof_trunc)
|
---|
181 | {
|
---|
182 | JvAssert (whence == SET || whence == CUR);
|
---|
183 |
|
---|
184 | jlong len = length();
|
---|
185 | jlong here = getFilePointer();
|
---|
186 |
|
---|
187 | if (eof_trunc
|
---|
188 | && ((whence == SET && pos > len) || (whence == CUR && here + pos > len)))
|
---|
189 | {
|
---|
190 | whence = SET;
|
---|
191 | pos = len;
|
---|
192 | }
|
---|
193 |
|
---|
194 | LONG high = pos >> 32;
|
---|
195 | DWORD low = SetFilePointer ((HANDLE)fd, (DWORD)(0xffffffff & pos), &high, whence == SET ? FILE_BEGIN : FILE_CURRENT);
|
---|
196 | if ((low == 0xffffffff) && (GetLastError () != NO_ERROR))
|
---|
197 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
198 | return low;
|
---|
199 | }
|
---|
200 |
|
---|
201 | jlong
|
---|
202 | java::io::FileDescriptor::getFilePointer(void)
|
---|
203 | {
|
---|
204 | LONG high = 0;
|
---|
205 | DWORD low = SetFilePointer ((HANDLE)fd, 0, &high, FILE_CURRENT);
|
---|
206 | if ((low == 0xffffffff) && (GetLastError() != NO_ERROR))
|
---|
207 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
208 | return (((jlong)high) << 32L) | (jlong)low;
|
---|
209 | }
|
---|
210 |
|
---|
211 | jlong
|
---|
212 | java::io::FileDescriptor::length(void)
|
---|
213 | {
|
---|
214 | DWORD high;
|
---|
215 | DWORD low;
|
---|
216 |
|
---|
217 | low = GetFileSize ((HANDLE)fd, &high);
|
---|
218 | // FIXME: Error checking
|
---|
219 | return (((jlong)high) << 32L) | (jlong)low;
|
---|
220 | }
|
---|
221 |
|
---|
222 | jint
|
---|
223 | java::io::FileDescriptor::read(void)
|
---|
224 | {
|
---|
225 | CHAR buf;
|
---|
226 | DWORD read;
|
---|
227 |
|
---|
228 | if (! ReadFile ((HANDLE)fd, &buf, 1, &read, NULL))
|
---|
229 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
230 | if (! read)
|
---|
231 | return -1;
|
---|
232 | else
|
---|
233 | return (jint)(buf & 0xff);
|
---|
234 | }
|
---|
235 |
|
---|
236 | jint
|
---|
237 | java::io::FileDescriptor::read(jbyteArray buffer, jint offset, jint count)
|
---|
238 | {
|
---|
239 | if (! buffer)
|
---|
240 | throw new java::lang::NullPointerException;
|
---|
241 |
|
---|
242 | jsize bsize = JvGetArrayLength (buffer);
|
---|
243 | if (offset < 0 || count < 0 || offset + count > bsize)
|
---|
244 | throw new java::lang::ArrayIndexOutOfBoundsException;
|
---|
245 |
|
---|
246 | jbyte *bytes = elements (buffer) + offset;
|
---|
247 |
|
---|
248 | DWORD read;
|
---|
249 | if (! ReadFile((HANDLE)fd, bytes, count, &read, NULL))
|
---|
250 | throw new IOException (JvNewStringLatin1 (winerr ()));
|
---|
251 |
|
---|
252 | if (read == 0) return -1;
|
---|
253 | return (jint)read;
|
---|
254 | }
|
---|
255 |
|
---|
256 | jint
|
---|
257 | java::io::FileDescriptor::available(void)
|
---|
258 | {
|
---|
259 | // FIXME:
|
---|
260 | return length() - getFilePointer();
|
---|
261 | }
|
---|