/driver-core/src/main/com/mongodb/connection/Stream.java

https://github.com/foursquare/mongo-java-driver · Java · 99 lines · 16 code · 13 blank · 70 comment · 0 complexity · d3e7814aae9dd45e2017ee9fd36f5ec8 MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2014 MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.connection;
  17. import com.mongodb.ServerAddress;
  18. import org.bson.ByteBuf;
  19. import java.io.IOException;
  20. import java.util.List;
  21. /**
  22. * A full duplex stream of bytes.
  23. *
  24. * @since 3.0
  25. */
  26. public interface Stream extends BufferProvider{
  27. /**
  28. * Open the stream.
  29. *
  30. * @throws IOException if an I/O error occurs
  31. */
  32. void open() throws IOException;
  33. /**
  34. * Open the stream asynchronously.
  35. *
  36. * @param handler the completion handler for opening the stream
  37. */
  38. void openAsync(AsyncCompletionHandler<Void> handler);
  39. /**
  40. * Write each buffer in the list to the stream in order, blocking until all are completely written.
  41. *
  42. * @param buffers the buffers to write
  43. * @throws IOException if there are problems writing to the stream
  44. */
  45. void write(List<ByteBuf> buffers) throws IOException;
  46. /**
  47. * Read from the stream, blocking until the requested number of bytes have been read.
  48. *
  49. * @param numBytes The number of bytes to read into the returned byte buffer
  50. * @return a byte buffer filled with number of bytes requested
  51. * @throws IOException if there are problems reading from the stream
  52. */
  53. ByteBuf read(int numBytes) throws IOException;
  54. /**
  55. * Write each buffer in the list to the stream in order, asynchronously. This method should return immediately, and invoke the given
  56. * callback on completion.
  57. *
  58. * @param buffers the buffers to write
  59. * @param handler invoked when the read operation has completed
  60. */
  61. void writeAsync(List<ByteBuf> buffers, AsyncCompletionHandler<Void> handler);
  62. /**
  63. * Read from the stream, asynchronously. This method should return immediately, and invoke the given callback when the number of
  64. * requested bytes have been read.
  65. *
  66. * @param numBytes the number of bytes
  67. * @param handler invoked when the read operation has completed
  68. */
  69. void readAsync(int numBytes, AsyncCompletionHandler<ByteBuf> handler);
  70. /**
  71. * The address that this stream is connected to.
  72. *
  73. * @return the address
  74. */
  75. ServerAddress getAddress();
  76. /**
  77. * Closes the connection.
  78. */
  79. void close();
  80. /**
  81. * Returns the closed state of the connection
  82. *
  83. * @return true if connection is closed
  84. */
  85. boolean isClosed();
  86. }