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