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