Java Methods of InputStream

www.igi‮tf‬idea.com

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:

  1. 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.

  2. read(byte[] b) - Reads up to b.length bytes of data from the input stream and stores them in the byte array b. Returns the number of bytes actually read, or -1 if the end of the stream has been reached.

  3. read(byte[] b, int off, int len) - Reads up to len bytes of data from the input stream and stores them in the byte array b starting at the specified offset off. Returns the number of bytes actually read, or -1 if the end of the stream has been reached.

  4. skip(long n) - Skips over and discards n bytes of data from the input stream. Returns the number of bytes actually skipped.

  5. 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.

  6. 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.

  7. reset() - Resets the input stream to the last mark that was set using the mark method.

  8. markSupported() - Returns true if the input stream supports the mark and reset 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.