src/windows/classes/java/io/FileDescriptor.java

Print this page




   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 


  30 /**
  31  * Instances of the file descriptor class serve as an opaque handle
  32  * to the underlying machine-specific structure representing an
  33  * open file, an open socket, or another source or sink of bytes.
  34  * The main practical use for a file descriptor is to create a
  35  * {@link FileInputStream} or {@link FileOutputStream} to contain it.
  36  *
  37  * <p>Applications should not create their own file descriptors.
  38  *
  39  * @author  Pavani Diwanji
  40  * @since   JDK1.0
  41  */
  42 public final class FileDescriptor {
  43 
  44     private int fd;

  45     private long handle;
  46     private Closeable parent;
  47     private List<Closeable> otherParents;
  48     private boolean closed;
  49 
  50     /**








  51      * Constructs an (invalid) FileDescriptor
  52      * object.
  53      */
  54     public /**/ FileDescriptor() {
  55         fd = -1;
  56         handle = -1;

  57     }
  58 
  59     static {
  60         initIDs();
  61     }
  62 
  63     // Set up JavaIOFileDescriptorAccess in SharedSecrets
  64     static {
  65         sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
  66             new sun.misc.JavaIOFileDescriptorAccess() {
  67                 public void set(FileDescriptor obj, int fd) {
  68                     obj.fd = fd;
  69                 }
  70 
  71                 public int get(FileDescriptor obj) {
  72                     return obj.fd;
  73                 }
  74 
  75                 public void setHandle(FileDescriptor obj, long handle) {
  76                     obj.handle = handle;


 144      *
 145      * @exception SyncFailedException
 146      *        Thrown when the buffers cannot be flushed,
 147      *        or because the system cannot guarantee that all the
 148      *        buffers have been synchronized with physical media.
 149      * @since     JDK1.1
 150      */
 151     public native void sync() throws SyncFailedException;
 152 
 153     /* This routine initializes JNI field offsets for the class */
 154     private static native void initIDs();
 155 
 156     private static native long set(int d);
 157 
 158     private static FileDescriptor standardStream(int fd) {
 159         FileDescriptor desc = new FileDescriptor();
 160         desc.handle = set(fd);
 161         return desc;
 162     }
 163 
 164     /*
 165      * Package private methods to track referents.
 166      * If multiple streams point to the same FileDescriptor, we cycle
 167      * through the list of all referents and call close()
 168      */
 169 
 170     /**
 171      * Attach a Closeable to this FD for tracking.
 172      * parent reference is added to otherParents when
 173      * needed to make closeAll simpler.
 174      */
 175     synchronized void attach(Closeable c) {
 176         if (parent == null) {
 177             // first caller gets to do this
 178             parent = c;
 179         } else if (otherParents == null) {
 180             otherParents = new ArrayList<>();
 181             otherParents.add(parent);
 182             otherParents.add(c);
 183         } else {
 184             otherParents.add(c);
 185         }
 186     }
 187 
 188     /**
 189      * Cycle through all Closeables sharing this FD and call
 190      * close() on each one.
 191      *
 192      * The caller closeable gets to call close0().
 193      */
 194     @SuppressWarnings("try")
 195     synchronized void closeAll(Closeable releaser) throws IOException {
 196         if (!closed) {
 197             closed = true;
 198             IOException ioe = null;
 199             try (Closeable c = releaser) {
 200                 if (otherParents != null) {
 201                     for (Closeable referent : otherParents) {
 202                         try {
 203                             referent.close();
 204                         } catch(IOException x) {
 205                             if (ioe == null) {
 206                                 ioe = x;
 207                             } else {
 208                                 ioe.addSuppressed(x);
 209                             }
 210                         }
 211                     }
 212                 }
 213             } catch(IOException ex) {
 214                 /*
 215                  * If releaser close() throws IOException
 216                  * add other exceptions as suppressed.
 217                  */
 218                 if (ioe != null)
 219                     ex.addSuppressed(ioe);
 220                 ioe = ex;
 221             } finally {
 222                 if (ioe != null)
 223                     throw ioe;
 224             }
 225         }
 226     }
 227 }


   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.io;


  27 
  28 import java.util.concurrent.atomic.AtomicInteger;
  29 
  30 /**
  31  * Instances of the file descriptor class serve as an opaque handle
  32  * to the underlying machine-specific structure representing an
  33  * open file, an open socket, or another source or sink of bytes.
  34  * The main practical use for a file descriptor is to create a
  35  * {@link FileInputStream} or {@link FileOutputStream} to contain it.
  36  *
  37  * <p>Applications should not create their own file descriptors.
  38  *
  39  * @author  Pavani Diwanji
  40  * @since   JDK1.0
  41  */
  42 public final class FileDescriptor {
  43 
  44     private int fd;
  45 
  46     private long handle;



  47 
  48     /**
  49      * A use counter for tracking the FIS/FOS/RAF instances that
  50      * use this FileDescriptor. The FIS/FOS.finalize() will not release
  51      * the FileDescriptor if it is still under use by any stream.
  52      */
  53     private AtomicInteger useCount;
  54 
  55 
  56     /**
  57      * Constructs an (invalid) FileDescriptor
  58      * object.
  59      */
  60     public /**/ FileDescriptor() {
  61         fd = -1;
  62         handle = -1;
  63         useCount = new AtomicInteger();
  64     }
  65 
  66     static {
  67         initIDs();
  68     }
  69 
  70     // Set up JavaIOFileDescriptorAccess in SharedSecrets
  71     static {
  72         sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
  73             new sun.misc.JavaIOFileDescriptorAccess() {
  74                 public void set(FileDescriptor obj, int fd) {
  75                     obj.fd = fd;
  76                 }
  77 
  78                 public int get(FileDescriptor obj) {
  79                     return obj.fd;
  80                 }
  81 
  82                 public void setHandle(FileDescriptor obj, long handle) {
  83                     obj.handle = handle;


 151      *
 152      * @exception SyncFailedException
 153      *        Thrown when the buffers cannot be flushed,
 154      *        or because the system cannot guarantee that all the
 155      *        buffers have been synchronized with physical media.
 156      * @since     JDK1.1
 157      */
 158     public native void sync() throws SyncFailedException;
 159 
 160     /* This routine initializes JNI field offsets for the class */
 161     private static native void initIDs();
 162 
 163     private static native long set(int d);
 164 
 165     private static FileDescriptor standardStream(int fd) {
 166         FileDescriptor desc = new FileDescriptor();
 167         desc.handle = set(fd);
 168         return desc;
 169     }
 170 
 171     // package private methods used by FIS, FOS and RAF.




 172 
 173     int incrementAndGetUseCount() {
 174         return useCount.incrementAndGet();













 175     }

 176 
 177     int decrementAndGetUseCount() {
 178         return useCount.decrementAndGet();



















 179     }

















 180 }