RutledgePaulV / injecting-streams

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Build Status Coverage Status Maven Central

Injecting Streams

Implements a java.io.OutputStream that injects bytes as data is written to the original output stream. Bytes are injected before or after a delimiter of your choice (an array of known bytes). Correct for all uses of the returned stream, carefully implemented for optimal efficiency, and has zero dependencies.

Usage

You might use this to inject additional markup into a stream of html content without having to otherwise parse the markup or convert back into text.

var source       = new FileInputStream("original.html");
var sink         = new FileOutputStream("modified.html");
var modifiedSink = InjectingStreams.injectAfterOutput(sink, "<head>", "<script>alert('hello, world')</script>");

try {
    IOUtils.copy(source,modifiedSink);
} finally {
    IOUtils.close(source);
    IOUtils.close(modifiedSink);
}
var source       = new FileInputStream("original.html");
var sink         = new FileOutputStream("modified.html");
var modifiedSink = InjectingStreams.injectBeforeOutput(sink, "</body>", "<script>alert('hello, world')</script>");

try {
    IOUtils.copy(source,modifiedSink);
} finally {
    IOUtils.close(source);
    IOUtils.close(modifiedSink);
}

Installation

<dependencies>
    <dependency>
        <groupId>com.github.rutledgepaulv</groupId>
        <artifactId>injecting-streams</artifactId>
        <version>2.0</version>
    </dependency>
</dependencies>

About


Languages

Language:Java 93.7%Language:Shell 6.3%