InputStream Class

Start course
Overview
Difficulty
Beginner
Duration
1h 46m
Students
26
Description

In this course, we'll learn about the Java Input/Output subject.

Learning Objectives

  • API and Java I/O
  • OutputStream Class
  • InputStream Class
  • Reader Class
  • Writer Class
  • Serialization, Deserialization, and Non-Serializable objects

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll learn about the Java InputStream and its methods with the help of an example. The InputStream class is used to get data from the data source to the program. Moves data in bytes. This class is included in the java.io package. It's an abstract class. Since it's abstract, it cannot be used directly. But we can use this class thanks to the subclasses of this class. This abstract class is the super class of all classes representing an InputStream of bytes. An InputStream accepts input bytes and gets them to the program. The main subclasses of the most known and used to OutputStream class are: FileInputStream, AudioInputStream, ByteArrayInputStream, FilterInputStream, ObjectInputStream, StringBufferInputStream etc. 

Now let's look at the useful methods of this class. The first method is the read method. With the read method, we can read any data from a file. There are three types of this method. The first one is the parameter list read method. This method reads the next byte of data from the InputStream. The value byte is returned as an int in the range of 0-255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available. The end of the stream is detected or an exception is thrown. The second one is again the read method with a single parameter, but this time it takes a byte array as a parameter. This method reads some numbers of bytes from the InputStream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. 

This method blocks until input data is available, the end of the file is detected, or an exception is thrown. If the length of b is zero, then no bytes are read and zero is returned. Otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned, otherwise at least one byte is read and stored into b. And the last one is the three parameters read method. The type of the parameter is the byte array. The type of the second and third parameters are int. The first parameter represents the buffer into which the data is read. The second parameter represents the start offset in array b at which the data is written. And the third parameter represents the maximum numbers of bytes to read. This method reads up to len bytes of data from the InputStream into an array of bytes. 

An attempt is made to read as many len bytes, but a smaller number maybe read. The number of bytes actually read is returned as an integer. This method blocks until input data is available, the end of the file is detected, or an exception is thrown. If len is zero, then no bytes are read and zero is returned. Otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned. Otherwise, at least one byte is read and stored into b. Also, all types of the read method throws the I/O exception if an I/O error occurs. For this, when we use this method, we should use the try and catch block. Now, let's look at the second method. 

The second method is the skip method with the parameter type of long. This method skips over and discards n bytes of data from the input stream. The actual number of bytes skipped is returned. If n is negative, the skip method for class InputStream always return zero and no bytes are skipped. Also, this method throws I/O exception if the stream does not support seek, or if some other IO error occurs. The other method is the available method. This method returns an estimate of the number of bytes that can be read or skipped over from the input stream without blocking by the next invocation of a method for the input stream. Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It's never correct to use the return value of this method to allocate a buffer intended to hold all data in the stream. And the other method is the close method. It closes the input stream and releases any system resources associated with the stream. 

Thus, it increases the system efficiency. Also, a closed stream cannot perform input operations and cannot be reopened. The close method of InputStream does nothing. For this reason, if you don't use this method, you don't get any errors. But from the perspective of the system efficiency, we use the close method. If you remember, we were calling the close method after using the scanner class because the system.in that we gave as a parameter to the scanner class is actually a part of InputStream. Yes, the InputStream is basically like that. Let's take a short break here. In our next lesson, we'll read data from a file on the device using the InputStream class.

 

About the Author
Students
512
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics