Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/java/io/FileDescriptor.java
32287 views
/*1* Copyright (c) 1995, 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 an open33* file, an open socket, or another source or sink of bytes. The34* main practical use for a file descriptor is to create a35* <code>FileInputStream</code> or <code>FileOutputStream</code> to36* contain it.37* <p>38* Applications should not create their own file descriptors.39*40* @author Pavani Diwanji41* @see java.io.FileInputStream42* @see java.io.FileOutputStream43* @since JDK1.044*/45public final class FileDescriptor {4647private int fd;4849private Closeable parent;50private List<Closeable> otherParents;51private boolean closed;5253/**54* Constructs an (invalid) FileDescriptor55* object.56*/57public /**/ FileDescriptor() {58fd = -1;59}6061private /* */ FileDescriptor(int fd) {62this.fd = fd;63}6465/**66* A handle to the standard input stream. Usually, this file67* descriptor is not used directly, but rather via the input stream68* known as <code>System.in</code>.69*70* @see java.lang.System#in71*/72public static final FileDescriptor in = new FileDescriptor(0);7374/**75* A handle to the standard output stream. Usually, this file76* descriptor is not used directly, but rather via the output stream77* known as <code>System.out</code>.78* @see java.lang.System#out79*/80public static final FileDescriptor out = new FileDescriptor(1);8182/**83* A handle to the standard error stream. Usually, this file84* descriptor is not used directly, but rather via the output stream85* known as <code>System.err</code>.86*87* @see java.lang.System#err88*/89public static final FileDescriptor err = new FileDescriptor(2);9091/**92* Tests if this file descriptor object is valid.93*94* @return <code>true</code> if the file descriptor object represents a95* valid, open file, socket, or other active I/O connection;96* <code>false</code> otherwise.97*/98public boolean valid() {99return fd != -1;100}101102/**103* Force all system buffers to synchronize with the underlying104* device. This method returns after all modified data and105* attributes of this FileDescriptor have been written to the106* relevant device(s). In particular, if this FileDescriptor107* refers to a physical storage medium, such as a file in a file108* system, sync will not return until all in-memory modified copies109* of buffers associated with this FileDescriptor have been110* written to the physical medium.111*112* sync is meant to be used by code that requires physical113* storage (such as a file) to be in a known state For114* example, a class that provided a simple transaction facility115* might use sync to ensure that all changes to a file caused116* by a given transaction were recorded on a storage medium.117*118* sync only affects buffers downstream of this FileDescriptor. If119* any in-memory buffering is being done by the application (for120* example, by a BufferedOutputStream object), those buffers must121* be flushed into the FileDescriptor (for example, by invoking122* OutputStream.flush) before that data will be affected by sync.123*124* @exception SyncFailedException125* Thrown when the buffers cannot be flushed,126* or because the system cannot guarantee that all the127* buffers have been synchronized with physical media.128* @since JDK1.1129*/130public native void sync() throws SyncFailedException;131132/* This routine initializes JNI field offsets for the class */133private static native void initIDs();134135static {136initIDs();137}138139// Set up JavaIOFileDescriptorAccess in SharedSecrets140static {141sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(142new sun.misc.JavaIOFileDescriptorAccess() {143public void set(FileDescriptor obj, int fd) {144obj.fd = fd;145}146147public int get(FileDescriptor obj) {148return obj.fd;149}150151public void setHandle(FileDescriptor obj, long handle) {152throw new UnsupportedOperationException();153}154155public long getHandle(FileDescriptor obj) {156throw new UnsupportedOperationException();157}158}159);160}161162/*163* Package private methods to track referents.164* If multiple streams point to the same FileDescriptor, we cycle165* through the list of all referents and call close()166*/167168/**169* Attach a Closeable to this FD for tracking.170* parent reference is added to otherParents when171* needed to make closeAll simpler.172*/173synchronized void attach(Closeable c) {174if (parent == null) {175// first caller gets to do this176parent = c;177} else if (otherParents == null) {178otherParents = new ArrayList<>();179otherParents.add(parent);180otherParents.add(c);181} else {182otherParents.add(c);183}184}185186/**187* Cycle through all Closeables sharing this FD and call188* close() on each one.189*190* The caller closeable gets to call close0().191*/192@SuppressWarnings("try")193synchronized void closeAll(Closeable releaser) throws IOException {194if (!closed) {195closed = true;196IOException ioe = null;197try (Closeable c = releaser) {198if (otherParents != null) {199for (Closeable referent : otherParents) {200try {201referent.close();202} catch(IOException x) {203if (ioe == null) {204ioe = x;205} else {206ioe.addSuppressed(x);207}208}209}210}211} catch(IOException ex) {212/*213* If releaser close() throws IOException214* add other exceptions as suppressed.215*/216if (ioe != null)217ex.addSuppressed(ioe);218ioe = ex;219} finally {220if (ioe != null)221throw ioe;222}223}224}225}226227228