8187631: Refactor FileDescriptor close implementation

Reviewed-by: bpb, alanb
This commit is contained in:
Roger Riggs 2017-09-21 11:41:12 -04:00
parent 8088bd6350
commit 5e55e5e2ee
20 changed files with 184 additions and 305 deletions

View File

@ -1,5 +1,5 @@
#
# Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 1997, 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
@ -74,17 +74,16 @@ SUNWprivate_1.1 {
JNU_ThrowStringIndexOutOfBoundsException;
JNU_ToString;
Java_java_io_FileDescriptor_close;
Java_java_io_FileDescriptor_initIDs;
Java_java_io_FileDescriptor_sync;
Java_java_io_FileDescriptor_getAppend;
Java_java_io_FileInputStream_available0;
Java_java_io_FileInputStream_close0;
Java_java_io_FileInputStream_initIDs;
Java_java_io_FileInputStream_open0;
Java_java_io_FileInputStream_read0;
Java_java_io_FileInputStream_readBytes;
Java_java_io_FileInputStream_skip0;
Java_java_io_FileOutputStream_close0;
Java_java_io_FileOutputStream_initIDs;
Java_java_io_FileOutputStream_open0;
Java_java_io_FileOutputStream_write;
@ -95,7 +94,6 @@ SUNWprivate_1.1 {
Java_java_io_ObjectOutputStream_floatsToBytes;
Java_java_io_ObjectStreamClass_hasStaticInitializer;
Java_java_io_ObjectStreamClass_initNative;
Java_java_io_RandomAccessFile_close0;
Java_java_io_RandomAccessFile_getFilePointer;
Java_java_io_RandomAccessFile_initIDs;
Java_java_io_RandomAccessFile_length;

View File

@ -341,7 +341,7 @@ class FileInputStream extends InputStream
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
fd.close();
}
});
}
@ -403,7 +403,6 @@ class FileInputStream extends InputStream
private static native void initIDs();
private native void close0() throws IOException;
static {
initIDs();

View File

@ -365,7 +365,7 @@ class FileOutputStream extends OutputStream
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
fd.close();
}
});
}
@ -458,8 +458,6 @@ class FileOutputStream extends OutputStream
}
}
private native void close0() throws IOException;
private static native void initIDs();
static {

View File

@ -648,7 +648,7 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
fd.closeAll(new Closeable() {
public void close() throws IOException {
close0();
fd.close();
}
});
}
@ -1177,8 +1177,6 @@ public class RandomAccessFile implements DataOutput, DataInput, Closeable {
private static native void initIDs();
private native void close0() throws IOException;
static {
initIDs();
SharedSecrets.setJavaIORandomAccessFileAccess(new JavaIORandomAccessFileAccess()

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 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
@ -31,12 +31,13 @@ import java.io.FileDescriptor;
*/
public interface JavaIOFileDescriptorAccess {
public void set(FileDescriptor obj, int fd);
public int get(FileDescriptor fd);
public void setAppend(FileDescriptor obj, boolean append);
public boolean getAppend(FileDescriptor obj);
public void set(FileDescriptor fdo, int fd);
public int get(FileDescriptor fdo);
public void setAppend(FileDescriptor fdo, boolean append);
public boolean getAppend(FileDescriptor fdo);
public void close(FileDescriptor fdo);
// Only valid on Windows
public void setHandle(FileDescriptor obj, long handle);
public long getHandle(FileDescriptor obj);
public void setHandle(FileDescriptor fdo, long handle);
public long getHandle(FileDescriptor fdo);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -144,7 +144,7 @@ public class FileChannelImpl
//
((java.io.Closeable)parent).close();
} else {
nd.close(fd);
fdAccess.close(fd);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 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
@ -33,16 +33,13 @@ 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. The
* main practical use for a file descriptor is to create a
* <code>FileInputStream</code> or <code>FileOutputStream</code> to
* contain it.
* 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.
*
* @author Pavani Diwanji
* @see java.io.FileInputStream
* @see java.io.FileOutputStream
* @since 1.0
*/
public final class FileDescriptor {
@ -58,6 +55,45 @@ public final class FileDescriptor {
*/
private boolean append;
static {
initIDs();
}
// Set up JavaIOFileDescriptorAccess in SharedSecrets
static {
SharedSecrets.setJavaIOFileDescriptorAccess(
new JavaIOFileDescriptorAccess() {
public void set(FileDescriptor fdo, int fd) {
fdo.fd = fd;
}
public int get(FileDescriptor fdo) {
return fdo.fd;
}
public void setAppend(FileDescriptor fdo, boolean append) {
fdo.append = append;
}
public boolean getAppend(FileDescriptor fdo) {
return fdo.append;
}
public void close(FileDescriptor fdo) {
fdo.close();
}
public void setHandle(FileDescriptor fdo, long handle) {
throw new UnsupportedOperationException();
}
public long getHandle(FileDescriptor fdo) {
throw new UnsupportedOperationException();
}
}
);
}
/**
* Constructs an (invalid) FileDescriptor
* object.
@ -74,7 +110,7 @@ public final class FileDescriptor {
/**
* A handle to the standard input stream. Usually, this file
* descriptor is not used directly, but rather via the input stream
* known as <code>System.in</code>.
* known as {@code System.in}.
*
* @see java.lang.System#in
*/
@ -83,7 +119,7 @@ public final class FileDescriptor {
/**
* A handle to the standard output stream. Usually, this file
* descriptor is not used directly, but rather via the output stream
* known as <code>System.out</code>.
* known as {@code System.out}.
* @see java.lang.System#out
*/
public static final FileDescriptor out = new FileDescriptor(1);
@ -91,7 +127,7 @@ public final class FileDescriptor {
/**
* A handle to the standard error stream. Usually, this file
* descriptor is not used directly, but rather via the output stream
* known as <code>System.err</code>.
* known as {@code System.err}.
*
* @see java.lang.System#err
*/
@ -100,9 +136,9 @@ public final class FileDescriptor {
/**
* Tests if this file descriptor object is valid.
*
* @return <code>true</code> if the file descriptor object represents a
* @return {@code true} if the file descriptor object represents a
* valid, open file, socket, or other active I/O connection;
* <code>false</code> otherwise.
* {@code false} otherwise.
*/
public boolean valid() {
return fd != -1;
@ -141,46 +177,18 @@ public final class FileDescriptor {
/* This routine initializes JNI field offsets for the class */
private static native void initIDs();
static {
initIDs();
}
// Set up JavaIOFileDescriptorAccess in SharedSecrets
static {
SharedSecrets.setJavaIOFileDescriptorAccess(
new JavaIOFileDescriptorAccess() {
public void set(FileDescriptor obj, int fd) {
obj.fd = fd;
}
public int get(FileDescriptor obj) {
return obj.fd;
}
public void setAppend(FileDescriptor obj, boolean append) {
obj.append = append;
}
public boolean getAppend(FileDescriptor obj) {
return obj.append;
}
public void setHandle(FileDescriptor obj, long handle) {
throw new UnsupportedOperationException();
}
public long getHandle(FileDescriptor obj) {
throw new UnsupportedOperationException();
}
}
);
}
/**
* Returns true, if the file was opened for appending.
*/
private static native boolean getAppend(int fd);
/**
* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 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
@ -28,6 +28,9 @@ package sun.nio.ch;
import java.io.FileDescriptor;
import java.io.IOException;
import jdk.internal.misc.JavaIOFileDescriptorAccess;
import jdk.internal.misc.SharedSecrets;
class FileDispatcherImpl extends FileDispatcher {
static {
@ -35,6 +38,9 @@ class FileDispatcherImpl extends FileDispatcher {
init();
}
private static final JavaIOFileDescriptorAccess fdAccess =
SharedSecrets.getJavaIOFileDescriptorAccess();
FileDispatcherImpl() {
}
@ -95,7 +101,7 @@ class FileDispatcherImpl extends FileDispatcher {
}
void close(FileDescriptor fd) throws IOException {
close0(fd);
fdAccess.close(fd);
}
void preClose(FileDescriptor fd) throws IOException {
@ -153,6 +159,8 @@ class FileDispatcherImpl 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;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 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
@ -70,3 +70,9 @@ Java_java_io_FileDescriptor_getAppend(JNIEnv *env, jclass fdClass, jint fd) {
int flags = fcntl(fd, F_GETFL);
return ((flags & O_APPEND) == 0) ? JNI_FALSE : JNI_TRUE;
}
// instance method close0 for FileDescriptor
JNIEXPORT void JNICALL
Java_java_io_FileDescriptor_close(JNIEnv *env, jobject this) {
fileDescriptorClose(env, this);
}

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 2003, 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#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) {
fileClose(env, this, fis_fd);
}

View File

@ -1,48 +0,0 @@
/*
* Copyright (c) 2003, 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#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) {
fileClose(env, this, raf_fd);
}

View File

@ -124,19 +124,31 @@ fileOpen(JNIEnv *env, jobject this, jstring path, jfieldID fid, int flags)
void
fileClose(JNIEnv *env, jobject this, jfieldID fid)
{
FD fd = GET_FD(this, fid);
if (fd == -1) {
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)->GetIntField(env, this, IO_fd_fdID);
if ((*env)->ExceptionOccurred(env)) {
return;
}
/* Set the fd to -1 before closing it so that the timing window
* of other threads using the wrong fd (closed but recycled fd,
* that gets re-opened with some other filename) is reduced.
* Practically the chance of its occurance is low, however, we are
* taking extra precaution over here.
*/
SET_FD(this, -1, fid);
(*env)->SetIntField(env, this, IO_fd_fdID, -1);
if ((*env)->ExceptionOccurred(env)) {
return;
}
/*
* Don't close file descriptors 0, 1, or 2. If we close these stream
* then a subsequent file open or socket will use them. Instead we
@ -145,7 +157,7 @@ fileClose(JNIEnv *env, jobject this, jfieldID fid)
if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
int devnull = open("/dev/null", O_WRONLY);
if (devnull < 0) {
SET_FD(this, fd, fid); // restore fd
(*env)->SetIntField(env, this, IO_fd_fdID, fd);
JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
} else {
dup2(devnull, fd);

View File

@ -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
@ -103,6 +103,7 @@ FD handleOpen(const char *path, int oflag, int mode);
* IO helper function(s)
*/
void fileClose(JNIEnv *env, jobject this, jfieldID fid);
void fileDescriptorClose(JNIEnv *env, jobject this);
#ifdef MACOSX
jstring newStringPlatform(JNIEnv *env, const char* str);

View File

@ -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

View File

@ -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 {

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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

View File

@ -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);
/*