-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More efficient filtering of input streams
- Loading branch information
Showing
3 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
src/main/java/com/cloudbees/jenkins/support/filter/FilteredInputStream.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,65 @@ | ||
package com.cloudbees.jenkins.support.filter; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.Charset; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* A custom {@link InputStream} that filters lines of text read from an underlying {@link InputStream} using a provided | ||
* mapping function. Each line read from the input stream is processed by the function before being returned, allowing | ||
* for modifications such as redaction or transformation of the line content. | ||
* | ||
* @author Basil Crow | ||
*/ | ||
public class FilteredInputStream extends InputStream { | ||
|
||
private final Function<String, String> filter; | ||
private final Charset encoding; | ||
private final BufferedReader reader; | ||
private ByteArrayInputStream buffer; | ||
|
||
/** | ||
* Constructs a filtered stream using the provided filter and encoding. | ||
* | ||
* @param is Input stream to read line content from | ||
* @param encoding Character set to use for decoding and encoding bytes read from this stream | ||
* @param filter Filter to apply to lines read from this stream | ||
*/ | ||
public FilteredInputStream( | ||
@NonNull InputStream is, @NonNull Charset encoding, @NonNull Function<String, String> filter) { | ||
this.encoding = encoding; | ||
this.reader = new BufferedReader(new InputStreamReader(is, encoding)); | ||
this.filter = filter; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (buffer == null) { | ||
String line = reader.readLine(); | ||
if (line != null) { | ||
line = filter.apply(line); | ||
line += System.lineSeparator(); | ||
buffer = new ByteArrayInputStream(line.getBytes(encoding)); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
int ch = buffer.read(); | ||
if (ch != -1) { | ||
return ch; | ||
} else { | ||
buffer = null; | ||
return read(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
reader.close(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/test/java/com/cloudbees/jenkins/support/filter/FilteredInputStreamTest.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,51 @@ | ||
package com.cloudbees.jenkins.support.filter; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.Test; | ||
|
||
public class FilteredInputStreamTest { | ||
|
||
@Test | ||
public void testReadSingleLine() throws IOException { | ||
String input = "foo bar"; | ||
String output; | ||
try (FilteredInputStream fis = new FilteredInputStream( | ||
new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), | ||
StandardCharsets.UTF_8, | ||
String::toUpperCase)) { | ||
output = new String(fis.readAllBytes(), StandardCharsets.UTF_8); | ||
} | ||
assertEquals("FOO BAR" + System.lineSeparator(), output); | ||
} | ||
|
||
@Test | ||
public void testReadMultipleLines() throws IOException { | ||
String input = "Line 1\nLine 2\nLine 3"; | ||
String output; | ||
try (FilteredInputStream fis = new FilteredInputStream( | ||
new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)), | ||
StandardCharsets.UTF_8, | ||
String::toUpperCase)) { | ||
output = new String(fis.readAllBytes(), StandardCharsets.UTF_8); | ||
} | ||
assertEquals( | ||
"LINE 1" + System.lineSeparator() + "LINE 2" + System.lineSeparator() + "LINE 3" | ||
+ System.lineSeparator(), | ||
output); | ||
} | ||
|
||
@Test | ||
public void testReadEmptyStream() throws IOException { | ||
byte[] input = new byte[0]; | ||
String output; | ||
try (FilteredInputStream fis = | ||
new FilteredInputStream(new ByteArrayInputStream(input), StandardCharsets.UTF_8, String::toUpperCase)) { | ||
output = new String(fis.readAllBytes(), StandardCharsets.UTF_8); | ||
} | ||
assertEquals("", output); | ||
} | ||
} |