jdk/src/windows/classes/java/io/FileDescriptor.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 715 f16baef3a20e
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.concurrent.atomic.AtomicInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Instances of the file descriptor class serve as an opaque handle
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    32
 * to the underlying machine-specific structure representing an
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    33
 * open file, an open socket, or another source or sink of bytes.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    34
 * The main practical use for a file descriptor is to create a
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    35
 * {@link FileInputStream} or {@link FileOutputStream} to contain it.
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    36
 *
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    37
 * <p>Applications should not create their own file descriptors.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Pavani Diwanji
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public final class FileDescriptor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private long handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * A use counter for tracking the FIS/FOS/RAF instances that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * use this FileDescriptor. The FIS/FOS.finalize() will not release
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * the FileDescriptor if it is still under use by any stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private AtomicInteger useCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs an (invalid) FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public /**/ FileDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        handle = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        useCount = new AtomicInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Set up JavaIOFileDescriptorAccess in SharedSecrets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            new sun.misc.JavaIOFileDescriptorAccess() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                public void set(FileDescriptor obj, int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    obj.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                public int get(FileDescriptor obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    return obj.fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                }
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    81
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    82
                public void setHandle(FileDescriptor obj, long handle) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    83
                    obj.handle = handle;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    84
                }
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    85
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    86
                public long getHandle(FileDescriptor obj) {
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    87
                    return obj.handle;
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    88
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * A handle to the standard input stream. Usually, this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * descriptor is not used directly, but rather via the input stream
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
    96
     * known as {@code System.in}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see     java.lang.System#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final FileDescriptor in = standardStream(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * A handle to the standard output stream. Usually, this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * descriptor is not used directly, but rather via the output stream
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   105
     * known as {@code System.out}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see     java.lang.System#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static final FileDescriptor out = standardStream(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * A handle to the standard error stream. Usually, this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * descriptor is not used directly, but rather via the output stream
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   113
     * known as {@code System.err}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see     java.lang.System#err
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final FileDescriptor err = standardStream(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Tests if this file descriptor object is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   122
     * @return  {@code true} if the file descriptor object represents a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *          valid, open file, socket, or other active I/O connection;
48
dc5744ca15ea 4960438: (process) Need IO redirection API for subprocesses
martin
parents: 2
diff changeset
   124
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public boolean valid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return ((handle != -1) || (fd != -1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Force all system buffers to synchronize with the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * device.  This method returns after all modified data and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * attributes of this FileDescriptor have been written to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * relevant device(s).  In particular, if this FileDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * refers to a physical storage medium, such as a file in a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * system, sync will not return until all in-memory modified copies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * of buffers associated with this FileDesecriptor have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * written to the physical medium.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * sync is meant to be used by code that requires physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * storage (such as a file) to be in a known state  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * example, a class that provided a simple transaction facility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * might use sync to ensure that all changes to a file caused
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * by a given transaction were recorded on a storage medium.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * sync only affects buffers downstream of this FileDescriptor.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * any in-memory buffering is being done by the application (for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * example, by a BufferedOutputStream object), those buffers must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * be flushed into the FileDescriptor (for example, by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * OutputStream.flush) before that data will be affected by sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception SyncFailedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *        Thrown when the buffers cannot be flushed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *        or because the system cannot guarantee that all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *        buffers have been synchronized with physical media.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @since     JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public native void sync() throws SyncFailedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /* This routine initializes JNI field offsets for the class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static native long set(int d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static FileDescriptor standardStream(int fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        FileDescriptor desc = new FileDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        desc.handle = set(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    // package private methods used by FIS, FOS and RAF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    int incrementAndGetUseCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return useCount.incrementAndGet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    int decrementAndGetUseCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return useCount.decrementAndGet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}