1. /*
  2. * @(#)FileDescriptor.java 1.3 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. /**
  9. * Instances of the file descriptor class serve as an opaque handle
  10. * to the underlying machine-specific structure representing an open
  11. * file, an open socket, or another source or sink of bytes. The
  12. * main practical use for a file descriptor is to create a
  13. * <code>FileInputStream</code> or <code>FileOutputStream</code> to
  14. * contain it.
  15. * <p>
  16. * Applications should not create their own file descriptors.
  17. *
  18. * @author Pavani Diwanji
  19. * @version 1.3, 12/19/03
  20. * @see java.io.FileInputStream
  21. * @see java.io.FileOutputStream
  22. * @since JDK1.0
  23. */
  24. public final class FileDescriptor {
  25. private int fd;
  26. private long handle;
  27. /**
  28. * Constructs an (invalid) FileDescriptor
  29. * object.
  30. */
  31. public /**/ FileDescriptor() {
  32. fd = -1;
  33. handle = -1;
  34. }
  35. private /* */ FileDescriptor(int fd) {
  36. this.fd = fd;
  37. handle = -1;
  38. }
  39. static {
  40. initIDs();
  41. }
  42. /**
  43. * A handle to the standard input stream. Usually, this file
  44. * descriptor is not used directly, but rather via the input stream
  45. * known as <code>System.in</code>.
  46. *
  47. * @see java.lang.System#in
  48. */
  49. public static final FileDescriptor in = standardStream(0);
  50. /**
  51. * A handle to the standard output stream. Usually, this file
  52. * descriptor is not used directly, but rather via the output stream
  53. * known as <code>System.out</code>.
  54. * @see java.lang.System#out
  55. */
  56. public static final FileDescriptor out = standardStream(1);
  57. /**
  58. * A handle to the standard error stream. Usually, this file
  59. * descriptor is not used directly, but rather via the output stream
  60. * known as <code>System.err</code>.
  61. *
  62. * @see java.lang.System#err
  63. */
  64. public static final FileDescriptor err = standardStream(2);
  65. /**
  66. * Tests if this file descriptor object is valid.
  67. *
  68. * @return <code>true</code> if the file descriptor object represents a
  69. * valid, open file, socket, or other active I/O connection;
  70. * <code>false</code> otherwise.
  71. */
  72. public boolean valid() {
  73. return ((handle != -1) || (fd != -1));
  74. }
  75. /**
  76. * Force all system buffers to synchronize with the underlying
  77. * device. This method returns after all modified data and
  78. * attributes of this FileDescriptor have been written to the
  79. * relevant device(s). In particular, if this FileDescriptor
  80. * refers to a physical storage medium, such as a file in a file
  81. * system, sync will not return until all in-memory modified copies
  82. * of buffers associated with this FileDesecriptor have been
  83. * written to the physical medium.
  84. *
  85. * sync is meant to be used by code that requires physical
  86. * storage (such as a file) to be in a known state For
  87. * example, a class that provided a simple transaction facility
  88. * might use sync to ensure that all changes to a file caused
  89. * by a given transaction were recorded on a storage medium.
  90. *
  91. * sync only affects buffers downstream of this FileDescriptor. If
  92. * any in-memory buffering is being done by the application (for
  93. * example, by a BufferedOutputStream object), those buffers must
  94. * be flushed into the FileDescriptor (for example, by invoking
  95. * OutputStream.flush) before that data will be affected by sync.
  96. *
  97. * @exception SyncFailedException
  98. * Thrown when the buffers cannot be flushed,
  99. * or because the system cannot guarantee that all the
  100. * buffers have been synchronized with physical media.
  101. * @since JDK1.1
  102. */
  103. public native void sync() throws SyncFailedException;
  104. /* This routine initializes JNI field offsets for the class */
  105. private static native void initIDs();
  106. private static native long set(int d);
  107. private static FileDescriptor standardStream(int fd) {
  108. FileDescriptor desc = new FileDescriptor();
  109. desc.handle = set(fd);
  110. return desc;
  111. }
  112. }