BidirectionalStream

public abstract class BidirectionalStream
extends Object

java.lang.Object
   ↳ android.net.http.BidirectionalStream


Class for bidirectional sending and receiving of data over HTTP/2 or QUIC connections. Created by Builder. Note: There are ordering restrictions on methods of BidirectionalStream; please see individual methods for description of restrictions.

Summary

Nested classes

class BidirectionalStream.Builder

Builder for BidirectionalStreams. 

class BidirectionalStream.Callback

Callback class used to receive callbacks from a BidirectionalStream

Public constructors

BidirectionalStream()

Public methods

abstract void cancel()

Cancels the stream.

abstract void flush()

Flushes pending writes.

abstract boolean isDone()

Returns true if the stream was successfully started and is now done (succeeded, canceled, or failed).

abstract void read(ByteBuffer buffer)

Reads data from the stream into the provided buffer.

abstract void start()

Starts the stream, all callbacks go to the callback argument passed to BidirectionalStream.Builder's constructor.

abstract void write(ByteBuffer buffer, boolean endOfStream)

Attempts to write data from the provided buffer into the stream.

Inherited methods

Public constructors

BidirectionalStream

public BidirectionalStream ()

Public methods

cancel

public abstract void cancel ()

Cancels the stream. Can be called at any time after start(). onCanceled() will be invoked when cancelation is complete and no further callback methods will be invoked. If the stream has completed or has not started, calling cancel() has no effect and onCanceled() will not be invoked. If the Executor passed in during BidirectionalStream construction runs tasks on a single thread, and cancel() is called on that thread, no listener methods (besides onCanceled()) will be invoked after cancel() is called. Otherwise, at most one callback method may be invoked after cancel() has completed.

flush

public abstract void flush ()

Flushes pending writes. This method should not be invoked before onStreamReady(). For previously delayed write()s, a corresponding onWriteCompleted() will be invoked when the buffer is sent.

isDone

public abstract boolean isDone ()

Returns true if the stream was successfully started and is now done (succeeded, canceled, or failed).

Returns
boolean true if the stream was successfully started and is now done (completed, canceled, or failed), otherwise returns false to indicate stream is not yet started or is in progress.

read

public abstract void read (ByteBuffer buffer)

Reads data from the stream into the provided buffer. Can only be called at most once in response to each invocation of the onStreamReady()/ onResponseHeadersReceived() and onReadCompleted() methods of the Callback. Each call will result in an invocation of one of the Callback's onReadCompleted() method if data is read, or its onFailed() method if there's an error. An attempt to read data into buffer starting at buffer.position() is begun. At most buffer.remaining() bytes are read. buffer.position() is updated upon invocation of onReadCompleted() to indicate how much data was read.

Parameters
buffer ByteBuffer: the ByteBuffer to read data into. Must be a direct ByteBuffer. The embedder must not read or modify buffer's position, limit, or data between its position and limit until onReadCompleted(), onCanceled(), or onFailed() are invoked. This value cannot be null.

start

public abstract void start ()

Starts the stream, all callbacks go to the callback argument passed to BidirectionalStream.Builder's constructor. Should only be called once.

write

public abstract void write (ByteBuffer buffer, 
                boolean endOfStream)

Attempts to write data from the provided buffer into the stream. If auto flush is disabled, data will be sent only after flush() is called. Each call will result in an invocation of one of the Callback's onWriteCompleted() method if data is sent, or its onFailed() method if there's an error. An attempt to write data from buffer starting at buffer.position() is begun. buffer.remaining() bytes will be written. onWriteCompleted() will be invoked only when the full ByteBuffer is written.

Parameters
buffer ByteBuffer: the ByteBuffer to write data from. Must be a direct ByteBuffer. The embedder must not read or modify buffer's position, limit, or data between its position and limit until onWriteCompleted(), onCanceled(), or onFailed() are invoked. Can be empty when endOfStream is true. This value cannot be null.

endOfStream boolean: if true, then buffer is the last buffer to be written, and once written, stream is closed from the client side, resulting in half-closed stream or a fully closed stream if the remote side has already closed.