Skip to content

Commit d667895

Browse files
author
Brian Burkhalter
committed
8294399: (ch) Refactor some methods out of sun.nio.ch.UnixFileDispatcherImpl
Reviewed-by: alanb
1 parent 628820f commit d667895

File tree

9 files changed

+171
-73
lines changed

9 files changed

+171
-73
lines changed

src/java.base/linux/classes/sun/nio/ch/FileDispatcherImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ static native long transferFrom0(FileDescriptor src, FileDescriptor dst,
5858
static native void init0();
5959

6060
static {
61+
IOUtil.load();
6162
init0();
6263
}
6364
}

src/java.base/unix/classes/sun/nio/ch/DatagramDispatcher.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@
3333
* for read and write operations.
3434
*/
3535

36-
class DatagramDispatcher extends NativeDispatcher {
36+
class DatagramDispatcher extends UnixDispatcher {
3737

3838
static {
3939
IOUtil.load();
@@ -56,15 +56,15 @@ long writev(FileDescriptor fd, long address, int len) throws IOException {
5656
}
5757

5858
void close(FileDescriptor fd) throws IOException {
59-
FileDispatcherImpl.close0(fd);
59+
close0(fd);
6060
}
6161

6262
void preClose(FileDescriptor fd) throws IOException {
63-
FileDispatcherImpl.preClose0(fd);
63+
preClose0(fd);
6464
}
6565

6666
void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
67-
FileDispatcherImpl.dup0(fd1, fd2);
67+
dup0(fd1, fd2);
6868
}
6969

7070
static native int read0(FileDescriptor fd, long address, int len)
@@ -78,4 +78,7 @@ static native int write0(FileDescriptor fd, long address, int len)
7878

7979
static native long writev0(FileDescriptor fd, long address, int len)
8080
throws IOException;
81+
82+
static native void dup0(FileDescriptor fd1, FileDescriptor fd2)
83+
throws IOException;
8184
}

src/java.base/unix/classes/sun/nio/ch/SocketDispatcher.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -33,7 +33,7 @@
3333
* for read and write operations.
3434
*/
3535

36-
class SocketDispatcher extends NativeDispatcher {
36+
class SocketDispatcher extends UnixDispatcher {
3737
SocketDispatcher() { }
3838

3939
/**
@@ -59,19 +59,19 @@ long readv(FileDescriptor fd, long address, int len) throws IOException {
5959
}
6060

6161
int write(FileDescriptor fd, long address, int len) throws IOException {
62-
return FileDispatcherImpl.write0(fd, address, len);
62+
return write0(fd, address, len);
6363
}
6464

6565
long writev(FileDescriptor fd, long address, int len) throws IOException {
66-
return FileDispatcherImpl.writev0(fd, address, len);
66+
return writev0(fd, address, len);
6767
}
6868

6969
void close(FileDescriptor fd) throws IOException {
70-
FileDispatcherImpl.close0(fd);
70+
close0(fd);
7171
}
7272

7373
void preClose(FileDescriptor fd) throws IOException {
74-
FileDispatcherImpl.preClose0(fd);
74+
preClose0(fd);
7575
}
7676

7777
// -- Native methods --
@@ -82,6 +82,12 @@ private static native int read0(FileDescriptor fd, long address, int len)
8282
private static native long readv0(FileDescriptor fd, long address, int len)
8383
throws IOException;
8484

85+
static native int write0(FileDescriptor fd, long address, int len)
86+
throws IOException;
87+
88+
static native long writev0(FileDescriptor fd, long address, int len)
89+
throws IOException;
90+
8591
static {
8692
IOUtil.load();
8793
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
*
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.
24+
*/
25+
26+
package sun.nio.ch;
27+
28+
import java.io.FileDescriptor;
29+
import java.io.IOException;
30+
31+
abstract class UnixDispatcher extends NativeDispatcher {
32+
33+
void close(FileDescriptor fd) throws IOException {
34+
close0(fd);
35+
}
36+
37+
void preClose(FileDescriptor fd) throws IOException {
38+
preClose0(fd);
39+
}
40+
41+
static native void close0(FileDescriptor fd) throws IOException;
42+
43+
static native void preClose0(FileDescriptor fd) throws IOException;
44+
45+
static native void init();
46+
47+
static {
48+
IOUtil.load();
49+
init();
50+
}
51+
}

src/java.base/unix/classes/sun/nio/ch/UnixFileDispatcherImpl.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class UnixFileDispatcherImpl extends FileDispatcher {
3939

4040
static {
4141
IOUtil.load();
42-
init();
4342
}
4443

4544
private static final JavaIOFileDescriptorAccess fdAccess =
@@ -108,14 +107,6 @@ void close(FileDescriptor fd) throws IOException {
108107
fdAccess.close(fd);
109108
}
110109

111-
void preClose(FileDescriptor fd) throws IOException {
112-
preClose0(fd);
113-
}
114-
115-
void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
116-
dup0(fd1, fd2);
117-
}
118-
119110
FileDescriptor duplicateForMapping(FileDescriptor fd) {
120111
// file descriptor not required for mapping operations; okay
121112
// to return invalid file descriptor.
@@ -211,14 +202,6 @@ static native int lock0(FileDescriptor fd, boolean blocking, long pos,
211202
static native void release0(FileDescriptor fd, long pos, long size)
212203
throws IOException;
213204

214-
// Shared with SocketDispatcher and DatagramDispatcher but
215-
// NOT used by FileDispatcherImpl
216-
static native void close0(FileDescriptor fd) throws IOException;
217-
218-
static native void preClose0(FileDescriptor fd) throws IOException;
219-
220-
static native void dup0(FileDescriptor fd1, FileDescriptor fd2) throws IOException;
221-
222205
static native void closeIntFD(int fd) throws IOException;
223206

224207
static native long allocationGranularity0();
@@ -230,6 +213,4 @@ static native long map0(FileDescriptor fd, int prot, long position,
230213
static native int unmap0(long address, long length);
231214

232215
static native int setDirect0(FileDescriptor fd) throws IOException;
233-
234-
static native void init();
235216
}

src/java.base/unix/native/libnio/ch/DatagramDispatcher.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -115,3 +115,12 @@ Java_sun_nio_ch_DatagramDispatcher_writev0(JNIEnv *env, jclass clazz,
115115
}
116116
return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
117117
}
118+
119+
JNIEXPORT void JNICALL
120+
Java_sun_nio_ch_DatagramDispatcher_dup0(JNIEnv* env, jclass clazz,
121+
jobject fdo1, jobject fdo2)
122+
{
123+
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
124+
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
125+
}
126+
}

src/java.base/unix/native/libnio/ch/SocketDispatcher.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -63,3 +63,22 @@
6363
return convertLongReturnVal(env, n, JNI_TRUE);
6464
}
6565
}
66+
67+
JNIEXPORT jint JNICALL
68+
Java_sun_nio_ch_SocketDispatcher_write0(JNIEnv *env, jclass clazz,
69+
jobject fdo, jlong address, jint len)
70+
{
71+
jint fd = fdval(env, fdo);
72+
void *buf = (void *)jlong_to_ptr(address);
73+
74+
return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
75+
}
76+
77+
JNIEXPORT jlong JNICALL
78+
Java_sun_nio_ch_SocketDispatcher_writev0(JNIEnv *env, jclass clazz,
79+
jobject fdo, jlong address, jint len)
80+
{
81+
jint fd = fdval(env, fdo);
82+
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
83+
return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
84+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
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
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
*
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.
24+
*/
25+
26+
#include "nio.h"
27+
#include "nio_util.h"
28+
29+
#include "sun_nio_ch_UnixDispatcher.h"
30+
31+
static int preCloseFD = -1; /* File descriptor to which we dup other fd's
32+
before closing them for real */
33+
34+
static void closeFileDescriptor(JNIEnv *env, int fd) {
35+
if (fd != -1) {
36+
int result = close(fd);
37+
if (result < 0)
38+
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
39+
}
40+
}
41+
42+
JNIEXPORT void JNICALL
43+
Java_sun_nio_ch_UnixDispatcher_init(JNIEnv *env, jclass clazz)
44+
{
45+
int sp[2];
46+
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
47+
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
48+
return;
49+
}
50+
preCloseFD = sp[0];
51+
close(sp[1]);
52+
}
53+
54+
JNIEXPORT void JNICALL
55+
Java_sun_nio_ch_UnixDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo)
56+
{
57+
jint fd = fdval(env, fdo);
58+
closeFileDescriptor(env, fd);
59+
}
60+
61+
JNIEXPORT void JNICALL
62+
Java_sun_nio_ch_UnixDispatcher_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
63+
{
64+
jint fd = fdval(env, fdo);
65+
if (preCloseFD >= 0) {
66+
if (dup2(preCloseFD, fd) < 0)
67+
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
68+
}
69+
}

