public final class IOUtilities extends HttpDownloader
© Copyright 2009 Sans Pareil Technologies, Inc.
logger| Modifier and Type | Method and Description |
|---|---|
static void |
close(InputStream stream)
Close the specified input stream.
|
static void |
close(OutputStream stream)
Close the specified output stream.
|
static void |
close(Reader reader)
Close the specified character stream reader.
|
static void |
close(Writer writer)
Close the specified character stream writer.
|
void |
fromUrl(String url,
OutputStream outputStream,
String... parameters)
|
static IOUtilities |
getInstance() |
void |
write(BufferedReader reader,
Writer writer)
Writes all the data from the specified
BufferedReader to the
specified Writer. |
void |
write(InputStream in,
OutputStream out)
Read the contents of the specified input stream and write them to the
specified output stream.
|
abortIfRequested, deregisterListener, doGET, doPOST, doPOST, getConditionVariable, getConnection, getInputStream, getListener, getReader, registerConditionVariable, registerListener, resetAcceptEncoding, retryCount, setAcceptEncoding, socketTimeoutpublic static IOUtilities getInstance()
public void write(InputStream in, OutputStream out) throws IOException
in - The input stream to read from.out - The output stream to write to.IOException - If errors are encountered.public void write(BufferedReader reader, Writer writer) throws IOException
BufferedReader to the
specified Writer.
Note: The reader and writer instances are closed by this method.reader - The BufferedReader from which the
data is to be read.writer - The writer to which the data is to be written.IOException - If errors are encountered while reading the
data.public void fromUrl(String url, OutputStream outputStream, String... parameters)
URL and save to
a local File. The file will be named identical to
the name of the file specified in the URL and stored under the current
working directory with the same path as on the url resource.
The following code shows sample usage of this method.
import java.io.File;
import com.sptci.io.IOUtilities;
public class test
{
public static void main( String[] args )
{
try
{
final HttpDownloadListener listener = createListener();
IOUtilities.registerListener( listener );
final String url = "http://www.sptci.com/index.jsp";
final OutputStream fos = new FileOutputStream( "/path/to/file" );
IOUtilities.fromUrl( url, fos );
...
final String purl = "http://www.sptci.com/private/manual.pdf";
final String userName = getUserName();
final String password = getPassword();
final OutputStream another = new FileOutputStream( "/path/to/anotherfile" );
IOUtilities.fromUrl( purl, another, userName, password );
}
catch ( Throwable t )
{
// Error handling
}
finally
{
IOUtilities.deregisterListener();
}
}
}
url - The url from which the data is to be fetched.outputStream - The output stream to which the data from the url is to be writtenparameters - Optional parameters used to control the location and
name of the downloaded file. Also used to specify the credentials
to be used to fetch the file. The parameters should be specified
in the following order:
userName The user name part of the credentials to
use to authenticate with the server.password The password to use to authenticate with
the server.userName and password
values must always be specified in tandem.RuntimeException - If errors are encountered while reading or writing
the data and a was not registered.public static void close(InputStream stream)
stream - The stream to close.public static void close(Reader reader)
reader - The stream to close.public static void close(OutputStream stream)
stream - The stream to close.public static void close(Writer writer)
writer - The stream to close.