1 /* 2 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. 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. 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 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 /** 50 * A counter for tracking the FIS/FOS/RAF instances that 51 * use this FileDescriptor. The FIS/FOS.finalize() will not release 52 * the FileDescriptor if it is still under user by a stream. 53 */ 54 private AtomicInteger useCount; 55 56 /** 57 * Constructs an (invalid) FileDescriptor 58 * object. 59 */ 60 public /**/ FileDescriptor() { 61 fd = -1; 62 useCount = new AtomicInteger(); 63 } 64 65 private /* */ FileDescriptor(int fd) { 66 this.fd = fd; 67 useCount = new AtomicInteger(); 68 } 69 70 /** 71 * A handle to the standard input stream. Usually, this file 72 * descriptor is not used directly, but rather via the input stream 73 * known as <code>System.in</code>. 74 * 75 * @see java.lang.System#in 76 */ 77 public static final FileDescriptor in = new FileDescriptor(0); 78 79 /** 80 * A handle to the standard output stream. Usually, this file 81 * descriptor is not used directly, but rather via the output stream 82 * known as <code>System.out</code>. 83 * @see java.lang.System#out 84 */ 85 public static final FileDescriptor out = new FileDescriptor(1); 86 87 /** 147 new sun.misc.JavaIOFileDescriptorAccess() { 148 public void set(FileDescriptor obj, int fd) { 149 obj.fd = fd; 150 } 151 152 public int get(FileDescriptor obj) { 153 return obj.fd; 154 } 155 156 public void setHandle(FileDescriptor obj, long handle) { 157 throw new UnsupportedOperationException(); 158 } 159 160 public long getHandle(FileDescriptor obj) { 161 throw new UnsupportedOperationException(); 162 } 163 } 164 ); 165 } 166 167 // package private methods used by FIS, FOS and RAF 168 169 int incrementAndGetUseCount() { 170 return useCount.incrementAndGet(); 171 } 172 173 int decrementAndGetUseCount() { 174 return useCount.decrementAndGet(); 175 } 176 } | 1 /* 2 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. 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. 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.ArrayList; 29 import java.util.List; 30 31 /** 32 * Instances of the file descriptor class serve as an opaque handle 33 * to the underlying machine-specific structure representing an open 34 * file, an open socket, or another source or sink of bytes. The 35 * main practical use for a file descriptor is to create a 36 * <code>FileInputStream</code> or <code>FileOutputStream</code> to 37 * contain it. 38 * <p> 39 * Applications should not create their own file descriptors. 40 * 41 * @author Pavani Diwanji 42 * @see java.io.FileInputStream 43 * @see java.io.FileOutputStream 44 * @since JDK1.0 45 */ 46 public final class FileDescriptor { 47 48 private int fd; 49 50 private Closeable parent; 51 private List<Closeable> otherParents; 52 private boolean closed; 53 54 /** 55 * Constructs an (invalid) FileDescriptor 56 * object. 57 */ 58 public /**/ FileDescriptor() { 59 fd = -1; 60 } 61 62 private /* */ FileDescriptor(int fd) { 63 this.fd = fd; 64 } 65 66 /** 67 * A handle to the standard input stream. Usually, this file 68 * descriptor is not used directly, but rather via the input stream 69 * known as <code>System.in</code>. 70 * 71 * @see java.lang.System#in 72 */ 73 public static final FileDescriptor in = new FileDescriptor(0); 74 75 /** 76 * A handle to the standard output stream. Usually, this file 77 * descriptor is not used directly, but rather via the output stream 78 * known as <code>System.out</code>. 79 * @see java.lang.System#out 80 */ 81 public static final FileDescriptor out = new FileDescriptor(1); 82 83 /** 143 new sun.misc.JavaIOFileDescriptorAccess() { 144 public void set(FileDescriptor obj, int fd) { 145 obj.fd = fd; 146 } 147 148 public int get(FileDescriptor obj) { 149 return obj.fd; 150 } 151 152 public void setHandle(FileDescriptor obj, long handle) { 153 throw new UnsupportedOperationException(); 154 } 155 156 public long getHandle(FileDescriptor obj) { 157 throw new UnsupportedOperationException(); 158 } 159 } 160 ); 161 } 162 163 /* 164 * Package private methods to track referents. 165 * If multiple streams point to the same FileDescriptor, we cycle 166 * through the list of all referents and call close() 167 */ 168 169 /** 170 * Attach a Closeable to this FD for tracking. 171 * parent reference is added to otherParents when 172 * needed to make closeAll simpler. 173 */ 174 synchronized void attach(Closeable c) { 175 if (parent == null) { 176 // first caller gets to do this 177 parent = c; 178 } else if (otherParents == null) { 179 otherParents = new ArrayList<>(); 180 otherParents.add(parent); 181 otherParents.add(c); 182 } else { 183 otherParents.add(c); 184 } 185 } 186 187 /** 188 * Cycle through all Closeables sharing this FD and call 189 * close() on each one. 190 * 191 * The caller closeable gets to call close0(). 192 */ 193 @SuppressWarnings("try") 194 synchronized void closeAll(Closeable releaser) throws IOException { 195 if (!closed) { 196 closed = true; 197 IOException ioe = null; 198 try (Closeable c = releaser) { 199 if (otherParents != null) { 200 for (Closeable referent : otherParents) { 201 try { 202 referent.close(); 203 } catch(IOException x) { 204 if (ioe == null) { 205 ioe = x; 206 } else { 207 ioe.addSuppressed(x); 208 } 209 } 210 } 211 } 212 } catch(IOException ex) { 213 /* 214 * If releaser close() throws IOException 215 * add other exceptions as suppressed. 216 */ 217 if (ioe != null) 218 ex.addSuppressed(ioe); 219 ioe = ex; 220 } finally { 221 if (ioe != null) 222 throw ioe; 223 } 224 } 225 } 226 } |