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, you can filter the raw bytes of an InputStream by wrapping it in another InputStream class that provides the filtering functionality. One way to do this is by using the FilterInputStream class, which is an abstract class that can be used as a base for creating custom filter streams.
Here is an example of a custom filter stream that reads bytes from an input stream and converts all uppercase letters to lowercase:
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class LowerCaseInputStream extends FilterInputStream {

   public LowerCaseInputStream(InputStream in) {
       super(in);
   }

   @Override
   public int read() throws IOException {
       int c = super.read();
       return (c == -1 ? c : Character.toLowerCase((char) c));
   }

   @Override
   public int read(byte[] b, int off, int len) throws IOException {
       int result = super.read(b, off, len);
       for (int i = off; i < off + result; i++) {
           b[i] = (byte) Character.toLowerCase((char) b[i]);
       }
       return result;
   }
}

You can use this custom filter stream by wrapping it around an existing input stream:
InputStream in = new LowerCaseInputStream(new FileInputStream("example.txt"));

Now when you read from the in stream, all bytes will be filtered and converted to lowercase before being returned.
Another way to filter bytes is by using the BufferedInputStream class, which provides a buffer to read bytes from the underlying input stream, this can be useful in cases when you want to improve the performance of your program.
InputStream in = new BufferedInputStream(new FileInputStream("example.txt"));

You can also use the DataInputStream Class which provides methods to read primitive data types from the underlying input stream, this can be useful when you want to read structured data from the input stream.
InputStream in = new DataInputStream(new FileInputStream("example.txt"));

A Java programmer can chain multiple filters together by wrapping one filter around another. So you can use a combination of different filter streams to achieve the desired functionality.


Coming up

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.

Constructor and Description

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.