//==============================================================================
// tribble/io/StreamCopier.java
//------------------------------------------------------------------------------
package tribble.io;
// System imports
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.Exception;
import java.lang.String;
// Local imports
// (None)
/*******************************************************************************
* Simple stream copying methods.
*
* @version $Revision: 1.1 $ $Date: 2001/01/06 17:37:36 $
* @since 2001-01-06
* @author
* David R. Tribble,
* david@tribble.com
*
* Copyright ©2001 by David R. Tribble,
* all rights reserved.
*
* Permission is granted to freely use and distribute this source code
* provided that the original copyright and authorship notices remain
* intact.
*/
public class StreamCopier
{
// Identification
/** Revision information. */
static final String REV =
"@(#)tribble/io/StreamCopier.java $Revision: 1.1 $ $Date: 2001/01/06 17:37:36 $\n";
/** Embedded copyright notice. */
static final String COPYRIGHT =
"\nCopyright \u00A9 2001 by David R. Tribble, all rights reserved.\n";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static constants
/** Default data copying buffer size (in bytes). */
public static final int DFL_BUFSZ = 32*1024;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static methods
/***************************************************************************
* Test driver.
*
* @param args
* The command line arguments.
*
* @since 1.1, 2001-01-06
*/
public static void main(final String[] args)
{
run(args);
System.exit(0);
}
/***************************************************************************
* Equivalent to:
* copy(in, out, {@link #DFL_BUFSZ}).
*
* @see #copy(InputStream, OutputStream, int)
*
* @since 1.1, 2001-01-06
*/
public static long copy(InputStream in, OutputStream out)
throws IOException
{
return (copy(in, out, DFL_BUFSZ));
}
/***************************************************************************
* Copies the contents of an input stream to an output stream.
*
* @param in
* The input stream, which is assumed to already be open for reading.
* This object is synchronized.
*
* @param out
* The output stream, which is assumed to already be open for writing.
* This object is synchronized.
*
* @param bufsz
* The buffer size to use (in bytes).
*
* @return
* The number of bytes copied.
*
* @throws IOException
* Thrown if a read or write error occurs.
*
* @see #copy(InputStream, OutputStream)
*
* @since 1.1, 2001-01-06
*/
public static long copy(InputStream in, OutputStream out, int bufsz)
throws IOException
{
long nBytes = 0;
byte[] buf;
// Allocate a data byte buffer
buf = new byte[bufsz];
// Copy bytes from the input stream to the output stream
synchronized (out)
{
synchronized (in)
{
// Copy blocks of bytes from the input to the output stream
for (;;)
{
int len;
// Read a block of bytes from the input stream
len = in.read(buf); // May throw IOException
// Check for end of file
if (len <= 0)
break;
// Write the block of bytes to the output stream
out.write(buf, 0, len); // May throw IOException
nBytes += len;
}
}
// Clean up
out.flush();
}
// Return the total number of bytes copied
return (nBytes);
}
/***************************************************************************
* Equivalent to:
* copy(in, out, {@link #DFL_BUFSZ}).
*
* @see #copy(Reader, Writer, int)
*
* @since 1.1, 2001-01-06
*/
public static long copy(Reader in, Writer out)
throws IOException
{
return (copy(in, out, DFL_BUFSZ));
}
/***************************************************************************
* Copies the contents of an input stream to an output stream.
*
* @param in
* The input stream, which is assumed to already be open for reading.
* This object is synchronized.
*
* @param out
* The output stream, which is assumed to already be open for writing.
* This object is synchronized.
*
* @param bufsz
* The buffer size to use (in bytes).
*
* @return
* The number of characters copied.
*
* @throws IOException
* Thrown if a read or write error occurs.
*
* @since 1.1, 2001-01-06
*/
public static long copy(Reader in, Writer out, int bufsz)
throws IOException
{
long nChars = 0;
char[] data;
// Copy chars from the input stream to the output stream
///...
if (nChars < 1)
throw new Error("Method tribble.io.StreamCopier.copy(Reader,Writer) is incomplete");
// Return the total number of chars copied
return (nChars);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Protected static methods
/***************************************************************************
* Runs this class as a program.
* Copies the contents of the standard input to the standard output.
*
* @since 1.1, 2001-01-06
*/
protected static void run(final String[] args)
{
boolean opt_char = false;
// Parse command line option args
for (int i = 0; i < args.length && args[i].charAt(0) == '-'; i++)
{
if (args[i].equals("-c"))
opt_char = true;
else if (args[i].equals("-b"))
opt_char = false;
else
usage();
}
// Copy the contents of the standard input to the standard output
try
{
// Copy the contents of the standard input to the standard output
if (opt_char)
{
// Copy the stream using characters
copy(new InputStreamReader(System.in),
new OutputStreamWriter(System.out));
}
else
{
// Copy the stream using bytes
copy(System.in, System.out);
}
// Done
System.exit(0);
}
catch (Exception ex)
{
// Display the exception
System.err.println(ex);
System.exit(1);
}
}
/***************************************************************************
* Displays a program usage message, then punts.
*
*
* This does not return, but calls {@link System#exit}. * * @since 1.1, 2001-01-06 */ protected static void usage() { // Display usage message System.out.println("[" + REV.substring(0, REV.length()-1) + "]\n"); System.out.println( "Copies the standard input stream to the standard output.\n"); System.out.println( "usage: java tribble.io.StreamCopier [-option...]\n"); System.out.println( "Options:\n" + " -b Use byte streams.\n" + " -c Use character streams."); // Punt System.exit(255); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Private methods /*************************************************************************** * Constructor. * This is private to prevent empty instantiations. * * @since 1.1, 2001-01-06 */ private StreamCopier() { // Do nothing } } // End StreamCopier.java