//==============================================================================
// InflaterOutputStreamTest.java
//==============================================================================
package test;
// System imports
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.String;
import java.lang.System;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.InflaterOutputStream;
// Local imports
// (None)
/*******************************************************************************
* Test driver for class java.util.zip.InflaterOutputStream.
*
*
* @version 1.1, 2005-07-04
* @since JDK 1.6, 2005-07-04
* @author David R. Tribble (david@tribble.com)
*
* @see InflaterOutputStream
*/
class InflaterOutputStreamTest
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constants
/** I/O buffer size (in bytes) (see {@link #main main()}). */
private static final int IO_BUFLEN = 64*1024;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static methods
/***************************************************************************
* Uncompress a file containing data stored in the "deflate" compression
* format.
*
*
* The file to be uncompressed should contain compressed data stored in the
* "deflate" compression format, such as the output from the
* {@link DeflaterInputStream} class.
*
*
*
* Usage
*
*
* java tribble.io.InflaterOutputStreamTest
* [-option...] file
*
*
*
* Options
*
*
* - -j
*
- Use the java.util.zip.InflaterInputStream class
* instead of this class.
*
*
- -o file
*
- Output filename.
* By default, the output is written to standard output.
*
*
- file
*
- Input filename.
* If this is "-", input is read from standard input.
*
*
*
* @param args
* Command line arguments.
*
* @see DeflaterInputStream#main DeflaterInputStream.main()
*
* @since 1.1, 2005-06-29
*/
public static void main(final String[] args)
throws Exception
{
String inFname = "-";
String outFname = "-";
boolean useThis = true;
InputStream in;
OutputStream out;
byte[] buf = new byte[IO_BUFLEN];
int i;
// Get command line options
for (i = 0; i < args.length && args[i].charAt(0) == '-'; i++)
{
if (args[i].equals("-"))
break;
else if (args[i].equals("-j"))
useThis = false;
else if (args[i].equals("-o"))
outFname = args[++i];
else
{
System.err.println("Bad option: '" + args[i] + "'");
System.exit(255);
}
}
// Check command line args
if (i >= args.length)
{
// Display a usage message
System.out.println("Compress a file.");
System.out.println();
System.out.println("usage: "
+ InflaterOutputStreamTest.class.getName()
+ " [-option...] file");
System.out.println();
System.out.println("Options:");
System.out.println(" -j "
+ "Use the standard Java class instead of this one.");
System.out.println(" -o file "
+ "Output file.");
// Punt
System.exit(255);
}
// Open the input file
inFname = args[i++];
if (inFname.equals("-"))
{
// Read from standard input
in = System.in;
}
else
{
File inFile;
// Read from a named file
inFile = new File(inFname);
if (!inFile.exists() || !inFile.canRead())
throw new IOException("Can't read: " + inFname);
in = new FileInputStream(inFname);
}
// Open the output file
if (outFname.equals("-"))
{
// Write to standard output
out = System.out;
}
else
{
// Write to a named file
out = new FileOutputStream(outFname);
}
// Inflate the input file
if (useThis)
{
InflaterOutputStream infl;
// Initialize the file inflater
infl = new InflaterOutputStream(out);
// Inflate the contents of the input file
for (;;)
{
int len;
// Read some data from the input stream
len = in.read(buf, 0, buf.length);
if (len < 0)
break;
// Decompress and write the data to the output stream
infl.write(buf, 0, len);
out.flush();
}
// Finish up
if (in != System.in)
infl.close();
}
else
{
InflaterInputStream infl;
// Initialize the file inflater
infl = new InflaterInputStream(in);
// Inflate the contents of the input file
for (;;)
{
int len;
// Read and decompress some data from the input stream
len = infl.read(buf, 0, buf.length);
if (len < 0)
break;
// Write the decompressed data to the output stream
out.write(buf, 0, len);
out.flush();
}
// Finish up
if (out != System.out)
infl.close();
}
// Clean up
if (in != System.in)
in.close();
out.flush();
if (out != System.out)
out.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Private constructors
/***************************************************************************
* Default constructor.
*
* @since 1.1, 2005-06-29
*/
private InflaterOutputStreamTest()
{
// Do nothing
}
}
// End InflaterOutputStreamTest.java