8294399: (ch) Refactor some methods out of sun.nio.ch.UnixFileDispatcherImpl

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2022-10-27 15:35:24 +00:00
parent 628820f47e
commit d6678952a6
9 changed files with 171 additions and 73 deletions

View file

@ -58,6 +58,7 @@ class FileDispatcherImpl extends UnixFileDispatcherImpl {
static native void init0();
static {
IOUtil.load();
init0();
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,7 +33,7 @@ import java.io.IOException;
* for read and write operations.
*/
class DatagramDispatcher extends NativeDispatcher {
class DatagramDispatcher extends UnixDispatcher {
static {
IOUtil.load();
@ -56,15 +56,15 @@ class DatagramDispatcher extends NativeDispatcher {
}
void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
close0(fd);
}
void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
preClose0(fd);
}
void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
FileDispatcherImpl.dup0(fd1, fd2);
dup0(fd1, fd2);
}
static native int read0(FileDescriptor fd, long address, int len)
@ -78,4 +78,7 @@ class DatagramDispatcher extends NativeDispatcher {
static native long writev0(FileDescriptor fd, long address, int len)
throws IOException;
static native void dup0(FileDescriptor fd1, FileDescriptor fd2)
throws IOException;
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,7 +33,7 @@ import java.io.IOException;
* for read and write operations.
*/
class SocketDispatcher extends NativeDispatcher {
class SocketDispatcher extends UnixDispatcher {
SocketDispatcher() { }
/**
@ -59,19 +59,19 @@ class SocketDispatcher extends NativeDispatcher {
}
int write(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.write0(fd, address, len);
return write0(fd, address, len);
}
long writev(FileDescriptor fd, long address, int len) throws IOException {
return FileDispatcherImpl.writev0(fd, address, len);
return writev0(fd, address, len);
}
void close(FileDescriptor fd) throws IOException {
FileDispatcherImpl.close0(fd);
close0(fd);
}
void preClose(FileDescriptor fd) throws IOException {
FileDispatcherImpl.preClose0(fd);
preClose0(fd);
}
// -- Native methods --
@ -82,6 +82,12 @@ class SocketDispatcher extends NativeDispatcher {
private static native long readv0(FileDescriptor fd, long address, int len)
throws IOException;
static native int write0(FileDescriptor fd, long address, int len)
throws IOException;
static native long writev0(FileDescriptor fd, long address, int len)
throws IOException;
static {
IOUtil.load();
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
abstract class UnixDispatcher extends NativeDispatcher {
void close(FileDescriptor fd) throws IOException {
close0(fd);
}
void preClose(FileDescriptor fd) throws IOException {
preClose0(fd);
}
static native void close0(FileDescriptor fd) throws IOException;
static native void preClose0(FileDescriptor fd) throws IOException;
static native void init();
static {
IOUtil.load();
init();
}
}

View file

@ -39,7 +39,6 @@ class UnixFileDispatcherImpl extends FileDispatcher {
static {
IOUtil.load();
init();
}
private static final JavaIOFileDescriptorAccess fdAccess =
@ -108,14 +107,6 @@ class UnixFileDispatcherImpl extends FileDispatcher {
fdAccess.close(fd);
}
void preClose(FileDescriptor fd) throws IOException {
preClose0(fd);
}
void dup(FileDescriptor fd1, FileDescriptor fd2) throws IOException {
dup0(fd1, fd2);
}
FileDescriptor duplicateForMapping(FileDescriptor fd) {
// file descriptor not required for mapping operations; okay
// to return invalid file descriptor.
@ -211,14 +202,6 @@ class UnixFileDispatcherImpl extends FileDispatcher {
static native void release0(FileDescriptor fd, long pos, long size)
throws IOException;
// Shared with SocketDispatcher and DatagramDispatcher but
// NOT used by FileDispatcherImpl
static native void close0(FileDescriptor fd) throws IOException;
static native void preClose0(FileDescriptor fd) throws IOException;
static native void dup0(FileDescriptor fd1, FileDescriptor fd2) throws IOException;
static native void closeIntFD(int fd) throws IOException;
static native long allocationGranularity0();
@ -230,6 +213,4 @@ class UnixFileDispatcherImpl extends FileDispatcher {
static native int unmap0(long address, long length);
static native int setDirect0(FileDescriptor fd) throws IOException;
static native void init();
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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,
}
return convertLongReturnVal(env, (jlong)result, JNI_FALSE);
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_DatagramDispatcher_dup0(JNIEnv* env, jclass clazz,
jobject fdo1, jobject fdo2)
{
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -63,3 +63,22 @@
return convertLongReturnVal(env, n, JNI_TRUE);
}
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_SocketDispatcher_write0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
void *buf = (void *)jlong_to_ptr(address);
return convertReturnVal(env, write(fd, buf, len), JNI_FALSE);
}
JNIEXPORT jlong JNICALL
Java_sun_nio_ch_SocketDispatcher_writev0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
{
jint fd = fdval(env, fdo);
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
return convertLongReturnVal(env, writev(fd, iov, len), JNI_FALSE);
}

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
#include "nio.h"
#include "nio_util.h"
#include "sun_nio_ch_UnixDispatcher.h"
static int preCloseFD = -1; /* File descriptor to which we dup other fd's
before closing them for real */
static void closeFileDescriptor(JNIEnv *env, int fd) {
if (fd != -1) {
int result = close(fd);
if (result < 0)
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
}
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_init(JNIEnv *env, jclass clazz)
{
int sp[2];
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
return;
}
preCloseFD = sp[0];
close(sp[1]);
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
closeFileDescriptor(env, fd);
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixDispatcher_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
if (preCloseFD >= 0) {
if (dup2(preCloseFD, fd) < 0)
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}

View file

@ -52,21 +52,6 @@
#include "java_lang_Long.h"
#include <assert.h>
static int preCloseFD = -1; /* File descriptor to which we dup other fd's
before closing them for real */
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_init(JNIEnv *env, jclass cl)
{
int sp[2];
if (socketpair(PF_UNIX, SOCK_STREAM, 0, sp) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "socketpair failed");
return;
}
preCloseFD = sp[0];
close(sp[1]);
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_read0(JNIEnv *env, jclass clazz,
jobject fdo, jlong address, jint len)
@ -257,7 +242,6 @@ Java_sun_nio_ch_UnixFileDispatcherImpl_release0(JNIEnv *env, jobject this,
}
}
static void closeFileDescriptor(JNIEnv *env, int fd) {
if (fd != -1) {
int result = close(fd);
@ -266,31 +250,6 @@ static void closeFileDescriptor(JNIEnv *env, int fd) {
}
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
closeFileDescriptor(env, fd);
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_preClose0(JNIEnv *env, jclass clazz, jobject fdo)
{
jint fd = fdval(env, fdo);
if (preCloseFD >= 0) {
if (dup2(preCloseFD, fd) < 0)
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_dup0(JNIEnv *env, jobject this, jobject fdo1, jobject fdo2)
{
if (dup2(fdval(env, fdo1), fdval(env, fdo2)) < 0) {
JNU_ThrowIOExceptionWithLastError(env, "dup2 failed");
}
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_UnixFileDispatcherImpl_closeIntFD(JNIEnv *env, jclass clazz, jint fd)
{