capture-output-stream

Capture STDOUT and STDERR from Java with AutoCloseable

License

License

Categories

Categories

Net
GroupId

GroupId

net.moznion
ArtifactId

ArtifactId

capture-output-stream
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

capture-output-stream
Capture STDOUT and STDERR from Java with AutoCloseable
Project URL

Project URL

https://github.com/moznion/capture-output-stream
Source Code Management

Source Code Management

https://github.com/moznion/capture-output-stream

Download capture-output-stream

How to add to project

<!-- https://jarcasting.com/artifacts/net.moznion/capture-output-stream/ -->
<dependency>
    <groupId>net.moznion</groupId>
    <artifactId>capture-output-stream</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/net.moznion/capture-output-stream/
implementation 'net.moznion:capture-output-stream:1.0.0'
// https://jarcasting.com/artifacts/net.moznion/capture-output-stream/
implementation ("net.moznion:capture-output-stream:1.0.0")
'net.moznion:capture-output-stream:jar:1.0.0'
<dependency org="net.moznion" name="capture-output-stream" rev="1.0.0">
  <artifact name="capture-output-stream" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.moznion', module='capture-output-stream', version='1.0.0')
)
libraryDependencies += "net.moznion" % "capture-output-stream" % "1.0.0"
[net.moznion/capture-output-stream "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
commons-io : commons-io jar 2.4

test (2)

Group / Artifact Type Version
junit : junit jar 4.11
com.google.code.findbugs : findbugs jar 3.0.0

Project Modules

There are no modules declared in this project.

capture-output-stream Build Status javadoc.io

Capture STDOUT and STDERR from Java with AutoCloseable

Synopsis

Capturing

import net.moznion.capture.output.stream.Capturer;

import java.io.ByteArrayOutputStream;

ByteArrayOutputStream stdout = new ByteArrayOutputStream();
ByteArrayOutputStream stderr = new ByteArrayOutputStream();

try (Capturer capturer = new Capturer(stdout, stderr)) {
    System.out.print("hello");   // <= don't print anything
    System.err.print("goodbye"); // <= don't print anything
} // turn back to original stdout and stderr at here

System.out.print(stdout.toString()); // <= print "hello" on stdout
System.err.print(stderr.toString()); // <= print "goodbye" on stderr

Tee

import net.moznion.capture.output.stream.Tee;

import java.io.ByteArrayOutputStream;

ByteArrayOutputStream stdoutBranch = new ByteArrayOutputStream();
ByteArrayOutputStream stderrBranch = new ByteArrayOutputStream();

try (Tee tee = new Tee(stdoutBranch, stderrBranch)) {
    System.out.print("hello");   // <= print "hello" and pass contents to stdoutBranch
    System.err.print("goodbye"); // <= print "goodbye" and pass contents to stderrBranch
} // don't pass contents to branch anymore if it reaches here

System.out.print(stdoutBranch.toString()); // <= print "hello" on stdout
System.err.print(stderrBranch.toString()); // <= print "goodbye" on stderr

Description

This package provides simple functions to capture or tee contents which is sent to STDERR or STDOUT. It turns back to STDOUT or STDERR state which is before capturing or tee when escaping from try-with-resources statement.

This package is inspired by Capture::Tiny from Perl.

Author

moznion ([email protected])

License

The MIT License (MIT)
Copyright © 2014 moznion, http://moznion.net/ <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Versions

Version
1.0.0