1. /*
  2. * @(#)Pipe.java 1.20 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.nio.channels;
  8. import java.io.IOException;
  9. import java.nio.channels.spi.*;
  10. /**
  11. * A pair of channels that implements a unidirectional pipe.
  12. *
  13. * <p> A pipe consists of a pair of channels: A writable {@link
  14. * Pipe.SinkChannel </code>sink<code>} channel and a readable {@link
  15. * Pipe.SourceChannel </code>source<code>} channel. Once some bytes are
  16. * written to the sink channel they can be read from source channel in exactly
  17. * the order in which they were written.
  18. *
  19. * <p> Whether or not a thread writing bytes to a pipe will block until another
  20. * thread reads those bytes, or some previously-written bytes, from the pipe is
  21. * system-dependent and therefore unspecified. Many pipe implementations will
  22. * buffer up to a certain number of bytes between the sink and source channels,
  23. * but such buffering should not be assumed. </p>
  24. *
  25. *
  26. * @author Mark Reinhold
  27. * @author JSR-51 Expert Group
  28. * @version 1.20, 03/12/19
  29. * @since 1.4
  30. */
  31. public abstract class Pipe {
  32. /**
  33. * A channel representing the readable end of a {@link Pipe}. </p>
  34. *
  35. * @since 1.4
  36. */
  37. public static abstract class SourceChannel
  38. extends AbstractSelectableChannel
  39. implements ReadableByteChannel, ScatteringByteChannel
  40. {
  41. /**
  42. * Constructs a new instance of this class.
  43. */
  44. protected SourceChannel(SelectorProvider provider) {
  45. super(provider);
  46. }
  47. /**
  48. * Returns an operation set identifying this channel's supported
  49. * operations.
  50. *
  51. * <p> Pipe-source channels only support reading, so this method
  52. * returns {@link SelectionKey#OP_READ}. </p>
  53. *
  54. * @return The valid-operation set
  55. */
  56. public final int validOps() {
  57. return SelectionKey.OP_READ;
  58. }
  59. }
  60. /**
  61. * A channel representing the writable end of a {@link Pipe}. </p>
  62. *
  63. * @since 1.4
  64. */
  65. public static abstract class SinkChannel
  66. extends AbstractSelectableChannel
  67. implements WritableByteChannel, GatheringByteChannel
  68. {
  69. /**
  70. * Initializes a new instance of this class.
  71. */
  72. protected SinkChannel(SelectorProvider provider) {
  73. super(provider);
  74. }
  75. /**
  76. * Returns an operation set identifying this channel's supported
  77. * operations.
  78. *
  79. * <p> Pipe-sink channels only support writing, so this method returns
  80. * {@link SelectionKey#OP_WRITE}. </p>
  81. *
  82. * @return The valid-operation set
  83. */
  84. public final int validOps() {
  85. return SelectionKey.OP_WRITE;
  86. }
  87. }
  88. /**
  89. * Initializes a new instance of this class.
  90. */
  91. protected Pipe() { }
  92. /**
  93. * Returns this pipe's source channel. </p>
  94. *
  95. * @return This pipe's source channel
  96. */
  97. public abstract SourceChannel source();
  98. /**
  99. * Returns this pipe's sink channel. </p>
  100. *
  101. * @return This pipe's sink channel
  102. */
  103. public abstract SinkChannel sink();
  104. /**
  105. * Opens a pipe.
  106. *
  107. * <p> The new pipe is created by invoking the {@link
  108. * java.nio.channels.spi.SelectorProvider#openPipe openPipe} method of the
  109. * system-wide default {@link java.nio.channels.spi.SelectorProvider}
  110. * object. </p>
  111. *
  112. * @return A new pipe
  113. *
  114. * @throws IOException
  115. * If an I/O error occurs
  116. */
  117. public static Pipe open() throws IOException {
  118. return SelectorProvider.provider().openPipe();
  119. }
  120. }