Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/classes/java/io/FileDescriptor.java
32287 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.util.ArrayList;28import java.util.List;2930/**31* Instances of the file descriptor class serve as an opaque handle32* to the underlying machine-specific structure representing an33* open file, an open socket, or another source or sink of bytes.34* The main practical use for a file descriptor is to create a35* {@link FileInputStream} or {@link FileOutputStream} to contain it.36*37* <p>Applications should not create their own file descriptors.38*39* @author Pavani Diwanji40* @since JDK1.041*/42public final class FileDescriptor {4344private int fd;4546private long handle;4748private Closeable parent;49private List<Closeable> otherParents;50private boolean closed;5152/**53* Constructs an (invalid) FileDescriptor54* object.55*/56public /**/ FileDescriptor() {57fd = -1;58handle = -1;59}6061static {62initIDs();63}6465// Set up JavaIOFileDescriptorAccess in SharedSecrets66static {67sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(68new sun.misc.JavaIOFileDescriptorAccess() {69public void set(FileDescriptor obj, int fd) {70obj.fd = fd;71}7273public int get(FileDescriptor obj) {74return obj.fd;75}7677public void setHandle(FileDescriptor obj, long handle) {78obj.handle = handle;79}8081public long getHandle(FileDescriptor obj) {82return obj.handle;83}84}85);86}8788/**89* A handle to the standard input stream. Usually, this file90* descriptor is not used directly, but rather via the input stream91* known as {@code System.in}.92*93* @see java.lang.System#in94*/95public static final FileDescriptor in = standardStream(0);9697/**98* A handle to the standard output stream. Usually, this file99* descriptor is not used directly, but rather via the output stream100* known as {@code System.out}.101* @see java.lang.System#out102*/103public static final FileDescriptor out = standardStream(1);104105/**106* A handle to the standard error stream. Usually, this file107* descriptor is not used directly, but rather via the output stream108* known as {@code System.err}.109*110* @see java.lang.System#err111*/112public static final FileDescriptor err = standardStream(2);113114/**115* Tests if this file descriptor object is valid.116*117* @return {@code true} if the file descriptor object represents a118* valid, open file, socket, or other active I/O connection;119* {@code false} otherwise.120*/121public boolean valid() {122return ((handle != -1) || (fd != -1));123}124125/**126* Force all system buffers to synchronize with the underlying127* device. This method returns after all modified data and128* attributes of this FileDescriptor have been written to the129* relevant device(s). In particular, if this FileDescriptor130* refers to a physical storage medium, such as a file in a file131* system, sync will not return until all in-memory modified copies132* of buffers associated with this FileDesecriptor have been133* written to the physical medium.134*135* sync is meant to be used by code that requires physical136* storage (such as a file) to be in a known state For137* example, a class that provided a simple transaction facility138* might use sync to ensure that all changes to a file caused139* by a given transaction were recorded on a storage medium.140*141* sync only affects buffers downstream of this FileDescriptor. If142* any in-memory buffering is being done by the application (for143* example, by a BufferedOutputStream object), those buffers must144* be flushed into the FileDescriptor (for example, by invoking145* OutputStream.flush) before that data will be affected by sync.146*147* @exception SyncFailedException148* Thrown when the buffers cannot be flushed,149* or because the system cannot guarantee that all the150* buffers have been synchronized with physical media.151* @since JDK1.1152*/153public native void sync() throws SyncFailedException;154155/* This routine initializes JNI field offsets for the class */156private static native void initIDs();157158private static native long set(int d);159160private static FileDescriptor standardStream(int fd) {161FileDescriptor desc = new FileDescriptor();162desc.handle = set(fd);163return desc;164}165166/*167* Package private methods to track referents.168* If multiple streams point to the same FileDescriptor, we cycle169* through the list of all referents and call close()170*/171172/**173* Attach a Closeable to this FD for tracking.174* parent reference is added to otherParents when175* needed to make closeAll simpler.176*/177synchronized void attach(Closeable c) {178if (parent == null) {179// first caller gets to do this180parent = c;181} else if (otherParents == null) {182otherParents = new ArrayList<>();183otherParents.add(parent);184otherParents.add(c);185} else {186otherParents.add(c);187}188}189190/**191* Cycle through all Closeables sharing this FD and call192* close() on each one.193*194* The caller closeable gets to call close0().195*/196@SuppressWarnings("try")197synchronized void closeAll(Closeable releaser) throws IOException {198if (!closed) {199closed = true;200IOException ioe = null;201try (Closeable c = releaser) {202if (otherParents != null) {203for (Closeable referent : otherParents) {204try {205referent.close();206} catch(IOException x) {207if (ioe == null) {208ioe = x;209} else {210ioe.addSuppressed(x);211}212}213}214}215} catch(IOException ex) {216/*217* If releaser close() throws IOException218* add other exceptions as suppressed.219*/220if (ioe != null)221ex.addSuppressed(ioe);222ioe = ex;223} finally {224if (ioe != null)225throw ioe;226}227}228}229}230231232