FileDescriptor.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* FileDescriptor.java -- Opaque file handle class
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.io;
  33. import gnu.java.nio.FileChannelImpl;
  34. import java.nio.channels.ByteChannel;
  35. import java.nio.channels.FileChannel;
  36. /**
  37. * This class represents an opaque file handle as a Java class. It should
  38. * be used only to pass to other methods that expect an object of this
  39. * type. No system specific information can be obtained from this object.
  40. *
  41. * @author Aaron M. Renn (arenn@urbanophile.com)
  42. * @author Tom Tromey (tromey@cygnus.com)
  43. * @date September 24, 1998
  44. */
  45. public final class FileDescriptor
  46. {
  47. /**
  48. * A <code>FileDescriptor</code> representing the system standard input
  49. * stream. This will usually be accessed through the
  50. * <code>System.in</code>variable.
  51. */
  52. public static final FileDescriptor in
  53. = new FileDescriptor (FileChannelImpl.in);
  54. /**
  55. * A <code>FileDescriptor</code> representing the system standard output
  56. * stream. This will usually be accessed through the
  57. * <code>System.out</code>variable.
  58. */
  59. public static final FileDescriptor out
  60. = new FileDescriptor (FileChannelImpl.out);
  61. /**
  62. * A <code>FileDescriptor</code> representing the system standard error
  63. * stream. This will usually be accessed through the
  64. * <code>System.err</code>variable.
  65. */
  66. public static final FileDescriptor err
  67. = new FileDescriptor (FileChannelImpl.err);
  68. final ByteChannel channel;
  69. /**
  70. * This method is used to initialize an invalid FileDescriptor object.
  71. */
  72. public FileDescriptor()
  73. {
  74. channel = null;
  75. }
  76. /**
  77. * This method is used to initialize a FileDescriptor object.
  78. */
  79. FileDescriptor(ByteChannel channel)
  80. {
  81. this.channel = channel;
  82. }
  83. /**
  84. * This method forces all data that has not yet been physically written to
  85. * the underlying storage medium associated with this
  86. * <code>FileDescriptor</code>
  87. * to be written out. This method will not return until all data has
  88. * been fully written to the underlying device. If the device does not
  89. * support this functionality or if an error occurs, then an exception
  90. * will be thrown.
  91. */
  92. public void sync () throws SyncFailedException
  93. {
  94. if (channel instanceof FileChannel)
  95. {
  96. try
  97. {
  98. ((FileChannel) channel).force(true);
  99. }
  100. catch (IOException ex)
  101. {
  102. if (ex instanceof SyncFailedException)
  103. throw (SyncFailedException) ex;
  104. else
  105. throw new SyncFailedException(ex.toString());
  106. }
  107. }
  108. }
  109. /**
  110. * This methods tests whether or not this object represents a valid open
  111. * native file handle.
  112. *
  113. * @return <code>true</code> if this object represents a valid
  114. * native file handle, <code>false</code> otherwise
  115. */
  116. public boolean valid ()
  117. {
  118. ByteChannel c = channel;
  119. return (c != null) && (c.isOpen());
  120. }
  121. }