PageRenderTime 60ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/src/windows/classes/java/io/FileDescriptor.java

https://github.com/ikeji/openjdk7-jdk
Java | 180 lines | 53 code | 24 blank | 103 comment | 3 complexity | d5e39ff7f883b954354ac2b69f23d919 MD5 | raw file
  1. /*
  2. * Copyright (c) 2003, 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. package java.io;
  26. import java.util.concurrent.atomic.AtomicInteger;
  27. /**
  28. * Instances of the file descriptor class serve as an opaque handle
  29. * to the underlying machine-specific structure representing an
  30. * open file, an open socket, or another source or sink of bytes.
  31. * The main practical use for a file descriptor is to create a
  32. * {@link FileInputStream} or {@link FileOutputStream} to contain it.
  33. *
  34. * <p>Applications should not create their own file descriptors.
  35. *
  36. * @author Pavani Diwanji
  37. * @since JDK1.0
  38. */
  39. public final class FileDescriptor {
  40. private int fd;
  41. private long handle;
  42. /**
  43. * A use counter for tracking the FIS/FOS/RAF instances that
  44. * use this FileDescriptor. The FIS/FOS.finalize() will not release
  45. * the FileDescriptor if it is still under use by any stream.
  46. */
  47. private AtomicInteger useCount;
  48. /**
  49. * Constructs an (invalid) FileDescriptor
  50. * object.
  51. */
  52. public /**/ FileDescriptor() {
  53. fd = -1;
  54. handle = -1;
  55. useCount = new AtomicInteger();
  56. }
  57. static {
  58. initIDs();
  59. }
  60. // Set up JavaIOFileDescriptorAccess in SharedSecrets
  61. static {
  62. sun.misc.SharedSecrets.setJavaIOFileDescriptorAccess(
  63. new sun.misc.JavaIOFileDescriptorAccess() {
  64. public void set(FileDescriptor obj, int fd) {
  65. obj.fd = fd;
  66. }
  67. public int get(FileDescriptor obj) {
  68. return obj.fd;
  69. }
  70. public void setHandle(FileDescriptor obj, long handle) {
  71. obj.handle = handle;
  72. }
  73. public long getHandle(FileDescriptor obj) {
  74. return obj.handle;
  75. }
  76. }
  77. );
  78. }
  79. /**
  80. * A handle to the standard input stream. Usually, this file
  81. * descriptor is not used directly, but rather via the input stream
  82. * known as {@code System.in}.
  83. *
  84. * @see java.lang.System#in
  85. */
  86. public static final FileDescriptor in = standardStream(0);
  87. /**
  88. * A handle to the standard output stream. Usually, this file
  89. * descriptor is not used directly, but rather via the output stream
  90. * known as {@code System.out}.
  91. * @see java.lang.System#out
  92. */
  93. public static final FileDescriptor out = standardStream(1);
  94. /**
  95. * A handle to the standard error stream. Usually, this file
  96. * descriptor is not used directly, but rather via the output stream
  97. * known as {@code System.err}.
  98. *
  99. * @see java.lang.System#err
  100. */
  101. public static final FileDescriptor err = standardStream(2);
  102. /**
  103. * Tests if this file descriptor object is valid.
  104. *
  105. * @return {@code true} if the file descriptor object represents a
  106. * valid, open file, socket, or other active I/O connection;
  107. * {@code false} otherwise.
  108. */
  109. public boolean valid() {
  110. return ((handle != -1) || (fd != -1));
  111. }
  112. /**
  113. * Force all system buffers to synchronize with the underlying
  114. * device. This method returns after all modified data and
  115. * attributes of this FileDescriptor have been written to the
  116. * relevant device(s). In particular, if this FileDescriptor
  117. * refers to a physical storage medium, such as a file in a file
  118. * system, sync will not return until all in-memory modified copies
  119. * of buffers associated with this FileDesecriptor have been
  120. * written to the physical medium.
  121. *
  122. * sync is meant to be used by code that requires physical
  123. * storage (such as a file) to be in a known state For
  124. * example, a class that provided a simple transaction facility
  125. * might use sync to ensure that all changes to a file caused
  126. * by a given transaction were recorded on a storage medium.
  127. *
  128. * sync only affects buffers downstream of this FileDescriptor. If
  129. * any in-memory buffering is being done by the application (for
  130. * example, by a BufferedOutputStream object), those buffers must
  131. * be flushed into the FileDescriptor (for example, by invoking
  132. * OutputStream.flush) before that data will be affected by sync.
  133. *
  134. * @exception SyncFailedException
  135. * Thrown when the buffers cannot be flushed,
  136. * or because the system cannot guarantee that all the
  137. * buffers have been synchronized with physical media.
  138. * @since JDK1.1
  139. */
  140. public native void sync() throws SyncFailedException;
  141. /* This routine initializes JNI field offsets for the class */
  142. private static native void initIDs();
  143. private static native long set(int d);
  144. private static FileDescriptor standardStream(int fd) {
  145. FileDescriptor desc = new FileDescriptor();
  146. desc.handle = set(fd);
  147. return desc;
  148. }
  149. // package private methods used by FIS, FOS and RAF.
  150. int incrementAndGetUseCount() {
  151. return useCount.incrementAndGet();
  152. }
  153. int decrementAndGetUseCount() {
  154. return useCount.decrementAndGet();
  155. }
  156. }