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.
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);
}
<dependencies>
<dependency>
<groupId>com.github.rutledgepaulv</groupId>
<artifactId>injecting-streams</artifactId>
<version>2.0</version>
</dependency>
</dependencies>