J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 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 |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 open |
| 33 | * file, an open socket, or another source or sink of bytes. The |
| 34 | * main practical use for a file descriptor is to create a |
| 35 | * <code>FileInputStream</code> or <code>FileOutputStream</code> to |
| 36 | * contain it. |
| 37 | * <p> |
| 38 | * Applications should not create their own file descriptors. |
| 39 | * |
| 40 | * @author Pavani Diwanji |
| 41 | * @see java.io.FileInputStream |
| 42 | * @see java.io.FileOutputStream |
| 43 | * @since JDK1.0 |
| 44 | */ |
| 45 | public final class FileDescriptor { |
| 46 | |
| 47 | private int fd; |
| 48 | |
| 49 | private long handle; |
| 50 | |
| 51 | /** |
| 52 | * A use counter for tracking the FIS/FOS/RAF instances that |
| 53 | * use this FileDescriptor. The FIS/FOS.finalize() will not release |
| 54 | * the FileDescriptor if it is still under use by any stream. |
| 55 | */ |
| 56 | private AtomicInteger useCount; |
| 57 | |
| 58 | |
| 59 | /** |
| 60 | * Constructs an (invalid) FileDescriptor |
| 61 | * object. |
| 62 | */ |
| 63 | public /**/ FileDescriptor() { |
| 64 | fd = -1; |
| 65 | handle = -1; |
| 66 | useCount = new AtomicInteger(); |
| 67 | } |
| 68 | |
| 69 | static { |
| 70 | initIDs(); |
| 71 | } |
| 72 | |
| 73 | // Set up JavaIOFileDescriptorAccess in SharedSecrets |
| 74 | static { |
| 75 | sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess( |
| 76 | new sun.misc.JavaIOFileDescriptorAccess() { |
| 77 | public void set(FileDescriptor obj, int fd) { |
| 78 | obj.fd = fd; |
| 79 | } |
| 80 | |
| 81 | public int get(FileDescriptor obj) { |
| 82 | return obj.fd; |
| 83 | } |
| 84 | } |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * A handle to the standard input stream. Usually, this file |
| 90 | * descriptor is not used directly, but rather via the input stream |
| 91 | * known as <code>System.in</code>. |
| 92 | * |
| 93 | * @see java.lang.System#in |
| 94 | */ |
| 95 | public static final FileDescriptor in = standardStream(0); |
| 96 | |
| 97 | /** |
| 98 | * A handle to the standard output stream. Usually, this file |
| 99 | * descriptor is not used directly, but rather via the output stream |
| 100 | * known as <code>System.out</code>. |
| 101 | * @see java.lang.System#out |
| 102 | */ |
| 103 | public static final FileDescriptor out = standardStream(1); |
| 104 | |
| 105 | /** |
| 106 | * A handle to the standard error stream. Usually, this file |
| 107 | * descriptor is not used directly, but rather via the output stream |
| 108 | * known as <code>System.err</code>. |
| 109 | * |
| 110 | * @see java.lang.System#err |
| 111 | */ |
| 112 | public static final FileDescriptor err = standardStream(2); |
| 113 | |
| 114 | /** |
| 115 | * Tests if this file descriptor object is valid. |
| 116 | * |
| 117 | * @return <code>true</code> if the file descriptor object represents a |
| 118 | * valid, open file, socket, or other active I/O connection; |
| 119 | * <code>false</code> otherwise. |
| 120 | */ |
| 121 | public boolean valid() { |
| 122 | return ((handle != -1) || (fd != -1)); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Force all system buffers to synchronize with the underlying |
| 127 | * device. This method returns after all modified data and |
| 128 | * attributes of this FileDescriptor have been written to the |
| 129 | * relevant device(s). In particular, if this FileDescriptor |
| 130 | * refers to a physical storage medium, such as a file in a file |
| 131 | * system, sync will not return until all in-memory modified copies |
| 132 | * of buffers associated with this FileDesecriptor have been |
| 133 | * written to the physical medium. |
| 134 | * |
| 135 | * sync is meant to be used by code that requires physical |
| 136 | * storage (such as a file) to be in a known state For |
| 137 | * example, a class that provided a simple transaction facility |
| 138 | * might use sync to ensure that all changes to a file caused |
| 139 | * by a given transaction were recorded on a storage medium. |
| 140 | * |
| 141 | * sync only affects buffers downstream of this FileDescriptor. If |
| 142 | * any in-memory buffering is being done by the application (for |
| 143 | * example, by a BufferedOutputStream object), those buffers must |
| 144 | * be flushed into the FileDescriptor (for example, by invoking |
| 145 | * OutputStream.flush) before that data will be affected by sync. |
| 146 | * |
| 147 | * @exception SyncFailedException |
| 148 | * Thrown when the buffers cannot be flushed, |
| 149 | * or because the system cannot guarantee that all the |
| 150 | * buffers have been synchronized with physical media. |
| 151 | * @since JDK1.1 |
| 152 | */ |
| 153 | public native void sync() throws SyncFailedException; |
| 154 | |
| 155 | /* This routine initializes JNI field offsets for the class */ |
| 156 | private static native void initIDs(); |
| 157 | |
| 158 | private static native long set(int d); |
| 159 | |
| 160 | private static FileDescriptor standardStream(int fd) { |
| 161 | FileDescriptor desc = new FileDescriptor(); |
| 162 | desc.handle = set(fd); |
| 163 | return desc; |
| 164 | } |
| 165 | |
| 166 | // package private methods used by FIS, FOS and RAF. |
| 167 | |
| 168 | int incrementAndGetUseCount() { |
| 169 | return useCount.incrementAndGet(); |
| 170 | } |
| 171 | |
| 172 | int decrementAndGetUseCount() { |
| 173 | return useCount.decrementAndGet(); |
| 174 | } |
| 175 | } |