ComputersProgramming

Java: InputStream. Input Streams

Java, like many modern programming languages, supports the implementation of data entry streams. In Java, InputStream is the base class for byte streams. This class is abstract, that is, in the process of the program we can not create its instance. However, in the io package, there are many classes that inherit and extend the functionality of the InputStream. To use this class in your code, you need to import it from the java.io.InputStream package. Next, we'll look at the basic functionality provided by the InputStream class, as well as the main classes that implement it.

Methods for the InputStream class

Before proceeding to 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 closing the thread, it must be closed. This is done using the close () method. Since most methods of the InputStream class generate an exception of type IOException in the event of an error, all operations must be performed in the try block, and the close () method should be rendered to the finally block to work 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 fire automatically and not add a finally block to your code. If you are using a seventh or later version of Java, you can put the InputStream into a so-called try with resources, which takes care of all the closing operations.

Consider the main methods of the InputStream class:

  • Int available () - Returns the number of bytes available for reading;
  • Int read () - takes from the resource the current byte and returns it in the integer representation; If all the bytes are read, returns -1;
  • Int read (byte [] buffer) - reads the available bytes in the specified buffer as an array of type byte (the number of bytes read is equal to the size of the specified buffer, returns the number of bytes that were read, returns -1 if all available bytes are read);
  • Int read (byte [] buffer, int offset, int number of bytes) - overload of the previous method, does the same, but with the position specified in the "offset" and reads as many as 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.

InputStream is implemented by several classes, designed to work with different sources and data types. The tree of inheritance is presented below.

Reading Files

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

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

Reading primitive data types

The previous example describes working with byte values that can be interpreted as symbols. But what if we need to read an integer, fractional or logical value? For this, in Java InputStream is indirectly expanded by the DataInputStream class. This class is the wrapper for the InputStream, which is passed to it when it is created as the constructor argument. This kind of stream reads the 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.

The names of the methods speak for themselves - each of them is intended for reading a certain type of data.

Buffered input stream

The buffered data read stream is implemented by the BufferedInputStream class in Java. The inputStream is wrapped in this class. This class complements the flow with a buffer, which allows you to read more than one byte at a time. This makes it possible to significantly improve the performance of the operation. BufferedInputStream is an indirect descendant of the 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 one sets the default buffer size, and in the second one it is possible to set it manually.

Conclusion

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

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.atomiyme.com. Theme powered by WordPress.