-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
333 additions
and
16 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/github/rutledgepaulv/injectingstreams/InjectingStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.github.rutledgepaulv.injectingstreams; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public final class InjectingStreams { | ||
private InjectingStreams() { | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, String delimiter, String injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, String delimiter, byte[] injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, String delimiter, InputStream injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, byte[] delimiter, String injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, byte[] delimiter, byte[] injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectBeforeOutput(OutputStream out, byte[] delimiter, InputStream injection) { | ||
return new PreDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, String delimiter, String injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, String delimiter, byte[] injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, String delimiter, InputStream injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, byte[] delimiter, String injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, byte[] delimiter, byte[] injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
public static OutputStream injectAfterOutput(OutputStream out, byte[] delimiter, InputStream injection) { | ||
return new PostDelimiterInjectingOutputStream(out, delimiter, injection); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
...ain/java/com/github/rutledgepaulv/injectingstreams/PreDelimiterInjectingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.github.rutledgepaulv.injectingstreams; | ||
|
||
import java.io.*; | ||
|
||
|
||
/** | ||
* An output stream that injects a stream of bytes immediately preceding the first series of | ||
* delimiter bytes. | ||
* <p> | ||
* Assumes a single writer (no synchronization) | ||
*/ | ||
public class PreDelimiterInjectingOutputStream extends FilterOutputStream { | ||
|
||
private final InputStream injection; | ||
private boolean injected = false; | ||
private final byte[] delimiter; | ||
private int bufferOffset = 0; | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, String delimiter, String injection) { | ||
this(out, delimiter.getBytes(), injection.getBytes()); | ||
} | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, String delimiter, byte[] injection) { | ||
this(out, delimiter.getBytes(), injection); | ||
} | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, String delimiter, InputStream injection) { | ||
this(out, delimiter.getBytes(), injection); | ||
} | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, byte[] delimiter, String injection) { | ||
this(out, delimiter, injection.getBytes()); | ||
} | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, byte[] delimiter, byte[] injection) { | ||
this(out, delimiter, new ByteArrayInputStream(injection)); | ||
} | ||
|
||
public PreDelimiterInjectingOutputStream(OutputStream out, byte[] delimiter, InputStream injection) { | ||
super(out); | ||
this.delimiter = delimiter; | ||
this.injection = injection; | ||
} | ||
|
||
private void inject() throws IOException { | ||
try (InputStream in = this.injection) { | ||
byte[] buffer = new byte[4096]; | ||
int n; | ||
while (-1 != (n = in.read(buffer))) { | ||
out.write(buffer, 0, n); | ||
} | ||
} finally { | ||
injected = true; | ||
bufferOffset = 0; | ||
} | ||
} | ||
|
||
private void drainBuffer() throws IOException { | ||
if (bufferOffset > 0) { | ||
out.write(delimiter, 0, bufferOffset); | ||
bufferOffset = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
if (!injected) { | ||
// if this byte is the next element of the delimiter | ||
if (b == delimiter[bufferOffset]) { | ||
// increment the buffer offset | ||
bufferOffset++; | ||
// if we've reached the end of the delimiter | ||
if (bufferOffset == delimiter.length) { | ||
// inject the content | ||
inject(); | ||
// inject the buffer | ||
out.write(delimiter); | ||
} | ||
} else { | ||
// flush the pending buffer | ||
this.drainBuffer(); | ||
// if this byte is the first element of another potential delimiter sequence | ||
if (b == delimiter[bufferOffset]) { | ||
bufferOffset++; | ||
} else { | ||
out.write(b); | ||
} | ||
} | ||
} else { | ||
out.write(b); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(byte[] bytes) throws IOException { | ||
this.write(bytes, 0, bytes.length); | ||
} | ||
|
||
@Override | ||
public void write(byte[] bytes, int off, int len) throws IOException { | ||
if (!injected) { | ||
for (int i = off; i < len + off; i++) { | ||
byte b = bytes[i]; | ||
// b matches next position in buffer | ||
if (b == delimiter[bufferOffset]) { | ||
bufferOffset++; | ||
// buffer became full | ||
if (bufferOffset == delimiter.length) { | ||
int length = i - off - delimiter.length + 1; | ||
if (length > 0) { | ||
out.write(bytes, off, length); | ||
} | ||
inject(); | ||
out.write(delimiter); | ||
int nextIndex = i + 1; | ||
if (nextIndex < len) { | ||
int remaining = len - nextIndex - off; | ||
out.write(bytes, nextIndex, remaining); | ||
} | ||
return; | ||
} | ||
} else { | ||
// we read a byte that negates the current match, go ahead and flush | ||
// the imaginary buffer if it might contain bytes from the prior write | ||
if (i - bufferOffset < off) { | ||
this.drainBuffer(); | ||
} | ||
bufferOffset = 0; | ||
// check if this byte is the beginning of the next sequence too | ||
if (b == delimiter[bufferOffset]) { | ||
bufferOffset++; | ||
} | ||
} | ||
} | ||
|
||
// still not injected, need to take care of earlier bytes up to buffer | ||
if (!injected) { | ||
int length = (len - bufferOffset); | ||
if (length > 0) { | ||
out.write(bytes, off, length); | ||
} | ||
} | ||
} else { | ||
out.write(bytes, off, len); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
this.drainBuffer(); | ||
try { | ||
injection.close(); | ||
} finally { | ||
super.close(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...java/com/github/rutledgepaulv/injectingstreams/PreDelimiterInjectingOutputStreamTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.github.rutledgepaulv.injectingstreams; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.nio.charset.Charset.defaultCharset; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class PreDelimiterInjectingOutputStreamTest { | ||
|
||
@Test | ||
public void constructors() { | ||
new PreDelimiterInjectingOutputStream(new ByteArrayOutputStream(), "Test", "Test".getBytes()); | ||
new PreDelimiterInjectingOutputStream(new ByteArrayOutputStream(), "Test".getBytes(), "Test"); | ||
new PreDelimiterInjectingOutputStream(new ByteArrayOutputStream(), "Test".getBytes(), "Test".getBytes()); | ||
new PreDelimiterInjectingOutputStream(new ByteArrayOutputStream(), "Test", new ByteArrayInputStream("Test".getBytes())); | ||
new PreDelimiterInjectingOutputStream(new ByteArrayOutputStream(), "Test".getBytes(), new ByteArrayInputStream("Test".getBytes())); | ||
} | ||
|
||
@Test | ||
public void fuzzingFirstCharacter() throws IOException { | ||
|
||
for (int i = 0; i < 10000; i++) { | ||
ByteArrayOutputStream rawOut = new ByteArrayOutputStream(); | ||
fuzzyWrite(new PreDelimiterInjectingOutputStream(rawOut, "b", "world "), "before hello after"); | ||
String finalOutput = new String(rawOut.toByteArray(), defaultCharset()); | ||
assertEquals("world before hello after", finalOutput); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void fuzzingSingleCharacterDelimiter() throws IOException { | ||
|
||
for (int i = 0; i < 100000; i++) { | ||
ByteArrayOutputStream rawOut = new ByteArrayOutputStream(); | ||
fuzzyWrite(new PreDelimiterInjectingOutputStream(rawOut, "h", "world "), "before hello after"); | ||
String finalOutput = new String(rawOut.toByteArray(), defaultCharset()); | ||
assertEquals("before world hello after", finalOutput); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void fuzzing() throws IOException { | ||
|
||
for (int i = 0; i < 10000; i++) { | ||
ByteArrayOutputStream rawOut = new ByteArrayOutputStream(); | ||
fuzzyWrite(new PreDelimiterInjectingOutputStream(rawOut, "hello ", "world "), "before hello after"); | ||
String finalOutput = new String(rawOut.toByteArray(), defaultCharset()); | ||
assertEquals("before world hello after", finalOutput); | ||
} | ||
|
||
} | ||
|
||
public void fuzzyWrite(OutputStream stream, String content) throws IOException { | ||
List<byte[]> parts = partitions(content); | ||
String combined = parts.stream().map(String::new).reduce((s1, s2) -> s1 + s2).get(); | ||
assertEquals(content, combined); | ||
try (OutputStream out = stream) { | ||
for (byte[] bites : parts) { | ||
if (bites.length == 1) { | ||
out.write(bites[0]); | ||
} else { | ||
out.write(bites); | ||
} | ||
} | ||
} | ||
} | ||
|
||
public List<byte[]> partitions(String content) { | ||
Random random = new Random(System.nanoTime()); | ||
int offset = 0; | ||
List<String> partitions = new ArrayList<>(); | ||
while (offset < content.length()) { | ||
int length = Math.min(random.nextInt(content.length() - offset) + 1, content.length()); | ||
partitions.add(content.substring(offset, offset + length)); | ||
offset += length; | ||
} | ||
return partitions.stream().map(String::getBytes).collect(Collectors.toList()); | ||
} | ||
} |