1. /*
  2. * @(#)FileDescriptor.java 1.16 01/11/29
  3. *
  4. * Copyright 2002 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.16, 11/29/01
  20. * @see java.io.FileInputStream
  21. * @see java.io.FileOutputStream
  22. * @see java.net.SocketInputStream
  23. * @see java.net.SocketOutputStream
  24. * @since JDK1.0
  25. */
  26. public final class FileDescriptor {
  27. private int fd;
  28. /**
  29. * Constructs an (invalid) FileDescriptor
  30. * object.
  31. */
  32. public /**/ FileDescriptor() {
  33. fd = -1;
  34. }
  35. private /* */ FileDescriptor(int fd) {
  36. this.fd = fd;
  37. }
  38. /**
  39. * A handle to the standard input stream. Usually, this file
  40. * descriptor is not used directly, but rather via the input stream
  41. * known as <code>System.in</code>.
  42. *
  43. * @see java.lang.System#in
  44. */
  45. public static final FileDescriptor in = new FileDescriptor(0);
  46. /**
  47. * A handle to the standard output stream. Usually, this file
  48. * descriptor is not used directly, but rather via the output stream
  49. * known as <code>System.out</code>.
  50. * @see java.lang.System#out
  51. */
  52. public static final FileDescriptor out = new FileDescriptor(1);
  53. /**
  54. * A handle to the standard error stream. Usually, this file
  55. * descriptor is not used directly, but rather via the output stream
  56. * known as <code>System.err</code>.
  57. *
  58. * @see java.lang.System#err
  59. */
  60. public static final FileDescriptor err = new FileDescriptor(2);
  61. /**
  62. * Tests if this file descriptor object is valid.
  63. *
  64. * @return <code>true</code> if the file descriptor object represents a
  65. * valid, open file, socket, or other active I/O connection;
  66. * <code>false</code> otherwise.
  67. */
  68. public boolean valid() {
  69. return fd != -1;
  70. }
  71. /**
  72. * Force all system buffers to synchronize with the underlying
  73. * device. This method returns after all modified data and
  74. * attributes of this FileDescriptor have been written to the
  75. * relevant device(s). In particular, if this FileDescriptor
  76. * refers to a physical storage medium, such as a file in a file
  77. * system, sync will not return until all in-memory modified copies
  78. * of buffers associated with this FileDesecriptor have been
  79. * written to the physical medium.
  80. *
  81. * sync is meant to be used by code that requires physical
  82. * storage (such as a file) to be in a known state For
  83. * example, a class that provided a simple transaction facility
  84. * might use sync to ensure that all changes to a file caused
  85. * by a given transaction were recorded on a storage medium.
  86. *
  87. * sync only affects buffers downstream of this FileDescriptor. If
  88. * any in-memory buffering is being done by the application (for
  89. * example, by a BufferedOutputStream object), those buffers must
  90. * be flushed into the FileDescriptor (for example, by invoking
  91. * OutputStream.flush) before that data will be affected by sync.
  92. *
  93. * @exception SyncFailedException
  94. * Thrown when the buffers cannot be flushed,
  95. * or because the system cannot guarantee that all the
  96. * buffers have been synchronized with physical media.
  97. * @since JDK1.1
  98. */
  99. public native void sync() throws SyncFailedException;
  100. /* This routine initializes JNI field offsets for the class */
  101. private static native void initIDs();
  102. static {
  103. initIDs();
  104. }
  105. }