src/java.base/windows/classes/java/io/FileDescriptor.java
changeset 47235 9ef10c6e67b8
parent 47216 71c04702a3d5
child 48224 be0df5ab3093
equal deleted inserted replaced
47233:5ee94c2e087e 47235:9ef10c6e67b8
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    30 import jdk.internal.misc.JavaIOFileDescriptorAccess;
    30 import jdk.internal.misc.JavaIOFileDescriptorAccess;
    31 import jdk.internal.misc.SharedSecrets;
    31 import jdk.internal.misc.SharedSecrets;
    32 
    32 
    33 /**
    33 /**
    34  * Instances of the file descriptor class serve as an opaque handle
    34  * Instances of the file descriptor class serve as an opaque handle
    35  * to the underlying machine-specific structure representing an
    35  * to the underlying machine-specific structure representing an open
    36  * open file, an open socket, or another source or sink of bytes.
    36  * file, an open socket, or another source or sink of bytes.
    37  * The main practical use for a file descriptor is to create a
    37  * The main practical use for a file descriptor is to create a
    38  * {@link FileInputStream} or {@link FileOutputStream} to contain it.
    38  * {@link FileInputStream} or {@link FileOutputStream} to contain it.
    39  *
    39  * <p>
    40  * <p>Applications should not create their own file descriptors.
    40  * Applications should not create their own file descriptors.
    41  *
    41  *
    42  * @author  Pavani Diwanji
    42  * @author  Pavani Diwanji
    43  * @since   1.0
    43  * @since   1.0
    44  */
    44  */
    45 public final class FileDescriptor {
    45 public final class FileDescriptor {
    55     /**
    55     /**
    56      * true, if file is opened for appending.
    56      * true, if file is opened for appending.
    57      */
    57      */
    58     private boolean append;
    58     private boolean append;
    59 
    59 
    60     /**
       
    61      * Constructs an (invalid) FileDescriptor
       
    62      * object.
       
    63      */
       
    64     public /**/ FileDescriptor() {
       
    65         fd = -1;
       
    66         handle = -1;
       
    67     }
       
    68 
       
    69     static {
    60     static {
    70         initIDs();
    61         initIDs();
    71     }
    62     }
    72 
    63 
    73     // Set up JavaIOFileDescriptorAccess in SharedSecrets
    64     // Set up JavaIOFileDescriptorAccess in SharedSecrets
    74     static {
    65     static {
    75         SharedSecrets.setJavaIOFileDescriptorAccess(
    66         SharedSecrets.setJavaIOFileDescriptorAccess(
    76             new JavaIOFileDescriptorAccess() {
    67                 new JavaIOFileDescriptorAccess() {
    77                 public void set(FileDescriptor obj, int fd) {
    68                     public void set(FileDescriptor fdo, int fd) {
    78                     obj.fd = fd;
    69                         fdo.fd = fd;
       
    70                     }
       
    71 
       
    72                     public int get(FileDescriptor fdo) {
       
    73                         return fdo.fd;
       
    74                     }
       
    75 
       
    76                     public void setAppend(FileDescriptor fdo, boolean append) {
       
    77                         fdo.append = append;
       
    78                     }
       
    79 
       
    80                     public boolean getAppend(FileDescriptor fdo) {
       
    81                         return fdo.append;
       
    82                     }
       
    83 
       
    84                     public void close(FileDescriptor fdo) {
       
    85                         fdo.close();
       
    86                     }
       
    87 
       
    88                     public void setHandle(FileDescriptor fdo, long handle) {
       
    89                         fdo.handle = handle;
       
    90                     }
       
    91 
       
    92                     public long getHandle(FileDescriptor fdo) {
       
    93                         return fdo.handle;
       
    94                     }
    79                 }
    95                 }
    80 
       
    81                 public int get(FileDescriptor obj) {
       
    82                     return obj.fd;
       
    83                 }
       
    84 
       
    85                 public void setAppend(FileDescriptor obj, boolean append) {
       
    86                     obj.append = append;
       
    87                 }
       
    88 
       
    89                 public boolean getAppend(FileDescriptor obj) {
       
    90                     return obj.append;
       
    91                 }
       
    92 
       
    93                 public void setHandle(FileDescriptor obj, long handle) {
       
    94                     obj.handle = handle;
       
    95                 }
       
    96 
       
    97                 public long getHandle(FileDescriptor obj) {
       
    98                     return obj.handle;
       
    99                 }
       
   100             }
       
   101         );
    96         );
       
    97     }
       
    98 
       
    99     /**
       
   100      * Constructs an (invalid) FileDescriptor
       
   101      * object.
       
   102      */
       
   103     public FileDescriptor() {
       
   104         fd = -1;
       
   105         handle = -1;
   102     }
   106     }
   103 
   107 
   104     /**
   108     /**
   105      * A handle to the standard input stream. Usually, this file
   109      * A handle to the standard input stream. Usually, this file
   106      * descriptor is not used directly, but rather via the input stream
   110      * descriptor is not used directly, but rather via the input stream
   133      * @return  {@code true} if the file descriptor object represents a
   137      * @return  {@code true} if the file descriptor object represents a
   134      *          valid, open file, socket, or other active I/O connection;
   138      *          valid, open file, socket, or other active I/O connection;
   135      *          {@code false} otherwise.
   139      *          {@code false} otherwise.
   136      */
   140      */
   137     public boolean valid() {
   141     public boolean valid() {
   138         return ((handle != -1) || (fd != -1));
   142         return (handle != -1) || (fd != -1);
   139     }
   143     }
   140 
   144 
   141     /**
   145     /**
   142      * Force all system buffers to synchronize with the underlying
   146      * Force all system buffers to synchronize with the underlying
   143      * device.  This method returns after all modified data and
   147      * device.  This method returns after all modified data and
   176     private static FileDescriptor standardStream(int fd) {
   180     private static FileDescriptor standardStream(int fd) {
   177         FileDescriptor desc = new FileDescriptor();
   181         FileDescriptor desc = new FileDescriptor();
   178         desc.handle = set(fd);
   182         desc.handle = set(fd);
   179         return desc;
   183         return desc;
   180     }
   184     }
       
   185 
       
   186     /**
       
   187      * Close the raw file descriptor or handle, if it has not already been closed
       
   188      * and set the fd and handle to -1.
       
   189      * Package private to allow it to be used in java.io.
       
   190      */
       
   191     native void close();
   181 
   192 
   182     /*
   193     /*
   183      * Package private methods to track referents.
   194      * Package private methods to track referents.
   184      * If multiple streams point to the same FileDescriptor, we cycle
   195      * If multiple streams point to the same FileDescriptor, we cycle
   185      * through the list of all referents and call close()
   196      * through the list of all referents and call close()