Byte source is similar to InputStream, but is defined as an interface for allowing stream operations over RMI. RMI solutions can create proxy objects for network transferred objects only for interfaces, therefore it was necessary to declare such an interface that is usable for this use-case.
This interface works in similar ways as the InputStream class, but is designed to be RMI compatible. The
interface also contains extra method(s) for more efficient implementations of common use-cases (like
writeTo(
To convert between InputStream and ByteSource objects use the static methods declared in this interface.
Byte source implementations are not thread-safe by default.
public default void | close() Closes this stream and releases any system resources associated
with it. |
public default int | read() Reads a single byte from this byte source. |
public default ByteArrayRegion | read( Reads a given number of bytes from this byte source. |
public int | read( Reads bytes from this byte source and writes them to the argument buffer. |
public static int | redirectReadCall( RMI redirection method when read( |
public default long | skip( Skips over a number of bytes in this byte source. |
public static InputStream | toInputStream( Converts the argument ByteSource to an InputStream. |
public static ByteSource | valueOf( Converts the argument InputStream to a ByteSource. |
public static ByteSource | valueOf( Converts the argument ObjectInput to a ByteSource. |
public default long | Writes the remaining bytes in this byte source to the specified byte sink. |
As noted in AutoCloseable.close(), cases where the
close may fail require careful attention. It is strongly advised
to relinquish the underlying resources and to internally
mark the Closeable
as closed, prior to throwing
the IOException
.
This method works similarly to InputStream.read().
This method reads at most the specified number of bytes from this byte source and returns it as a byte array region. The resulting buffer may contain less than the requested number of bytes. This doesn't mean that the end of the stream has been reached, but only that reading more bytes will most likely block.
If a byte array of zero length is returned, that means that the end of the stream is reached (or the requested count was less or equal to 0).
It is recommended that the number of bytes to read is not too large, so a buffer allocated for that size will not cause OutOfMemoryError.
The number of bytes read is based on the buffer length.
This method works similarly to InputStream.read(
RMI method calls to this method is redirected to redirectReadCall(
null
.
This method is used to improve the performance of the associated method. If read(
This redirection method will call read(
During the operation of this method a temporary buffer will be allocated that has at most the above size, and which is discarded after this method ends. This method redirection servers as a tradeoff between memory and network calls. We deem that it is acceptable, as another allocation of the same (or smaller) size of the argument buffer should not hinder or crash the running JVM, but multiple network calls can decrease performance more.
If users are trying to read more bytes than the abot 64MB default limit, then they will need to call the
read(
null
.
This method will discard given amount of bytes from this byte source. This works the same way as calling
read() n
times.
This method works similarly to InputStream.skip(
This method may skip over 0 bytes even if there are still bytes available from this source. It may be recommended that in order to skip an exact amount of bytes, that external mechanisms are used to ensure valid operation.
If the argument is already an InputStream, then it will be returned without modification.
Else it will be wrapped into a forwarding InputStream.
Closing the result will close the argument too.
null
if the argument is
null
.If the argument is already a ByteSource, then it will be returned without modification.
Else it will be wrapped into a forwarding ByteSource.
Closing the result will close the argument too.
null
if the argument is
null
.If the argument is already a ByteSource, then it will be returned without modification.
Else it will be wrapped into a forwarding ByteSource.
Closing the result will close the argument too.
null
if the argument is
null
.This method will take the remaining bytes in this byte source and write it to the specified byte sink. It is expected that after this method finishes, reading from this byte source will not return any bytes.
null
.