src/java.base/unix/native/libnio/ch/UnixFileDispatcherImpl.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,6 @@
5252
#include "java_lang_Long.h"
5353
#include <assert.h>
5454

55-
static int preCloseFD = -1; /* File descriptor to which we dup other fd's
56-
before closing them for real */
57-
58-
JNIEXPORT void JNICALL
59-
Java_sun_nio_ch_UnixFileDispatcherImpl_init(JNIEnv *env, jclass cl)
60-
{
61-
int sp[2];
62-
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
63-
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
64-
return;
65-
}
66-
preCloseFD = sp[0];
67-
close(sp[1]);
68-
}
69-
7055
JNIEXPORT jint JNICALL
7156
Java_sun_nio_ch_UnixFileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
7257
jobject fdo, jlong address, jint len)
@@ -257,7 +242,6 @@ Java_sun_nio_ch_UnixFileDispatcherImpl_release0(JNIEnv *env, jobject this,
257242
}
258243
}
259244

260-
261245
static void closeFileDescriptor(JNIEnv *env, int fd) {
262246
if (fd != -1) {
263247
int result = close(fd);
@@ -266,31 +250,6 @@ static void closeFileDescriptor(JNIEnv *env, int fd) {
266250
}
267251
}
268252

269-
JNIEXPORT void JNICALL
270-
Java_sun_nio_ch_UnixFileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
271-
{
272-
jint fd = fdval(env, fdo);
273-
closeFileDescriptor(env, fd);
274-
}
275-
276-
JNIEXPORT void JNICALL
277-
Java_sun_nio_ch_UnixFileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
278-
{
279-
jint fd = fdval(env, fdo);
280-
if (preCloseFD >= 0) {
281-
if (dup2(preCloseFD, fd) < 0)
282-
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
283-
}
284-
}
285-
286-
JNIEXPORT void JNICALL
287-
Java_sun_nio_ch_UnixFileDispatcherImpl_dup0(JNIEnv *env, jobject this, jobject fdo1, jobject fdo2)
288-
{
289-
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
290-
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
291-
}
292-
}
293-
294253
JNIEXPORT void JNICALL
295254
Java_sun_nio_ch_UnixFileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
296255
{

0 commit comments

Comments
 (0)