public class StreamCopier
extends java.lang.Object
implements java.lang.Runnable
Stream copier copies the contents of one stream to another. The copying can be multi or single threaded.
An example of how to use the object would be:
new Thread(new StreamCopier(StreamCopier.DEFAULT_BUFF_SIZE,
System.in, System.out)).start();
The above example would copy whatever the contents of System.in are to System.out.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_BUFF_SIZE
The default buffer size for copying streams
|
Constructor and Description |
---|
StreamCopier(int buffsize,
java.io.InputStream in,
java.io.OutputStream out)
Creates a new instance of the stream copier object
|
Modifier and Type | Method and Description |
---|---|
void |
run()
This run method does the copying of the source stream to the
destination.
|
public static final int DEFAULT_BUFF_SIZE
public StreamCopier(int buffsize, java.io.InputStream in, java.io.OutputStream out)
buffsize
- the size to use for the buffer.in
- the input stream source to copy from.out
- the output stream destination to copy to.public void run()
This run method does the copying of the source stream to the destination.
The reason for the method's name and for using an object as opposed to a static method is because the stream copier can work as a separate thread. Perhaps the best example of where this is useful would be running a command. When a command is being run, its output is not printed directly to the console (at least not any way that I am aware of at the time of this writing). Because the output is instead saved in a stream within a Process object, we must manually copy the data to System.out, etc. Since there are two important streams we must consider when doing this (stdout and stderr), by multithreading the copy process we can handle both streams simultaneously, and hence the script should appear to run similar to how it would if we were to run it directly at the command prompts.
run
in interface java.lang.Runnable
Copyright © 2008-2022 University of Manitoba.