//==============================================================================
// DeflaterInputStreamTest.java
//==============================================================================
package test;
// System imports
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.String;
import java.lang.System;
import java.util.zip.DeflaterInputStream;
import java.util.zip.DeflaterOutputStream;
// Local imports
// (None)
/*******************************************************************************
* Test driver for class java.util.zip.DeflaterInputStream.
*
*
* @version 1.1, 2005-07-04
* @since JDK 1.6, 2005-07-04
* @author David R. Tribble (david@tribble.com)
*
* @see DeflaterInputStream
*/
class DeflaterInputStreamTest
{
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// 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 InflaterOutputStream} class.
*
*
*
* Usage
*
*
* java java.util.zip.DeflaterInputStreamTest
* [-option...] file
*
*
*
* Options
*
*
* - -j
*
- Use the java.util.zip.DeflaterOutputStream 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 InflaterOutputStream#main InflaterOutputStream.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("Decompress a file.");
System.out.println();
System.out.println("usage: "
+ DeflaterInputStreamTest.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);
}
// Deflate the input file
if (useThis)
{
DeflaterInputStream defl;
// Initialize the file deflater
defl = new DeflaterInputStream(in);
// Deflate the contents of the input file
for (;;)
{
int len;
// Read and compress some data from the input stream
len = defl.read(buf, 0, buf.length);
if (len < 0)
break;
// Write the compressed data to the output stream
out.write(buf, 0, len);
out.flush();
}
// Finish up
if (in != System.in)
defl.close();
}
else
{
DeflaterOutputStream defl;
// Initialize the file deflater
defl = new DeflaterOutputStream(out);
// Deflate 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;
// Compress and write the data to the output stream
defl.write(buf, 0, len);
out.flush();
}
// Finish up
defl.finish();
if (out != System.out)
defl.close();
}
// Clean up
if (in != System.in)
in.close();
out.flush();
if (out != System.out)
out.close();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constructors
/***************************************************************************
* Default construct.
*
* @since 1.1, 2005-06-29
*/
private DeflaterInputStreamTest()
{
// Do nothing
}
}
// End DeflaterInputStreamTest.java