mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-21 19:44:41 +02:00
8187631: Refactor FileDescriptor close implementation
Reviewed-by: bpb, alanb
This commit is contained in:
parent
8088bd6350
commit
5e55e5e2ee
20 changed files with 184 additions and 305 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2017, 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
|
||||
|
@ -32,12 +32,12 @@ import jdk.internal.misc.SharedSecrets;
|
|||
|
||||
/**
|
||||
* Instances of the file descriptor class serve as an opaque handle
|
||||
* to the underlying machine-specific structure representing an
|
||||
* open file, an open socket, or another source or sink of bytes.
|
||||
* to the underlying machine-specific structure representing an open
|
||||
* file, an open socket, or another source or sink of bytes.
|
||||
* The main practical use for a file descriptor is to create a
|
||||
* {@link FileInputStream} or {@link FileOutputStream} to contain it.
|
||||
*
|
||||
* <p>Applications should not create their own file descriptors.
|
||||
* <p>
|
||||
* Applications should not create their own file descriptors.
|
||||
*
|
||||
* @author Pavani Diwanji
|
||||
* @since 1.0
|
||||
|
@ -57,15 +57,6 @@ public final class FileDescriptor {
|
|||
*/
|
||||
private boolean append;
|
||||
|
||||
/**
|
||||
* Constructs an (invalid) FileDescriptor
|
||||
* object.
|
||||
*/
|
||||
public /**/ FileDescriptor() {
|
||||
fd = -1;
|
||||
handle = -1;
|
||||
}
|
||||
|
||||
static {
|
||||
initIDs();
|
||||
}
|
||||
|
@ -73,34 +64,47 @@ public final class FileDescriptor {
|
|||
// Set up JavaIOFileDescriptorAccess in SharedSecrets
|
||||
static {
|
||||
SharedSecrets.setJavaIOFileDescriptorAccess(
|
||||
new JavaIOFileDescriptorAccess() {
|
||||
public void set(FileDescriptor obj, int fd) {
|
||||
obj.fd = fd;
|
||||
}
|
||||
new JavaIOFileDescriptorAccess() {
|
||||
public void set(FileDescriptor fdo, int fd) {
|
||||
fdo.fd = fd;
|
||||
}
|
||||
|
||||
public int get(FileDescriptor obj) {
|
||||
return obj.fd;
|
||||
}
|
||||
public int get(FileDescriptor fdo) {
|
||||
return fdo.fd;
|
||||
}
|
||||
|
||||
public void setAppend(FileDescriptor obj, boolean append) {
|
||||
obj.append = append;
|
||||
}
|
||||
public void setAppend(FileDescriptor fdo, boolean append) {
|
||||
fdo.append = append;
|
||||
}
|
||||
|
||||
public boolean getAppend(FileDescriptor obj) {
|
||||
return obj.append;
|
||||
}
|
||||
public boolean getAppend(FileDescriptor fdo) {
|
||||
return fdo.append;
|
||||
}
|
||||
|
||||
public void setHandle(FileDescriptor obj, long handle) {
|
||||
obj.handle = handle;
|
||||
}
|
||||
public void close(FileDescriptor fdo) {
|
||||
fdo.close();
|
||||
}
|
||||
|
||||
public long getHandle(FileDescriptor obj) {
|
||||
return obj.handle;
|
||||
public void setHandle(FileDescriptor fdo, long handle) {
|
||||
fdo.handle = handle;
|
||||
}
|
||||
|
||||
public long getHandle(FileDescriptor fdo) {
|
||||
return fdo.handle;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an (invalid) FileDescriptor
|
||||
* object.
|
||||
*/
|
||||
public FileDescriptor() {
|
||||
fd = -1;
|
||||
handle = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* A handle to the standard input stream. Usually, this file
|
||||
* descriptor is not used directly, but rather via the input stream
|
||||
|
@ -135,7 +139,7 @@ public final class FileDescriptor {
|
|||
* {@code false} otherwise.
|
||||
*/
|
||||
public boolean valid() {
|
||||
return ((handle != -1) || (fd != -1));
|
||||
return (handle != -1) || (fd != -1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -179,6 +183,13 @@ public final class FileDescriptor {
|
|||
return desc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the raw file descriptor or handle, if it has not already been closed
|
||||
* and set the fd and handle to -1.
|
||||
* Package private to allow it to be used in java.io.
|
||||
*/
|
||||
native void close();
|
||||
|
||||
/*
|
||||
* Package private methods to track referents.
|
||||
* If multiple streams point to the same FileDescriptor, we cycle
|
||||
|
|
|
@ -104,7 +104,7 @@ class FileDispatcherImpl extends FileDispatcher {
|
|||
}
|
||||
|
||||
void close(FileDescriptor fd) throws IOException {
|
||||
close0(fd);
|
||||
fdAccess.close(fd);
|
||||
}
|
||||
|
||||
FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2017, 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
|
||||
|
@ -72,3 +72,9 @@ Java_java_io_FileDescriptor_sync(JNIEnv *env, jobject this) {
|
|||
JNU_ThrowByName(env, "java/io/SyncFailedException", "sync failed");
|
||||
}
|
||||
}
|
||||
|
||||
// instance method close0 for FileDescriptor
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_FileDescriptor_close(JNIEnv *env, jobject this) {
|
||||
fileDescriptorClose(env, this);
|
||||
}
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2004, 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 "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jvm.h"
|
||||
|
||||
#include "io_util.h"
|
||||
#include "io_util_md.h"
|
||||
|
||||
#include "java_io_FileInputStream.h"
|
||||
|
||||
extern jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
|
||||
|
||||
/*********************************************************************
|
||||
* Platform specific implementation of input stream native methods
|
||||
*/
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_FileInputStream_close0(JNIEnv *env, jobject this) {
|
||||
handleClose(env, this, fis_fd);
|
||||
}
|
|
@ -1,44 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2004, 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 "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jvm.h"
|
||||
|
||||
#include "io_util.h"
|
||||
#include "io_util_md.h"
|
||||
|
||||
#include "java_io_RandomAccessFile.h"
|
||||
|
||||
extern jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
|
||||
|
||||
/*********************************************************************
|
||||
* Platform specific implementation of input stream native methods
|
||||
*/
|
||||
|
||||
JNIEXPORT void JNICALL
|
||||
Java_java_io_RandomAccessFile_close0(JNIEnv *env, jobject this) {
|
||||
handleClose(env, this, raf_fd);
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 2017, 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
|
||||
|
@ -535,14 +535,28 @@ jint handleAppend(FD fd, const void *buf, jint len) {
|
|||
return writeInternal(fd, buf, len, JNI_TRUE);
|
||||
}
|
||||
|
||||
jint
|
||||
void
|
||||
handleClose(JNIEnv *env, jobject this, jfieldID fid)
|
||||
{
|
||||
FD fd = GET_FD(this, fid);
|
||||
jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
|
||||
if (fileDescriptor == NULL) {
|
||||
return;
|
||||
}
|
||||
fileDescriptorClose(env, fileDescriptor);
|
||||
}
|
||||
|
||||
// Function to close the fd held by this FileDescriptor and set fd to -1.
|
||||
void
|
||||
fileDescriptorClose(JNIEnv *env, jobject this)
|
||||
{
|
||||
FD fd = (*env)->GetLongField(env, this, IO_handle_fdID);
|
||||
if ((*env)->ExceptionOccurred(env)) {
|
||||
return;
|
||||
}
|
||||
HANDLE h = (HANDLE)fd;
|
||||
|
||||
if (h == INVALID_HANDLE_VALUE) {
|
||||
return 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Set the fd to -1 before closing it so that the timing window
|
||||
|
@ -551,12 +565,14 @@ handleClose(JNIEnv *env, jobject this, jfieldID fid)
|
|||
* Practically the chance of its occurance is low, however, we are
|
||||
* taking extra precaution over here.
|
||||
*/
|
||||
SET_FD(this, -1, fid);
|
||||
(*env)->SetLongField(env, this, IO_handle_fdID, -1);
|
||||
if ((*env)->ExceptionOccurred(env)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (CloseHandle(h) == 0) { /* Returns zero on failure */
|
||||
JNU_ThrowIOExceptionWithLastError(env, "close failed");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
jlong
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2003, 2017, 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
|
||||
|
@ -48,7 +48,8 @@ jlong handleGetLength(FD fd);
|
|||
JNIEXPORT jint handleRead(FD fd, void *buf, jint len);
|
||||
jint handleWrite(FD fd, const void *buf, jint len);
|
||||
jint handleAppend(FD fd, const void *buf, jint len);
|
||||
jint handleClose(JNIEnv *env, jobject this, jfieldID fid);
|
||||
void handleClose(JNIEnv *env, jobject this, jfieldID fid);
|
||||
void fileDescriptorClose(JNIEnv *env, jobject this);
|
||||
jlong handleLseek(FD fd, jlong offset, jint whence);
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue