42 * 43 * @author Pavani Diwanji 44 * @see java.io.FileInputStream 45 * @see java.io.FileOutputStream 46 * @since 1.0 47 */ 48 public final class FileDescriptor { 49 50 private int fd; 51 52 private Closeable parent; 53 private List<Closeable> otherParents; 54 private boolean closed; 55 56 /** 57 * true, if file is opened for appending. 58 */ 59 private boolean append; 60 61 /** 62 * Constructs an (invalid) FileDescriptor 63 * object. 64 */ 65 public FileDescriptor() { 66 fd = -1; 67 } 68 69 private FileDescriptor(int fd) { 70 this.fd = fd; 71 this.append = getAppend(fd); 72 } 73 74 /** 75 * A handle to the standard input stream. Usually, this file 76 * descriptor is not used directly, but rather via the input stream 77 * known as <code>System.in</code>. 78 * 79 * @see java.lang.System#in 80 */ 81 public static final FileDescriptor in = new FileDescriptor(0); 82 83 /** 84 * A handle to the standard output stream. Usually, this file 85 * descriptor is not used directly, but rather via the output stream 86 * known as <code>System.out</code>. 87 * @see java.lang.System#out 88 */ 89 public static final FileDescriptor out = new FileDescriptor(1); 90 91 /** 148 // Set up JavaIOFileDescriptorAccess in SharedSecrets 149 static { 150 SharedSecrets.setJavaIOFileDescriptorAccess( 151 new JavaIOFileDescriptorAccess() { 152 public void set(FileDescriptor obj, int fd) { 153 obj.fd = fd; 154 } 155 156 public int get(FileDescriptor obj) { 157 return obj.fd; 158 } 159 160 public void setAppend(FileDescriptor obj, boolean append) { 161 obj.append = append; 162 } 163 164 public boolean getAppend(FileDescriptor obj) { 165 return obj.append; 166 } 167 168 public void setHandle(FileDescriptor obj, long handle) { 169 throw new UnsupportedOperationException(); 170 } 171 172 public long getHandle(FileDescriptor obj) { 173 throw new UnsupportedOperationException(); 174 } 175 } 176 ); 177 } 178 179 /** 180 * Returns true, if the file was opened for appending. 181 */ 182 private static native boolean getAppend(int fd); 183 184 /* 185 * Package private methods to track referents. 186 * If multiple streams point to the same FileDescriptor, we cycle 187 * through the list of all referents and call close() 188 */ 189 190 /** 191 * Attach a Closeable to this FD for tracking. 192 * parent reference is added to otherParents when 193 * needed to make closeAll simpler. 194 */ 195 synchronized void attach(Closeable c) { 196 if (parent == null) { 197 // first caller gets to do this 198 parent = c; 199 } else if (otherParents == null) { 200 otherParents = new ArrayList<>(); 201 otherParents.add(parent); 202 otherParents.add(c); | 42 * 43 * @author Pavani Diwanji 44 * @see java.io.FileInputStream 45 * @see java.io.FileOutputStream 46 * @since 1.0 47 */ 48 public final class FileDescriptor { 49 50 private int fd; 51 52 private Closeable parent; 53 private List<Closeable> otherParents; 54 private boolean closed; 55 56 /** 57 * true, if file is opened for appending. 58 */ 59 private boolean append; 60 61 /** 62 * true, if file is opened with O_DIRECT flag; 63 */ 64 private boolean direct; 65 66 /** 67 * Constructs an (invalid) FileDescriptor 68 * object. 69 */ 70 public FileDescriptor() { 71 fd = -1; 72 } 73 74 private FileDescriptor(int fd) { 75 this.fd = fd; 76 this.append = getAppend(fd); 77 this.direct = getDirect(fd); 78 } 79 80 /** 81 * A handle to the standard input stream. Usually, this file 82 * descriptor is not used directly, but rather via the input stream 83 * known as <code>System.in</code>. 84 * 85 * @see java.lang.System#in 86 */ 87 public static final FileDescriptor in = new FileDescriptor(0); 88 89 /** 90 * A handle to the standard output stream. Usually, this file 91 * descriptor is not used directly, but rather via the output stream 92 * known as <code>System.out</code>. 93 * @see java.lang.System#out 94 */ 95 public static final FileDescriptor out = new FileDescriptor(1); 96 97 /** 154 // Set up JavaIOFileDescriptorAccess in SharedSecrets 155 static { 156 SharedSecrets.setJavaIOFileDescriptorAccess( 157 new JavaIOFileDescriptorAccess() { 158 public void set(FileDescriptor obj, int fd) { 159 obj.fd = fd; 160 } 161 162 public int get(FileDescriptor obj) { 163 return obj.fd; 164 } 165 166 public void setAppend(FileDescriptor obj, boolean append) { 167 obj.append = append; 168 } 169 170 public boolean getAppend(FileDescriptor obj) { 171 return obj.append; 172 } 173 174 public void setDirect(FileDescriptor obj, boolean direct) { 175 obj.direct = direct; 176 } 177 178 public boolean getDirect(FileDescriptor obj) { 179 return obj.direct; 180 } 181 182 public void setHandle(FileDescriptor obj, long handle) { 183 throw new UnsupportedOperationException(); 184 } 185 186 public long getHandle(FileDescriptor obj) { 187 throw new UnsupportedOperationException(); 188 } 189 } 190 ); 191 } 192 193 /** 194 * Returns true, if the file was opened for appending. 195 */ 196 private static native boolean getAppend(int fd); 197 198 /** 199 * Returns true, if the file was opened with O_DIRECT flag. 200 */ 201 private native boolean getDirect(int fd); 202 203 /* 204 * Package private methods to track referents. 205 * If multiple streams point to the same FileDescriptor, we cycle 206 * through the list of all referents and call close() 207 */ 208 209 /** 210 * Attach a Closeable to this FD for tracking. 211 * parent reference is added to otherParents when 212 * needed to make closeAll simpler. 213 */ 214 synchronized void attach(Closeable c) { 215 if (parent == null) { 216 // first caller gets to do this 217 parent = c; 218 } else if (otherParents == null) { 219 otherParents = new ArrayList<>(); 220 otherParents.add(parent); 221 otherParents.add(c); |