public class Streams extends Object
A utility class for handling standard operations using streams.
Modifier and Type | Method and Description |
---|---|
static void |
compressGzip(DataSource source,
DataTarget target,
int compressionLevel)
Compresses the content of the source to the target using GZIP until the
complete content is compressed.
|
static void |
compressGzip(DataSource source,
DataTarget target,
int blockSize,
long length,
int compressionLevel)
Compresses the content of the source to the target using GZIP until the
complete content is compressed.
|
static void |
copy(DataSource source,
DataTarget target)
Copies the content of the source to the target until the complete content
is copied.
|
static void |
copy(DataSource source,
DataTarget target,
int blockSize,
long length)
Copies the content of the source to the target until the complete content
is copied.
|
static void |
copy(InputStream in,
OutputStream out)
Copies the content of the input stream to the output stream until the end
of the input stream is reached.
|
static void |
copy(InputStream in,
OutputStream out,
int blockSize,
long length)
Copies the content of the input stream to the output stream until the end
of the input stream is reached.
|
static InputStream |
createBufferedFileInputStream(File file)
Creates a buffered stream for reading from the specified file.
|
static InputStream |
createBufferedFileInputStream(File file,
int fileBufferSize)
Creates a buffered stream for reading from the specified file.
|
static OutputStream |
createBufferedFileOutputStream(File file)
Creates a buffered stream for writing into the specified file.
|
static OutputStream |
createBufferedFileOutputStream(File file,
int fileBufferSize)
Creates a buffered stream for writing into the specified file.
|
static DataConsumer |
createConsumer(DataTarget target)
Creates a consumer which puts its data into the specified target.
|
static DataProducer |
createProducer(DataSource source)
Creates a producer which gets its data from the specified source.
|
static DataSource |
createResourceDataSource(Class<?> clazz,
String name)
Creates a source on a resource.
|
static DataSource |
createResourceDataSource(Package pkg,
ClassLoader loader,
String name)
Creates a source on a resource.
|
static InputStream |
createResourceInputStream(Class<?> clazz,
String name)
Creates a stream on a resource.
|
static InputStream |
createResourceInputStream(Package pkg,
ClassLoader loader,
String name)
Creates a stream on a resource.
|
static void |
transfer(DataProducer producer,
DataConsumer consumer)
Transfers data from a producer to a consumer.
|
static void |
transfer(DataProducer producer,
DataConsumer consumer,
int blockSize,
long length)
Transfers data from a producer to a consumer.
|
public static void copy(InputStream in, OutputStream out) throws IOException
Copies the content of the input stream to the output stream until the end of the input stream is reached.
in
- The input stream.out
- The output stream.IllegalArgumentException
- If any of the parameters are null
.IOException
- If an I/O error occurs.public static void copy(InputStream in, OutputStream out, int blockSize, long length) throws IOException
Copies the content of the input stream to the output stream until the end of the input stream is reached.
in
- The input stream.out
- The output stream.blockSize
- The block size of data which will be read at once.length
- The (maximum) length of the data which should be read from the
input stream. If the length is negative the method will read
from the input stream until the end is reached. If the
end-of-stream is reached before the specified length could be
read the method will exit without an exception.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static void transfer(DataProducer producer, DataConsumer consumer) throws IOException
Transfers data from a producer to a consumer.
producer
- The producer which offers the data.consumer
- The consumer which handles the data.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static void transfer(DataProducer producer, DataConsumer consumer, int blockSize, long length) throws IOException
Transfers data from a producer to a consumer.
producer
- The producer which offers the data.consumer
- The consumer which handles the data.blockSize
- The block size of data which will be read at once.length
- The (maximum) length of the data which should be read from the
producer. If the length is negative the method will read from
the producer until the end is reached. If the end is reached
before the specified length could be read the method will exit
without an exception.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static void copy(DataSource source, DataTarget target) throws IOException
Copies the content of the source to the target until the complete content is copied.
The source as well as the target is not closed by this method.
source
- The source.target
- The target.IllegalArgumentException
- If any of the parameters are null
.IOException
- If an I/O error occurs.public static void copy(DataSource source, DataTarget target, int blockSize, long length) throws IOException
Copies the content of the source to the target until the complete content is copied.
The source as well as the target is not closed by this method.
source
- The source.target
- The target.blockSize
- The block size of data which will be read at once.length
- The (maximum) length of the data which should be read from the
input stream. If the length is negative the method will read
from the input stream until the end is reached. If the
end-of-stream is reached before the specified length could be
read the method will exit without an exception.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static void compressGzip(DataSource source, DataTarget target, int compressionLevel) throws IOException
Compresses the content of the source to the target using GZIP until the complete content is compressed.
The source as well as the target is not closed by this method.
source
- The source.target
- The target.compressionLevel
- The compression level, a number from -1
to
9
. -1
is the default compression,
0
is no compression and 9
is the
best compression.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static void compressGzip(DataSource source, DataTarget target, int blockSize, long length, int compressionLevel) throws IOException
Compresses the content of the source to the target using GZIP until the complete content is compressed.
The source as well as the target is not closed by this method.
source
- The source.target
- The target.blockSize
- The block size of data which will be read at once.length
- The (maximum) length of the data which should be read from the
input stream. If the length is negative the method will read
from the input stream until the end is reached. If the
end-of-stream is reached before the specified length could be
read the method will exit without an exception.compressionLevel
- The compression level, a number from -1
to
9
. -1
is the default compression,
0
is no compression and 9
is the
best compression.IllegalArgumentException
- If any of the parameters are null
or the block
size is less than 1
.IOException
- If an I/O error occurs.public static DataProducer createProducer(DataSource source) throws IOException
Creates a producer which gets its data from the specified source.
source
- The source.IllegalArgumentException
- If the source is null
.IOException
- If an I/O error occurs while obtaining the
InputStream
of the source.public static DataConsumer createConsumer(DataTarget target) throws IOException
Creates a consumer which puts its data into the specified target.
target
- The target.IllegalArgumentException
- If the target is null
.IOException
- If an I/O error occurs while obtaining the
OutputStream
of the target.public static InputStream createBufferedFileInputStream(File file) throws FileNotFoundException
Creates a buffered stream for reading from the specified file.
file
- The file.IllegalArgumentException
- If the file is null
.FileNotFoundException
- If the file does not exist, is a directory rather than a
regular file, or for some other reason cannot be opened for
reading.public static InputStream createBufferedFileInputStream(File file, int fileBufferSize) throws FileNotFoundException
Creates a buffered stream for reading from the specified file.
file
- The file.fileBufferSize
- The size of the buffer for reading from the file.IllegalArgumentException
- If the file is null
or the file buffer size is
less than 1
.FileNotFoundException
- If the file does not exist, is a directory rather than a
regular file, or for some other reason cannot be opened for
reading.public static OutputStream createBufferedFileOutputStream(File file) throws FileNotFoundException
Creates a buffered stream for writing into the specified file.
file
- The file.IllegalArgumentException
- If the file is null
.FileNotFoundException
- If the file exists but is a directory rather than a regular
file, does not exist but cannot be created, or cannot be
opened for any other reason.public static OutputStream createBufferedFileOutputStream(File file, int fileBufferSize) throws FileNotFoundException
Creates a buffered stream for writing into the specified file.
file
- The file.fileBufferSize
- The size of the buffer for writing into the file.IllegalArgumentException
- If the file is null
or the file buffer size is
less than 1
.FileNotFoundException
- If the file exists but is a directory rather than a regular
file, does not exist but cannot be created, or cannot be
opened for any other reason.public static InputStream createResourceInputStream(Package pkg, ClassLoader loader, String name) throws FileNotFoundException
Creates a stream on a resource.
pkg
- The package in which the resource resides.loader
- The class loader which should be used.name
- The name of the resource.IllegalArgumentException
- If any of the parameters are null
.FileNotFoundException
- If the resource couldn't be found.public static InputStream createResourceInputStream(Class<?> clazz, String name) throws FileNotFoundException
Creates a stream on a resource.
clazz
- The class which provides the package and the class loader used
to load the resource. The package of the class is the package
in which the resource resides.name
- The name of the resource.IllegalArgumentException
- If any of the parameters are null
.FileNotFoundException
- If the resource couldn't be found.public static DataSource createResourceDataSource(Package pkg, ClassLoader loader, String name) throws FileNotFoundException
Creates a source on a resource.
pkg
- The package in which the resource resides.loader
- The class loader which should be used.name
- The name of the resource.IllegalArgumentException
- If any of the parameters are null
.FileNotFoundException
- If the resource couldn't be found.public static DataSource createResourceDataSource(Class<?> clazz, String name) throws FileNotFoundException
Creates a source on a resource.
clazz
- The class which provides the package and the class loader used
to load the resource. The package of the class is the package
in which the resource resides.name
- The name of the resource.IllegalArgumentException
- If any of the parameters are null
.FileNotFoundException
- If the resource couldn't be found.Copyright © 2015–2019. All rights reserved.