/*
 * Copyright 2008-present MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb.connection;

import com.mongodb.ServerAddress;
import org.bson.ByteBuf;

import java.io.IOException;
import java.util.List;

A full duplex stream of bytes.
Since:3.0
/** * A full duplex stream of bytes. * * @since 3.0 */
public interface Stream extends BufferProvider{
Open the stream.
Throws:
  • IOException – if an I/O error occurs
/** * Open the stream. * * @throws IOException if an I/O error occurs */
void open() throws IOException;
Open the stream asynchronously.
Params:
  • handler – the completion handler for opening the stream
/** * Open the stream asynchronously. * * @param handler the completion handler for opening the stream */
void openAsync(AsyncCompletionHandler<Void> handler);
Write each buffer in the list to the stream in order, blocking until all are completely written.
Params:
  • buffers – the buffers to write
Throws:
  • IOException – if there are problems writing to the stream
/** * Write each buffer in the list to the stream in order, blocking until all are completely written. * * @param buffers the buffers to write * @throws IOException if there are problems writing to the stream */
void write(List<ByteBuf> buffers) throws IOException;
Read from the stream, blocking until the requested number of bytes have been read.
Params:
  • numBytes – The number of bytes to read into the returned byte buffer
Throws:
  • IOException – if there are problems reading from the stream
Returns:a byte buffer filled with number of bytes requested
/** * Read from the stream, blocking until the requested number of bytes have been read. * * @param numBytes The number of bytes to read into the returned byte buffer * @return a byte buffer filled with number of bytes requested * @throws IOException if there are problems reading from the stream */
ByteBuf read(int numBytes) throws IOException;
Write each buffer in the list to the stream in order, asynchronously. This method should return immediately, and invoke the given callback on completion.
Params:
  • buffers – the buffers to write
  • handler – invoked when the read operation has completed
/** * Write each buffer in the list to the stream in order, asynchronously. This method should return immediately, and invoke the given * callback on completion. * * @param buffers the buffers to write * @param handler invoked when the read operation has completed */
void writeAsync(List<ByteBuf> buffers, AsyncCompletionHandler<Void> handler);
Read from the stream, asynchronously. This method should return immediately, and invoke the given callback when the number of requested bytes have been read.
Params:
  • numBytes – the number of bytes
  • handler – invoked when the read operation has completed
/** * Read from the stream, asynchronously. This method should return immediately, and invoke the given callback when the number of * requested bytes have been read. * * @param numBytes the number of bytes * @param handler invoked when the read operation has completed */
void readAsync(int numBytes, AsyncCompletionHandler<ByteBuf> handler);
The address that this stream is connected to.
Returns:the address
/** * The address that this stream is connected to. * * @return the address */
ServerAddress getAddress();
Closes the connection.
/** * Closes the connection. */
void close();
Returns the closed state of the connection
Returns:true if connection is closed
/** * Returns the closed state of the connection * * @return true if connection is closed */
boolean isClosed(); }