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);
|
|
70 |
|
|
71 |
if ((blocking == JNI_FALSE) && !(flags & O_NONBLOCK))
|
|
72 |
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
73 |
else if ((blocking == JNI_TRUE) && (flags & O_NONBLOCK))
|
|
74 |
return fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
|
|
75 |
return 0;
|
|
76 |
}
|
|
77 |
|
|
78 |
JNIEXPORT void JNICALL
|
|
79 |
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
|
|
80 |
jobject fdo, jboolean blocking)
|
|
81 |
{
|
|
82 |
if (configureBlocking(fdval(env, fdo), blocking) < 0)
|
|
83 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
|
|
84 |
}
|
|
85 |
|
|
86 |
JNIEXPORT void JNICALL
|
|
87 |
Java_sun_nio_ch_IOUtil_initPipe(JNIEnv *env, jobject this,
|
|
88 |
jintArray intArray, jboolean block)
|
|
89 |
{
|
|
90 |
int fd[2];
|
|
91 |
jint *ptr = 0;
|
|
92 |
|
|
93 |
if (pipe(fd) < 0) {
|
|
94 |
JNU_ThrowIOExceptionWithLastError(env, "Pipe failed");
|
|
95 |
return;
|
|
96 |
}
|
|
97 |
if (block == JNI_FALSE) {
|
|
98 |
if ((configureBlocking(fd[0], JNI_FALSE) < 0)
|
|
99 |
|| (configureBlocking(fd[1], JNI_FALSE) < 0)) {
|
|
100 |
JNU_ThrowIOExceptionWithLastError(env, "Configure blocking failed");
|
|
101 |
}
|
|
102 |
}
|
|
103 |
ptr = (*env)->GetPrimitiveArrayCritical(env, intArray, 0);
|
|
104 |
ptr[0] = fd[0];
|
|
105 |
ptr[1] = fd[1];
|
|
106 |
(*env)->ReleasePrimitiveArrayCritical(env, intArray, ptr, 0);
|
|
107 |
}
|
|
108 |
|
|
109 |
JNIEXPORT jboolean JNICALL
|
|
110 |
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
|
|
111 |
{
|
|
112 |
char buf[128];
|
|
113 |
int tn = 0;
|
|
114 |
|
|
115 |
for (;;) {
|
|
116 |
int n = read(fd, buf, sizeof(buf));
|
|
117 |
tn += n;
|
|
118 |
if ((n < 0) && (errno != EAGAIN))
|
|
119 |
JNU_ThrowIOExceptionWithLastError(env, "Drain");
|
|
120 |
if (n == (int)sizeof(buf))
|
|
121 |
continue;
|
|
122 |
return (tn > 0) ? JNI_TRUE : JNI_FALSE;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
|
|
127 |
/* Declared in nio_util.h for use elsewhere in NIO */
|
|
128 |
|
|
129 |
jint
|
|
130 |
convertReturnVal(JNIEnv *env, jint n, jboolean reading)
|
|
131 |
{
|
|
132 |
if (n > 0) /* Number of bytes written */
|
|
133 |
return n;
|
|
134 |
if (n < 0) {
|
|
135 |
if (errno == EAGAIN)
|
|
136 |
return IOS_UNAVAILABLE;
|
|
137 |
if (errno == EINTR)
|
|
138 |
return IOS_INTERRUPTED;
|
|
139 |
}
|
|
140 |
if (n == 0) {
|
|
141 |
if (reading) {
|
|
142 |
return IOS_EOF; /* EOF is -1 in javaland */
|
|
143 |
} else {
|
|
144 |
return 0;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
|
|
148 |
return IOS_THROWN;
|
|
149 |
}
|
|
150 |
|
|
151 |
/* Declared in nio_util.h for use elsewhere in NIO */
|
|
152 |
|
|
153 |
jlong
|
|
154 |
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
|
|
155 |
{
|
|
156 |
if (n > 0) /* Number of bytes written */
|
|
157 |
return n;
|
|
158 |
if (n < 0) {
|
|
159 |
if (errno == EAGAIN)
|
|
160 |
return IOS_UNAVAILABLE;
|
|
161 |
if (errno == EINTR)
|
|
162 |
return IOS_INTERRUPTED;
|
|
163 |
}
|
|
164 |
if (n == 0) {
|
|
165 |
if (reading) {
|
|
166 |
return IOS_EOF; /* EOF is -1 in javaland */
|
|
167 |
} else {
|
|
168 |
return 0;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
|
|
172 |
return IOS_THROWN;
|
|
173 |
}
|
|
174 |
|
|
175 |
jint
|
|
176 |
fdval(JNIEnv *env, jobject fdo)
|
|
177 |
{
|
|
178 |
return (*env)->GetIntField(env, fdo, fd_fdID);
|
|
179 |
}
|