net.markenwerk:utils-data-fetcher

A data stream fetching helper for Java

License

License

Categories

Categories

Data Net
GroupId

GroupId

net.markenwerk
ArtifactId

ArtifactId

utils-data-fetcher
Last Version

Last Version

4.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

net.markenwerk:utils-data-fetcher
A data stream fetching helper for Java
Project URL

Project URL

https://github.com/markenwerk/java-utils-data-fetcher
Project Organization

Project Organization

Markenwerk – Gesellschaft für markenbildende Maßnahmen mbH
Source Code Management

Source Code Management

https://github.com/markenwerk/java-utils-data-fetcher

Download utils-data-fetcher

How to add to project

<!-- https://jarcasting.com/artifacts/net.markenwerk/utils-data-fetcher/ -->
<dependency>
    <groupId>net.markenwerk</groupId>
    <artifactId>utils-data-fetcher</artifactId>
    <version>4.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/net.markenwerk/utils-data-fetcher/
implementation 'net.markenwerk:utils-data-fetcher:4.0.2'
// https://jarcasting.com/artifacts/net.markenwerk/utils-data-fetcher/
implementation ("net.markenwerk:utils-data-fetcher:4.0.2")
'net.markenwerk:utils-data-fetcher:jar:4.0.2'
<dependency org="net.markenwerk" name="utils-data-fetcher" rev="4.0.2">
  <artifact name="utils-data-fetcher" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.markenwerk', module='utils-data-fetcher', version='4.0.2')
)
libraryDependencies += "net.markenwerk" % "utils-data-fetcher" % "4.0.2"
[net.markenwerk/utils-data-fetcher "4.0.2"]

Dependencies

compile (1)

Group / Artifact Type Version
net.markenwerk : commons-nulls jar 1.0.4

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

A data stream fetching helper for Java

Build Status Coverage Status Dependency Status Maven Central Issues MIT License

Overview

This is a simple helper to fetch the complete content of an input stream. It is, among other things, intended as an alternative to using IOUtils#readFully for this everyday task.

Consult the documentation and the usage description for further information:

Maven

This library is hosted in the Maven Central Repository. You can use it with the following coordinates:

<dependency>
	<groupId>net.markenwerk</groupId>
	<artifactId>utils-data-fetcher</artifactId>
	<version>4.0.2</version>
</dependency>

Motivation

Copying the complete content of an InputStream into a byte[] or into an OutputStream is a menial task, that has to be dealt with very commonly. While this is certainly not a difficult challenge for any programmer, it is still boring and annoying to do it over and over again, and – as pretty much everything – prone to be erroneously, if done in a hurry.

There are common solutions, beside coding it over and over again. The Oracle JRE provides the class IOUtils for this simple task. But this class is not a part of the official Java specifications, which means that using it yields a compiler warning like

The type 'IOUtils' is not API.

and may break at runtime; i.e. if a runtime environment other than the Oracle JRE is used. Another solution to accomplishing this simple task, could be to bundle a major library like Commons IO. But doing so may appear like overkill, especially if no other functionality of the library is likely to be used.

This library provides a simple and lightweight alternative.

Usage

Fetching data streams

This library provides the DataFetcher which features two main functionalities to handle byte streams:

// create a simple DataFetcher
DataFetcher fetcher = new BufferedDataFetcher();

// copy the content of an InputStream into a byte[]
byte[] bytes = fetcher.fetch(inputStream);

// copy the content of an InputStream into an OutputStream
fetcher.copy(inputStream, outputStream);

Automatic closing of supplied streams

All methods have optional boolean parameters, that can be used to instruct the DataFetcher to close the supplied streams. Doing so allows to write compact code like:

foo.setContent(new BufferedDataFetcher().fetch(new FileInputStream(file), true));

Otherwise, it would be necessary to write unnecessarily verbose code like:

InputStream in = new FileInputStream(file);
foo.setContent(new BuffereddataFetcher().fetch(in));
in.close();

Listen to fetch progress

All methods optionally take a DataFetchProgressListener that gets notified on various points in the lifecycle (started, progressed, succeeded or failed, finished) of a fetch operation. The IdleDataFetchProgressListener is a convenient base implementation with empty methods.

new BufferedDataFetcher().copy(
	new FileInputStream(inFile),
	new FileOutputStream(outFile),
	new IdleDataFetchProgressListener() {
		public void onProgress(long bytesFetched) {
			System.out.println(bytesFetched + " bytes copied so far.");
		}
	},
	true, 
	true
);

Handling of null arguments

Missing or invalid arguments are handled gracefully with the following behaviour:

  • If null is given as the InputStream, it is simply ignored and handled as if there was nothing to read. That means, nothing is written to the OutputStream and, if requested, the OutputStream will be closed.
  • If null is given as the OutputStream, it is simply ignored, but the content of the given InputStream is fetched anyway. If requested, the InputStream be closed.

Customizability

This library provides BufferedDataFetcher as the simplest possible implementations of DataFetcher. It eagerly allocates a byte[] during construction and uses it as a buffer to perform fetch operations by sequentially reading from the InputStream into the buffer and then writing from the buffer to the OutputStream. It is not threadsafe.

Variations of the buffered copying strategy (e.g. lazy buffer creation, threadsafeness) or completely other copying strategies can easily be implemented by extending AbstractDataFetcher or AbstractBufferedDataFetcher respectively.

AbstractDataFetcher reduces all methods from the DataFetcher interface to the single method doCopy(InputStream, OutputStream, DataFetchProgressListener) where every parameter is guaranteed to be not null, and therefore greatly simplifies the implementation of new fetch strategies. AbstractBufferedDataFetcher is such an implementation of the aforementioned buffering fetch strategy that handles the sequentially write-read cycles and notifies the DataFetchProgressListener accordingly, but leaves the buffer allocation strategy to be specified.

net.markenwerk

Markenwerk

Versions

Version
4.0.2
4.0.1
4.0.0
3.1.0
3.0.0
2.1.2
2.1.1
2.1.0
2.0.0
1.1.2
1.1.1
1.1.0
1.0.0