Java Methods of InputStream
The InputStream
class is an abstract class in Java that provides a standard way of reading data from different sources in the form of bytes. It defines several methods that are commonly used for reading data, including:
read()
- Reads a single byte of data from the input stream and returns it as an integer value between 0 and 255. Returns -1 if the end of the stream has been reached.read(byte[] b)
- Reads up tob.length
bytes of data from the input stream and stores them in the byte arrayb
. Returns the number of bytes actually read, or -1 if the end of the stream has been reached.read(byte[] b, int off, int len)
- Reads up tolen
bytes of data from the input stream and stores them in the byte arrayb
starting at the specified offsetoff
. Returns the number of bytes actually read, or -1 if the end of the stream has been reached.skip(long n)
- Skips over and discardsn
bytes of data from the input stream. Returns the number of bytes actually skipped.available()
- Returns the number of bytes of data that can be read from the input stream without blocking. This is an estimate and may not be exact.mark(int readlimit)
- Sets a mark in the input stream at the current position. The mark is used to allow the stream to be reset to this position later.reset()
- Resets the input stream to the last mark that was set using themark
method.markSupported()
- Returnstrue
if the input stream supports themark
andreset
methods.
It's important to note that the behavior of these methods can vary depending on the specific subclass of InputStream
that you are using. For example, the FileInputStream
class provides additional methods for working with files, while the ByteArrayInputStream
class is designed specifically for reading data from a byte array.
Overall, the InputStream
class provides a flexible and efficient way of reading data from different sources in Java, and the various methods it provides can be used to read data in a variety of different ways depending on your specific needs.