public abstract class Channel extends Object
Represents Ballerina Byte Channel.
Allows reading/writing bytes to perform I/O operations.
The bytes are read through the channel into the buffer. This will manage the buffer and the channel to deliver bytes I/O reading/writing APIs.
Constructor and Description |
---|
Channel(ByteChannel channel,
Reader reader,
Writer writer)
Will read/write bytes from the provided channel
|
Channel(ByteChannel channel,
Reader reader,
Writer writer,
int size)
Creates a channel which will contain a fixed sized buffer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the given channel.
|
InputStream |
getInputStream()
This will return
InputStream from underlying ByteChannel . |
boolean |
hasReachedEnd()
Specifies whether the channel has reached to it's end.
|
int |
read(ByteBuffer buffer)
Async read bytes from the channel.
|
byte[] |
readFull(int numberOfBytes)
Reads specified amount of bytes from a given channel.
|
abstract void |
transfer(int position,
int count,
WritableByteChannel dstChannel)
Will be used when performing direct transfer operations from OS cache.
|
int |
write(ByteBuffer content)
Writes provided buffer content to the channel.
|
public Channel(ByteChannel channel, Reader reader, Writer writer) throws BallerinaIOException
Will read/write bytes from the provided channel
This operation will asynchronously read data from the channel.
channel
- which will be used to read/write content.reader
- will be used for reading content.writer
- will be used for writing content.BallerinaIOException
- initialization error.public Channel(ByteChannel channel, Reader reader, Writer writer, int size) throws BallerinaIOException
Creates a channel which will contain a fixed sized buffer.
This operation will in turn request for excess bytes than required. And buffer the content. Also this will mostly be used with blocking readers and writers.
channel
- which will be used to read/write content.reader
- will be used for reading content.writer
- will be used for writing content.size
- the size of the fixed buffer.BallerinaIOException
- initialization error.public abstract void transfer(int position, int count, WritableByteChannel dstChannel) throws IOException
position
- starting position of the bytes to be transferred.count
- number of bytes to be transferred.dstChannel
- destination channel to transfer.IOException
- during I/O error.public boolean hasReachedEnd()
public int read(ByteBuffer buffer) throws IOException
Async read bytes from the channel.
buffer
- the buffer which will hold the content.IOException
- errors occur during reading from channel.public int write(ByteBuffer content) throws IOException
Writes provided buffer content to the channel.
content
- the buffer which holds the content.IOException
- errors occur during writing data to channel.public InputStream getInputStream() throws BallerinaIOException
InputStream
from underlying ByteChannel
.InputStream
BallerinaIOException
- error occur during obtaining input-stream.public byte[] readFull(int numberOfBytes) throws IOException
Reads specified amount of bytes from a given channel.
Each time this method is called the data will be retrieved from the channels last read position.
Note : This operation cannot be called in parallel invocations since the underlying ByteBuffer and the channel are not synchronous.
numberOfBytes
- the number of bytes required.IOException
- during I/O error.public void close() throws IOException
IOException
- errors occur while closing the connection.Copyright © 2018 WSO2. All rights reserved.