0

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)

4
  • 1
    are you using the test object for the end result? Commented Feb 12, 2020 at 13:01
  • 1
    You did use test as InputStream i.o. input? Like: InputStream oldInput; InputStream input = new ReplacingInputStream(oldIInput, "\\s+", " "); Commented Feb 12, 2020 at 13:02
  • I've updated for clarity. Commented Feb 12, 2020 at 13:09
  • Check if this question helps Commented Feb 12, 2020 at 14:16

1 Answer 1

1

It looks like the 'pattern' parameter is not a Regexp pattern at all and even not a wildcard. It's just a string-to-string replacement.

If we're talking about 'org.apache.poi.util.ReplacingInputStream'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.