Create an InputStream that returns a filtered version of the original InputStream

Create a class that extends from FilterInputStream and override the method read in which you convert the original value to a new value.

Following example introduces the ChangeCaseInputStream which will convert any character to uppercase or lowercase depending on how you instantiated the class.

Main.java:

import java.io.*;
 
public class Main {
   public static void main(String args[]) {
      if (args.length != 1) {
         System.out.println("Usage: java Main <filename.txt>");
         System.exit(1);
      }
 
      try {
         FileInputStream fis = new FileInputStream(args[0]);
         ChangeCaseInputStream ccis = new ChangeCaseInputStream(
                                        ChangeCaseInputStream.UPPERCASE, fis);
         int b;
         while ((b = ccis.read()) > 0) {
            System.out.print((char) b);
         }
         ccis.close();
      }
      catch(FileNotFoundException e) {
         System.out.println(e);
      }
      catch(IOException e) {
         System.out.println(e);
      }
   }
}
 
class ChangeCaseInputStream extends FilterInputStream {
   public static final int LOWERCASE = 1;
   public static final int UPPERCASE = 2;
 
   private int conversionType;
 
   public ChangeCaseInputStream(int conversionType, InputStream in) {
      super(in);
 
      this.conversionType = conversionType;
   }
 
   public int read() throws IOException {
      int b = in.read();
 
      // first check for EOF
      if (b < 0) {
         return b;
      }
 
      if (conversionType == LOWERCASE) {
         b = (int) Character.toLowerCase((char) b);
      }
      else if (conversionType == UPPERCASE) {
         b = (int) Character.toUpperCase((char) b);
      }
 
      return b;     
   }
}