Skip to content Skip to sidebar Skip to footer

Change Inputstream By Applying Regex On It

I have an InputStream downloaded from the internet. And I need to apply regex on it - so that all occurrences of that regex would be changed to string, wich is provided. I need an

Solution 1:

Managed to get it working with github.com/rwitzel/streamflyer library. if app.gradle we have:

compile'com.github.rwitzel.streamflyer:streamflyer-core:1.2.0';

Example:

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;

import com.github.rwitzel.streamflyer.core.ModifyingReader;
import com.github.rwitzel.streamflyer.regex.RegexModifier;

publicclassInputStreamModifiedWithRegex {
    privatestaticfinalCharsetENCODE_CHARSET= Charset.forName("UTF-8");

    publicstaticvoidmain(String[] args)throws IOException {
        InputStreaminput= IOUtils.toInputStream("AB CD EF");
        InputStreamupdatedInput= applyRegex(input, "[A-C]", "Z");
        System.out.println(IOUtils.toString(updatedInput, ENCODE_CHARSET));
    }

    privatestatic InputStream applyRegex(InputStream inputStream, String pattern, String changeString)throws UnsupportedEncodingException {
        ReaderoriginalReader=newInputStreamReader(inputStream, ENCODE_CHARSET);
        ReadermodifyingReader=newModifyingReader(originalReader, newRegexModifier(pattern, 0, changeString));
        inputStream = newReaderInputStream(modifyingReader, ENCODE_CHARSET);

        return inputStream;
    }
}

Post a Comment for "Change Inputstream By Applying Regex On It"