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.
Java Water Filters
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.

Java Lambdas and Parallel Streams