author | martin |
Wed, 01 Sep 2010 09:45:08 -0700 | |
changeset 6520 | 0e7cf575332e |
parent 5506 | 202f599c92aa |
child 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <sys/types.h> |
|
27 |
#include <string.h> |
|
28 |
#include "jni.h" |
|
29 |
#include "jni_util.h" |
|
30 |
#include "jvm.h" |
|
31 |
#include "jlong.h" |
|
32 |
#include "sun_nio_ch_IOUtil.h" |
|
33 |
#include "nio.h" |
|
34 |
#include "nio_util.h" |
|
35 |
||
36 |
static jfieldID fd_fdID; /* for jint 'fd' in java.io.FileDescriptor */ |
|
37 |
||
38 |
||
39 |
JNIEXPORT void JNICALL |
|
40 |
Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) |
|
41 |
{ |
|
42 |
clazz = (*env)->FindClass(env, "java/io/FileDescriptor"); |
|
43 |
fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"); |
|
44 |
} |
|
45 |
||
46 |
JNIEXPORT jboolean JNICALL |
|
47 |
Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz, |
|
48 |
jbyteArray randArray) |
|
49 |
{ |
|
50 |
JNU_ThrowByName(env, "java/lang/UnsupportedOperationException", NULL); |
|
51 |
return JNI_FALSE; |
|
52 |
} |
|
53 |
||
54 |
JNIEXPORT jint JNICALL |
|
55 |
Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) |
|
56 |
{ |
|
57 |
return (*env)->GetIntField(env, fdo, fd_fdID); |
|
58 |
} |
|
59 |
||
60 |
JNIEXPORT void JNICALL |
|
61 |
Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val) |
|
62 |
{ |
|
63 |
(*env)->SetIntField(env, fdo, fd_fdID, val); |
|
64 |
} |
|
65 |
||
66 |
static int |
|
67 |
configureBlocking(int fd, jboolean blocking) |
|
68 |
{ |
|
69 |
int flags = fcntl(fd, F_GETFL); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
70 |
int newflags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK); |
2 | 71 |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
72 |
return (flags == newflags) ? 0 : fcntl(fd, F_SETFL, newflags); |
2 | 73 |
} |
74 |
||
75 |
JNIEXPORT void JNICALL |
|
76 |
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, |
|
77 |
jobject fdo, jboolean blocking) |
|
78 |
{ |
|
79 |
if (configureBlocking(fdval(env, fdo), blocking) < 0) |
|
80 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); |
|
81 |
} |
|
82 |
||
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
83 |
JNIEXPORT jlong JNICALL |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
84 |
Java_sun_nio_ch_IOUtil_makePipe(JNIEnv *env, jobject this, jboolean blocking) |
2 | 85 |
{ |
86 |
int fd[2]; |
|
87 |
||
88 |
if (pipe(fd) < 0) { |
|
89 |
JNU_ThrowIOExceptionWithLastError(env, "Pipe failed"); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
90 |
return 0; |
2 | 91 |
} |
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
92 |
if (blocking == JNI_FALSE) { |
2 | 93 |
if ((configureBlocking(fd[0], JNI_FALSE) < 0) |
94 |
|| (configureBlocking(fd[1], JNI_FALSE) < 0)) { |
|
95 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed"); |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
96 |
close(fd[0]); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
97 |
close(fd[1]); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
98 |
return 0; |
2 | 99 |
} |
100 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
101 |
return ((jlong) fd[0] << 32) | (jlong) fd[1]; |
2 | 102 |
} |
103 |
||
104 |
JNIEXPORT jboolean JNICALL |
|
105 |
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd) |
|
106 |
{ |
|
107 |
char buf[128]; |
|
108 |
int tn = 0; |
|
109 |
||
110 |
for (;;) { |
|
111 |
int n = read(fd, buf, sizeof(buf)); |
|
112 |
tn += n; |
|
113 |
if ((n < 0) && (errno != EAGAIN)) |
|
114 |
JNU_ThrowIOExceptionWithLastError(env, "Drain"); |
|
115 |
if (n == (int)sizeof(buf)) |
|
116 |
continue; |
|
117 |
return (tn > 0) ? JNI_TRUE : JNI_FALSE; |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
||
122 |
/* Declared in nio_util.h for use elsewhere in NIO */ |
|
123 |
||
124 |
jint |
|
125 |
convertReturnVal(JNIEnv *env, jint n, jboolean reading) |
|
126 |
{ |
|
127 |
if (n > 0) /* Number of bytes written */ |
|
128 |
return n; |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
129 |
else if (n == 0) { |
2 | 130 |
if (reading) { |
131 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
132 |
} else { |
|
133 |
return 0; |
|
134 |
} |
|
135 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
136 |
else if (errno == EAGAIN) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
137 |
return IOS_UNAVAILABLE; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
138 |
else if (errno == EINTR) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
139 |
return IOS_INTERRUPTED; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
140 |
else { |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
141 |
const char *msg = reading ? "Read failed" : "Write failed"; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
142 |
JNU_ThrowIOExceptionWithLastError(env, msg); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
143 |
return IOS_THROWN; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
144 |
} |
2 | 145 |
} |
146 |
||
147 |
/* Declared in nio_util.h for use elsewhere in NIO */ |
|
148 |
||
149 |
jlong |
|
150 |
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) |
|
151 |
{ |
|
152 |
if (n > 0) /* Number of bytes written */ |
|
153 |
return n; |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
154 |
else if (n == 0) { |
2 | 155 |
if (reading) { |
156 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
157 |
} else { |
|
158 |
return 0; |
|
159 |
} |
|
160 |
} |
|
6520
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
161 |
else if (errno == EAGAIN) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
162 |
return IOS_UNAVAILABLE; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
163 |
else if (errno == EINTR) |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
164 |
return IOS_INTERRUPTED; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
165 |
else { |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
166 |
const char *msg = reading ? "Read failed" : "Write failed"; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
167 |
JNU_ThrowIOExceptionWithLastError(env, msg); |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
168 |
return IOS_THROWN; |
0e7cf575332e
6981145: (se) Eliminate JNI*Critical when creating pipes and other cleanups
martin
parents:
5506
diff
changeset
|
169 |
} |
2 | 170 |
} |
171 |
||
172 |
jint |
|
173 |
fdval(JNIEnv *env, jobject fdo) |
|
174 |
{ |
|
175 |
return (*env)->GetIntField(env, fdo, fd_fdID); |
|
176 |
} |