Java: InputStream. Input streams

Java, like many modern programming languages, supports the implementation of data input streams. In Java, an InputStream is the base class for byte streams. This class is abstract, that is, in the process of the program, we cannot create an instance of it. However, the io package has many classes that inherit and extend the functionality of InputStream. To use this class in your code, you need to import it from the java.io.InputStream package. Next, we look at the basic functionality that the InputStream class provides, as well as the main classes that implement it.

Methods of the InputStream Class

Before starting a detailed study of the methods of the InputStream class, it should be mentioned that it implements the Closeable and AutoCloseable interfaces. The Closeable interface tells us that when you finish working with a stream, you must close it. This is done using the close () method. Since most of the methods of the InputStream class throw an IOException in the event of an error, all operations must be carried out in the try block, and the close () method must be put in the finally block so that it works regardless of the result of the work in the try body.

The AutoCloseable interface significantly reduces the amount of technical code, because it allows the close () method to work automatically and not add a finally block to your code. If you use the seventh or later version of Java, an InputStream can be placed in a so-called try with resources, which takes care of all the operations to close the threads.

java inputstream to string

Consider the basic methods of the InputStream class:

  • int available () - returns the number of bytes available for reading;
  • int read () - takes the current byte from the resource and returns it in an integer representation; if all bytes are read, returns -1;
  • int read (byte [] buffer) - reads the available bytes into the specified buffer as an array of type byte (the number of bytes read is equal to the size of the given buffer; returns the number of bytes that could be read; if all available bytes are read, it returns -1);
  • int read (byte [] buffer, int offset, int number of bytes) - overloading the previous method, does the same, but from the position specified in the "offset" and reads as much as is specified in the "number of bytes";
  • long skip (long number of bytes) - skips the specified number of bytes and returns the actual number of bytes skipped.

InputStream is implemented by several classes designed to work with different sources and data types. Below is the inheritance tree.

java inputstream

Reading files

A byte stream for reading information from files is implemented by the FileInputStream class. In order to open a file for reading bytes, it is enough to create an instance of this class, passing its name to the constructor as an argument. If the file with the passed name does not exist, an exception of type FileNotFoundException will be thrown.

In case of a successful file opening, work with it is performed using the methods described above, since FileInputStream is an inheritor of InputStream.

Reading primitive data types

The previous example described working with byte values ​​that can be interpreted as characters. But what if we need to read an integer, fractional, or logical value? To do this, in Java, an InputStream is indirectly extended by the DataInputStream class. This class is a wrapper for an InputStream, which is passed to it when created as an argument to the constructor. This kind of stream reads data in binary form.

DataInputStream also implements the DataInput interface and its methods for reading primitive data types. Below is a list of these methods.

java io inputstream

The names of the methods speak for themselves - each of them is designed to read a certain type of data.

Buffered input stream

A buffered data read stream is implemented by the BufferedInputStream class in Java. An InputStream is wrapped by this class. This class supplements the stream with a buffer, which allows you to read at the same time not one, but several bytes. This makes it possible to significantly increase the productivity of the operation. BufferedInputStream is an indirect descendant of InputStream and, accordingly, inherits all the above methods.

This class has two constructors:

  • BufferedInputStream (InputStream input stream);
  • BufferedInputStream (InputStream input stream, int buffer size)

From the signature of the constructors, you can understand that the first sets the default buffer size, and the second has the ability to set it manually.

Conclusion

We examined the main implementations of InputStream. If you need to convert the received bytes to a string, then in Java InputStream to String (), unfortunately, is not overloaded, so you will have to use special utilities, for example, IOUtils from the Apache Commons library.

Source: https://habr.com/ru/post/K13239/


All Articles