Stream INTERFACE

An abstraction of data origin/sink. A stream can support up to three types of operations: reading, writing and repositioning. The user may query these capabilities before using a stream, but can also opt to proceed without pre-checking if willing to handle the error.

An internal pointer is pointing to the position within the stream where the next operation will start from. If the stream doesn't support repositioning, this pointer will only move forward.

All Members


TypeNameSignature
methodcanMarkpublic bool canMark()
methodcanReadpublic bool canRead()
methodcanWritepublic bool canWrite()
methodclosepublic void close()
methodflushpublic void flush()
methodmarkpublic void mark()
methodreadpublic byte read()
methodreadpublic int read(byte[], int, int)
methodresetpublic void reset()
methodskippublic int skip(int)
methodwritepublic void write(byte)
methodwritepublic void write(byte[], int, int)

Methods


public bool canMark()

Whether this stream supports position-manipulating operations. This method governs mark, reset.

Returns

  • True if this stream supports position-manipulating operations.

public bool canRead()

Whether this stream supports reading. This method governs read, skip.

Returns

  • True if this stream supports reading.

public bool canWrite()

Whether this stream supports writing. This method governs write and other write methods.

Returns

  • True if this stream supports writing.

public void close()

Close the stream. Once the stream is closed it cannot be re-opened.


public void flush()

Flush the stream. This will materialize all the data copy which may so far have only occurred inside in-memory data buffer, which is usually an implementation detail of the stream. It's best practice for the user to call this method between writing calls, as well as at the conclusion of data copy logic, to ensure all the data have effectively flown into the sink.


public void mark()

Mark the current position of stream. Calling reset() will move the stream pointer to the marked position.

If canMark() return false, this method will throw IOException.


public byte read()

Read one byte from the stream.

This will move forward the stream pointer by one. If canRead() return false, this method will throw IOException.

Returns

  • The byte read from the stream. -1 if reaching the end.

public int read(byte[] buffer, int offset, int count)

Try to read desired number of bytes 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.

This method moves forward the stream pointer by the count equal to returned value. If canRead() return false, this method will throw IOException.

Parameters

  • buffer The byte buffer to hold the data to be read from the stream.
  • offset The offset on buffer to start filling the data in.
  • count The desired number of bytes to read.

Returns

  • The count of bytes read from the stream. -1 if reaching the end without any bytes read.

public void reset()

Move the stream pointer to the position marked by mark().

If canMark() return false, this method will throw IOException.


public int skip(int count)

Instead of reading, try to skip desired number of bytes from the stream. This will move forward the stream pointer by the number of bytes actually read. If the stream hits the end before reading the specified count, only those bytes will be skipped, making the returned value less than the count argument.

This method moves forward the stream pointer by the count equal to returned value. If canRead() return false, this method will throw IOException.

Parameters

  • count The desired number of bytes to skip.

Returns

  • The count of bytes skipped. -1 if reaching the end without any bytes skipped.

public void write(byte data)

Write one byte to the stream.

This will move forward the stream pointer by one. If canWrite() return false, this method will throw IOException.

Parameters

  • data The single byte to write to this stream.

public void write(byte[] buffer, int offset, int count)

Try to write desired number of bytes into the stream from 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 buffer hits the end before reading the specified count, only those bytes will be written, making the returned value less than the count argument.

This method moves forward the stream pointer by the count equal to returned value. If canWrite() return false, this method will throw IOException.

Parameters

  • buffer The byte buffer which holds the data to write into the stream.
  • offset The offset on the buffer from which the data will be pulled.
  • count The desired number of bytes to write.