Filter Streams   «Prev  Next»

Lesson 14

Filter InputStreams Raw Bytes - Conclusion

This module discussed how to filter the raw bytes of an input stream before you read them.
Filtering can interpret the raw bytes as different kinds of data, it can buffer the data for increased performance, it can allow you to back up over data you have already read, or it can do all three. Similarly you can filter output streams to change data as it goes from your program into the outside, non-Java world.

Filter the raw bytes of an InputStream

In Java 1.1, you can filter the raw bytes of an `InputStream` by implementing a custom `FilterInputStream`. A `FilterInputStream` provides a way to wrap an existing `InputStream` and override its methods to manipulate the data being read.
Here’s an example of how you can filter an `InputStream` to perform a simple transformation, such as converting all lowercase letters to uppercase:
Example Code:
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UpperCaseInputStream extends FilterInputStream {

    // Constructor that takes an existing InputStream
    protected UpperCaseInputStream(InputStream in) {
        super(in);
    }

    @Override
    public int read() throws IOException {
        int c = super.read(); // Read the next byte
        return (c == -1) ? c : Character.toUpperCase(c); // Convert to uppercase if valid
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int result = super.read(b, off, len); // Read bytes into the buffer
        if (result != -1) {
            for (int i = off; i < off + result; i++) {
                b[i] = (byte) Character.toUpperCase(b[i]); // Convert each byte to uppercase
            }
        }
        return result;
    }

    public static void main(String[] args) {
        String input = "Hello World!";
        try (InputStream inputStream = new UpperCaseInputStream(new java.io.ByteArrayInputStream(input.getBytes()))) {
            int data;
            while ((data = inputStream.read()) != -1) {
                System.out.print((char) data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Explanation:
  1. Custom FilterInputStream
    • You subclass FilterInputStream and override the read() and read(byte[], int, int) methods to process the bytes.
    • In this example, the bytes are converted to uppercase before being returned.
  2. Usage
    • Wrap the original InputStream (e.g., ByteArrayInputStream) with your custom FilterInputStream.
    • Use the read() method to access the transformed data.
  3. Compatibility with Java 1.1
    • The FilterInputStream class and InputStream are available in Java 1.1, so this approach will work without requiring any newer features.

Benefits:
  • This allows you to modify the byte stream in a reusable and composable manner.
  • The approach is extensible for more complex filtering or transformations.


Constructor and Description

In the final, brief module, you will review what you have learned in this course and how you can continue exploring Java input and output.
In addition, you will be asked to complete a course evaluation.
public class FileInputStream
extends InputStream

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
FileInputStream(File file) 

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
FileInputStream(FileDescriptor fdObj) 

Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.
FileInputStream(String name) 

Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
  • Method Summary
    int available() 
    

    Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
     void close() 
    

    Closes this file input stream and releases any system resources associated with the stream.
    protected void finalize() 
    

    Ensures that the close method of this file input stream is called when there are no more references to it.
    FileChannel getChannel() 
    

    Returns the unique FileChannel object associated with this file input stream.
    FileDescriptor getFD() 
    

    Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
    int read() 
    
    Reads a byte of data from this input stream.
    int read(byte[] b) 
    
    Reads up to b.length bytes of data from this input stream into an array of bytes.
    int read(byte[] b, int off, int len) 
    
    Reads up to len bytes of data from this input stream into an array of bytes.
    long skip(long n) 
    
    Skips over and discards n bytes of data from the input stream.

SEMrush Software