Filter Streams   «Prev  Next»

Lesson 2What are filter streams?
ObjectiveExplore the purpose and use of Filter Streams.

What are Filter Streams in Java?

In the last several modules, we treated streams as sequences of raw bytes with no particular meaning.
Generally this is not perfectly true. The bytes sent over a stream have meanings. Sometimes they mean letters like A or B. Other times they mean numbers like 1,024 or 3.141529.
It is not difficult to write code that converts an appropriate number of bytes into an integer, a floating point number, or a Unicode character. Nonetheless, doing so is so common that the class library includes classes that perform these conversions in the java.io package.
There are two kinds of interpreter classes in the java.io package:
  1. filter streams
  2. readers and writers

Filter streams interpret bytes as numeric data and Readers and writers interpret bytes as textual data. We will not cover readers and writers in this course. They are covered in the next course in the Networking with Java series, Java Readers and Writers.


Water Filter and Java Streams Analogy

A water filter sits between the pipe and faucet, pulling out impurities. A stream filter sits between the source of the data and its eventual destination, and modifies that data according to a specific algorithm. As drops of water are passed through the water filter and modified, so too are bytes of data passed through the stream filter and modified. Do not try to stretch this analogy too far or you will break it, but I do want you to see where the idea of filters comes from.
Of course, there are some big differences, namely a stream filter can add data or some other kind of annotation to the stream, in addition to removing things you do not want; it may even produce a stream that is completely different from its original input (for example, by compressing the original data).
public class DataInputStream extends FilterInputStream implements DataInput

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way.
An application uses a data output stream to write data that can later be read by a data input stream.
DataInputStream is not necessarily safe for multithreaded access. Thread safety is optional and is the responsibility of users of methods in this class.

Java Lambdas and Parallel Streams
The java.io.FilterInputStream and java.io.FilterOutputStream classes are concrete subclasses for input and output stream superclasses that somehow modify or manipulate data read from an underlying stream. You rarely use these classes directly; but their subclasses are extremely important, especially DataInputStream and DataOutputStream.

In which of the following cases can the Console object be acquired? (Select 1 option:)
  1. When the JVM is started from an interactive command line with redirecting the standard input and output streams to Console.
  2. When the JVM is started from an interactive command line without redirecting the standard input and output streams.
  3. When the JVM is started in the background with redirecting the standard input and output streams to Console.
  4. When the JVM is started in the background without redirecting the standard input and output streams.


Answer: b
Explanation:
Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console. If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

SEMrush Software