Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substitute chars on Java 1.4 InputStream

I have an InputStream that is returning, for example:

<?xml version='1.0' ?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><bbs:rule xmlns:bbs="http://com.foo/bbs">

I then pass the stream to a method that return a byte array. I'd like to substitute "com.foo" with something else, like "org.bar" before I pass to the byte[] method.

What is a good way to do that?

like image 751
bmw0128 Avatar asked Mar 08 '26 18:03

bmw0128


2 Answers

If you have a bytearray you can transform it into a String. Pay attention to the encoding, in the example I use utf-8. I think this is a simple way to do that:

String newString = new String(byteArray, "utf-8");
newString = newString.replace("com.foo", "org.bar");
return newString.getBytes("utf-8");
like image 180
javanna Avatar answered Mar 11 '26 07:03

javanna


One way is to wrap your InputStream in your own FilterInputStream subclass that does the transformation on the fly. It will have to be a look-ahead stream that checks every "c" character to see if it is followed by "om.foo" and if so make the substitution. You'll probably have to override just the read() method.

like image 44
Ted Hopp Avatar answered Mar 11 '26 07:03

Ted Hopp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!