The asynchronous stream interface.
The methods declared in Stream are synchronous. This interface provides an alternative. Instead of blocking the thread and waiting until I/O devices return, the asynchronous methods return a promise immediately, so that their caller may continue to execute other code and go back to check the promise anytime later. The promise wraps a continuation that will be called back upon the completion of I/O operation.
See Also
Type | Name | Signature |
---|---|---|
method | canReadAsync | public bool canReadAsync() |
method | canWriteAsync | public bool canWriteAsync() |
method | readAsync | public Promise readAsync(byte[], int) |
method | readToEndAsync | public Promise readToEndAsync(byte[], Function) |
method | writeAsync | public Promise writeAsync(byte[], int, int) |
public bool canReadAsync()
Whether this stream supports asynchronous reading. This method governs readAsync, readAllAsync.
Returns
public bool canWriteAsync()
Whether this stream supports asynchronous writing. This method governs writeAsync.
Returns
public Promise readAsync(byte[] buffer, int offset)
Read asynchronously from the stream to the buffer, and invokes callback upon completion.
This method tries to read as many bytes as possible from the stream into the given buffer, starting from the offset and not exceeding the buffer's capacity. This will move forward the stream pointer by the number of bytes actually read. If the stream hits the end, or the buffer runs short of room before reading the specified count, only those bytes will be read, making the returned value less than the count argument.
Upon successful completion, the callback function will be invoked. The callback function has signature Function(int, PromiseHandle)
, with first parameter indicating the number of bytes successfully read. If the reading failed, this callback won't be called, and the users must process that with a continuation on the promise itself.
This method moves forward the stream pointer by the count equal to returned value. If canRead() returns false, this method will throw IOException.
Parameters
Returns
public Promise readToEndAsync(byte[] buffer, Function callback)
Read asynchronously from the stream to the buffer until the end of stream is hit, and invoke callback evertime a chunk of data is read, which usually fills the buffer, except for the last time.
This method tries to read as many bytes as possible from the stream into the given buffer. Everytime it reads a chunk of data, it will invoke the callback function, which has signature Function(int, PromiseHandle)
, with first parameter indicating the number of bytes successfully read. If the reading failed, this callback won't be called, and the users must process that with a continuation on the promise itself.
This method moves forward the stream pointer to the end of stream. If canRead() return false, this method will throw IOException.
Parameters
Function(int, PromiseHandle)
, first parameter indicating the number of bytes successfully read. -1 if reaching the end, while buffer will contain no valid bytes. Throughout the promise's life cycle this callback will be invoked multiple times.Returns
public Promise writeAsync(byte[] buffer, int offset, int length)
Write asynchronously from the buffer to the stream.
This method tries to write all the bytes from the given buffer to the stream, starting from the offset and not exceeding either length or the buffer's capacity. This will move forward the stream pointer by the number of bytes actually written.
This method moves forward the stream pointer by the count equal to value used to settle the promise. If canWriteAsync() return false, this method will throw IOException.
Parameters
Returns