1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/io/FileDescriptor.java Thu Oct 03 15:40:35 2013 +0200
1.3 @@ -0,0 +1,176 @@
1.4 +/*
1.5 + * Copyright (c) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
1.6 + * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
1.7 + *
1.8 + *
1.9 + *
1.10 + *
1.11 + *
1.12 + *
1.13 + *
1.14 + *
1.15 + *
1.16 + *
1.17 + *
1.18 + *
1.19 + *
1.20 + *
1.21 + *
1.22 + *
1.23 + *
1.24 + *
1.25 + *
1.26 + *
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +import java.util.concurrent.atomic.AtomicInteger;
1.32 +
1.33 +/**
1.34 + * Instances of the file descriptor class serve as an opaque handle
1.35 + * to the underlying machine-specific structure representing an open
1.36 + * file, an open socket, or another source or sink of bytes. The
1.37 + * main practical use for a file descriptor is to create a
1.38 + * <code>FileInputStream</code> or <code>FileOutputStream</code> to
1.39 + * contain it.
1.40 + * <p>
1.41 + * Applications should not create their own file descriptors.
1.42 + *
1.43 + * @author Pavani Diwanji
1.44 + * @see java.io.FileInputStream
1.45 + * @see java.io.FileOutputStream
1.46 + * @since JDK1.0
1.47 + */
1.48 +public final class FileDescriptor {
1.49 +
1.50 + private int fd;
1.51 +
1.52 + /**
1.53 + * A counter for tracking the FIS/FOS/RAF instances that
1.54 + * use this FileDescriptor. The FIS/FOS.finalize() will not release
1.55 + * the FileDescriptor if it is still under user by a stream.
1.56 + */
1.57 + private AtomicInteger useCount;
1.58 +
1.59 + /**
1.60 + * Constructs an (invalid) FileDescriptor
1.61 + * object.
1.62 + */
1.63 + public /**/ FileDescriptor() {
1.64 + fd = -1;
1.65 + useCount = new AtomicInteger();
1.66 + }
1.67 +
1.68 + private /* */ FileDescriptor(int fd) {
1.69 + this.fd = fd;
1.70 + useCount = new AtomicInteger();
1.71 + }
1.72 +
1.73 + /**
1.74 + * A handle to the standard input stream. Usually, this file
1.75 + * descriptor is not used directly, but rather via the input stream
1.76 + * known as <code>System.in</code>.
1.77 + *
1.78 + * @see java.lang.System#in
1.79 + */
1.80 + public static final FileDescriptor in = new FileDescriptor(0);
1.81 +
1.82 + /**
1.83 + * A handle to the standard output stream. Usually, this file
1.84 + * descriptor is not used directly, but rather via the output stream
1.85 + * known as <code>System.out</code>.
1.86 + * @see java.lang.System#out
1.87 + */
1.88 + public static final FileDescriptor out = new FileDescriptor(1);
1.89 +
1.90 + /**
1.91 + * A handle to the standard error stream. Usually, this file
1.92 + * descriptor is not used directly, but rather via the output stream
1.93 + * known as <code>System.err</code>.
1.94 + *
1.95 + * @see java.lang.System#err
1.96 + */
1.97 + public static final FileDescriptor err = new FileDescriptor(2);
1.98 +
1.99 + /**
1.100 + * Tests if this file descriptor object is valid.
1.101 + *
1.102 + * @return <code>true</code> if the file descriptor object represents a
1.103 + * valid, open file, socket, or other active I/O connection;
1.104 + * <code>false</code> otherwise.
1.105 + */
1.106 + public boolean valid() {
1.107 + return fd != -1;
1.108 + }
1.109 +
1.110 + /**
1.111 + * Force all system buffers to synchronize with the underlying
1.112 + * device. This method returns after all modified data and
1.113 + * attributes of this FileDescriptor have been written to the
1.114 + * relevant device(s). In particular, if this FileDescriptor
1.115 + * refers to a physical storage medium, such as a file in a file
1.116 + * system, sync will not return until all in-memory modified copies
1.117 + * of buffers associated with this FileDescriptor have been
1.118 + * written to the physical medium.
1.119 + *
1.120 + * sync is meant to be used by code that requires physical
1.121 + * storage (such as a file) to be in a known state For
1.122 + * example, a class that provided a simple transaction facility
1.123 + * might use sync to ensure that all changes to a file caused
1.124 + * by a given transaction were recorded on a storage medium.
1.125 + *
1.126 + * sync only affects buffers downstream of this FileDescriptor. If
1.127 + * any in-memory buffering is being done by the application (for
1.128 + * example, by a BufferedOutputStream object), those buffers must
1.129 + * be flushed into the FileDescriptor (for example, by invoking
1.130 + * OutputStream.flush) before that data will be affected by sync.
1.131 + *
1.132 + * @exception SyncFailedException
1.133 + * Thrown when the buffers cannot be flushed,
1.134 + * or because the system cannot guarantee that all the
1.135 + * buffers have been synchronized with physical media.
1.136 + * @since JDK1.1
1.137 + */
1.138 + public native void sync() throws SyncFailedException;
1.139 +
1.140 + /* This routine initializes JNI field offsets for the class */
1.141 + private static native void initIDs();
1.142 +
1.143 + static {
1.144 + initIDs();
1.145 + }
1.146 +
1.147 + // Set up JavaIOFileDescriptorAccess in SharedSecrets
1.148 + static {
1.149 + sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
1.150 + new sun.misc.JavaIOFileDescriptorAccess() {
1.151 + public void set(FileDescriptor obj, int fd) {
1.152 + obj.fd = fd;
1.153 + }
1.154 +
1.155 + public int get(FileDescriptor obj) {
1.156 + return obj.fd;
1.157 + }
1.158 +
1.159 + public void setHandle(FileDescriptor obj, long handle) {
1.160 + throw new UnsupportedOperationException();
1.161 + }
1.162 +
1.163 + public long getHandle(FileDescriptor obj) {
1.164 + throw new UnsupportedOperationException();
1.165 + }
1.166 + }
1.167 + );
1.168 + }
1.169 +
1.170 + // package private methods used by FIS, FOS and RAF
1.171 +
1.172 + int incrementAndGetUseCount() {
1.173 + return useCount.incrementAndGet();
1.174 + }
1.175 +
1.176 + int decrementAndGetUseCount() {
1.177 + return useCount.decrementAndGet();
1.178 + }
1.179 +}