Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Compressing Streams: File Dumper4, Java example Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

/* Title: Hardcore Java
* Title: Java I/O
* Second Edition: May 2006
* ISBN 10: 0-596-52750-0
* ISBN 13: 9780596527501
*/

Code:


import java.io.*;
import java.util.zip.*;
import com.elharo.io.*;

public class FileDumper4 {

  public static final int ASC = 0;
  public static final int DEC = 1;
  public static final int HEX = 2;
  public static final int SHORT = 3;
  public static final int INT = 4;
  public static final int LONG = 5;
  public static final int FLOAT = 6;
  public static final int DOUBLE = 7;
 
  public static void main(String[] args) {

    if (args.length < 1) {
      System.err.println("Usage: java FileDumper4 [-ahdsilfx] [-little]"+
                                      "[-gzip|-deflated] file1...");
    }

    boolean bigEndian = true;
    int firstFile = 0;
    int mode = ASC;
    boolean deflated = false;
    boolean gzipped = false;
   
    // Process command-line switches.
    for (firstFile = 0; firstFile < args.length; firstFile++) {
      if (!args[firstFile].startsWith("-")) break;
      if (args[firstFile].equals("-h")) mode = HEX;
      else if (args[firstFile].equals("-d")) mode = DEC;
      else if (args[firstFile].equals("-s")) mode = SHORT;
      else if (args[firstFile].equals("-i")) mode = INT;
      else if (args[firstFile].equals("-l")) mode = LONG;
      else if (args[firstFile].equals("-f")) mode = FLOAT;
      else if (args[firstFile].equals("-x")) mode = DOUBLE;
      else if (args[firstFile].equals("-little")) bigEndian = false;
      else if (args[firstFile].equals("-deflated") && !gzipped) deflated = true;
      else if (args[firstFile].equals("-gzip") && !deflated) gzipped = true;
    }
   
    for (int i = firstFile; i < args.length; i++) {
      try {
        InputStream in = new FileInputStream(args[i]);
        dump(in, System.out, mode, bigEndian, deflated, gzipped);
       
        if (i < args.length-1) {  // more files to dump
          System.out.println();
          System.out.println("--------------------------------------");
          System.out.println();
        }
      }
      catch (Exception ex) {
        System.err.println(ex);
        e.printStackTrace();
      }
    }
  }

  public static void dump(InputStream in, OutputStream out, int mode,
   boolean bigEndian, boolean deflated, boolean gzipped) throws IOException {
   
    // The reference variable in may point to several different objects
    // within the space of the next few lines. We can attach
    //  more filters here to do decompression, decryption, and more.
    if (deflated) {
      in = new InflaterInputStream(in);
    }
    else if (gzipped) {
      in = new GZIPInputStream(in);
    }

    if (bigEndian) {
      DataInputStream din = new DataInputStream(in);
      switch (mode) {
        case HEX:
          in = new HexFilter(in);
          break;
        case DEC:
          in = new DecimalFilter(in);
          break;
        case INT:
          in = new IntFilter(din);
          break;
        case SHORT:
          in = new ShortFilter(din);
          break;
        case LONG:
          in = new LongFilter(din);
          break;
        case DOUBLE:
          in = new DoubleFilter(din);
          break;
        case FLOAT:
          in = new FloatFilter(din);
          break;
        default:
      }
    }
    else {
      LittleEndianInputStream lin = new LittleEndianInputStream(in);
      switch (mode) {
        case HEX:
          in = new HexFilter(in);
          break;
        case DEC:
          in = new DecimalFilter(in);
          break;
        case INT:
          in = new LEIntFilter(lin);
          break;
        case SHORT:
          in = new LEShortFilter(lin);
          break;
        case LONG:
          in = new LELongFilter(lin);
          break;
        case DOUBLE:
          in = new LEDoubleFilter(lin);
          break;
        case FLOAT:
          in = new LEFloatFilter(lin);
          break;
        default: 
      }
    }   
    StreamCopier.copy(in, out);
    in.close();
  }
}


 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.util.zip.Inflater

  java.lang.Process

  java.io.InputStream

  java.io.FileInputStream

  java.lang.Exception

  java.io.OutputStream

  java.io.IOException

  java.util.zip.InflaterInputStream

  java.util.zip.GZIPInputStream

  java.io.DataInputStream




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy