The FilterInputStream
class in Java is an abstract class that provides a basic implementation of the InputStream
interface by filtering the data read from an underlying stream. This class serves as the superclass for other input stream classes that perform filtering operations on input data.
The FilterInputStream
class is designed to be subclassed to provide specific filtering operations. Subclasses of FilterInputStream
may override some or all of the methods defined in the InputStream
interface to provide custom filtering behavior.
Some of the methods provided by the FilterInputStream
class include:
available()
: returns an estimate of the number of bytes that can be read from the underlying input stream without blocking.close()
: closes the underlying input stream and releases any resources associated with it.mark(int readlimit)
: marks the current position in the stream, allowing subsequent calls toreset()
to reposition the stream to this point.markSupported()
: returns a boolean value indicating whether themark()
andreset()
methods are supported by the underlying input stream.read()
: reads a single byte of data from the input stream.read(byte[] b)
: reads a sequence of bytes from the input stream into the specified byte array.skip(long n)
: skips over and discards up ton
bytes of data from the input stream.
Examples of subclasses of FilterInputStream
include BufferedInputStream
, which adds buffering to an input stream to improve performance, and DataInputStream
, which adds methods for reading primitive data types from an input stream.
Java FilterInputStream class declaration:
The FilterInputStream
class in Java is an abstract class that extends the InputStream
class. It provides a basic implementation of the InputStream
interface by filtering the data read from an underlying stream. Here is the declaration of the FilterInputStream
class:
public class FilterInputStream extends InputStream { protected volatile InputStream in; protected FilterInputStream(InputStream in) { this.in = Objects.requireNonNull(in); } public int available() throws IOException { return in.available(); } public void close() throws IOException { in.close(); } public synchronized void mark(int readlimit) { in.mark(readlimit); } public boolean markSupported() { return in.markSupported(); } public int read() throws IOException { return in.read(); } public int read(byte[] b, int off, int len) throws IOException { return in.read(b, off, len); } public synchronized void reset() throws IOException { in.reset(); } public long skip(long n) throws IOException { return in.skip(n); } }
As you can see, FilterInputStream
defines a constructor that takes an InputStream
object as a parameter, and it provides default implementations of the methods declared in the InputStream
interface. Subclasses of FilterInputStream
can override any of these methods to provide custom behavior.
Java FilterInputStream class Methods:
The FilterInputStream
class in Java provides several methods to perform filtering operations on input data. Here’s a brief overview of the methods provided by the FilterInputStream
class:
public int available() throws IOException
: This method returns an estimate of the number of bytes that can be read from the underlying input stream without blocking.public void close() throws IOException
: This method closes the underlying input stream and releases any resources associated with it.protected void finalize() throws IOException
: This method is called by the garbage collector when there are no more references to the object. It closes the underlying input stream if it hasn’t already been closed.public synchronized void mark(int readlimit)
: This method marks the current position in the stream, allowing subsequent calls toreset()
to reposition the stream to this point.public boolean markSupported()
: This method returns a boolean value indicating whether themark()
andreset()
methods are supported by the underlying input stream.public int read() throws IOException
: This method reads a single byte of data from the input stream.public int read(byte[] b) throws IOException
: This method reads a sequence of bytes from the input stream into the specified byte array.public int read(byte[] b, int off, int len) throws IOException
: This method reads up tolen
bytes of data from the input stream into the specified byte array, starting at the specified offsetoff
.public synchronized void reset() throws IOException
: This method repositions the stream to the most recentmark()
.public long skip(long n) throws IOException
: This method skips over and discards up ton
bytes of data from the input stream.
Subclasses of FilterInputStream
can override any of these methods to provide custom behavior.
Example of FilterInputStream class:
Here’s an example of using the FilterInputStream
class in Java to implement a simple filtering operation:
import java.io.*; public class UppercaseInputStream extends FilterInputStream { public UppercaseInputStream(InputStream in) { super(in); } public int read() throws IOException { int c = super.read(); return (c == -1 ? c : Character.toUpperCase((char) c)); } public int read(byte[] b, int offset, int len) throws IOException { int result = super.read(b, offset, len); for (int i = offset; i < offset + result; i++) { b[i] = (byte) Character.toUpperCase((char) b[i]); } return result; } }
This class extends FilterInputStream
and overrides the read()
and read(byte[] b, int offset, int len)
methods to convert all characters in the input stream to uppercase. The read()
method reads a single byte from the input stream and converts it to uppercase, while the read(byte[] b, int offset, int len)
method reads a sequence of bytes from the input stream and converts them to uppercase. The UppercaseInputStream
class can be used like any other input stream class in Java. For example:
public static void main(String[] args) throws IOException { FileInputStream fileInputStream = new FileInputStream("test.txt"); UppercaseInputStream uppercaseInputStream = new UppercaseInputStream(fileInputStream); int c; while ((c = uppercaseInputStream.read()) >= 0) { System.out.print((char) c); } uppercaseInputStream.close(); }
This code reads a file called “test.txt” and converts all characters to uppercase before printing them to the console.