I have an InputStream and I need to apply regex on it. I need an OutputStream as a return value since it should be forwarded to api. I do this:
// get byte stream
InputStream input = ...;
OutputStream output;
ReplacingInputStream test = new ReplacingInputStream(input, "\\s+", " ");
String inputText = IOUtils.toString(test, StandardCharsets.UTF_8);
System.out.println(inputText); // nothing is changed
but nothing is changed in input. I couldn't figure out how to do it, so I did
String inputText = IOUtils.toString(input, StandardCharsets.UTF_8);
inputText = inputText.replaceAll("\\s+", " ").trim(); //
System.out.println(inputText); // Changed
This is quick and easy, but of course does not scale nicely to large streams (because perhaps run out of memory)
testobject for the end result?testasInputStreami.o.input? Like:InputStream oldInput; InputStream input = new ReplacingInputStream(oldIInput, "\\s+", " ");