Menu

InputStream multiple replacing

Help
2003-02-20
2003-03-05
  • Nobody/Anonymous

    I'm using JRegExp for filtering an InputStream.
    The problem is that I want to filter the content more than once.

    I do it like this:

            for (int i = 0; i < patternCount; i++)
            {
                replacers[i].replace(in, READ_ALL, out);
           }

    But it seems like it only replaces once which is the first time through the loop.
    Is it because I need to mark and reset the stream after calling replace?

    Thanks for a great Regexp engine.

     
    • Sergey A. Samokhodkin

      Hi!
      > But it seems like it only replaces once which is the > first time through the loop.
      > Is it because I need to mark and reset the stream > after calling replace?

      Cannot say something certain on this without knowing the context. Generally, you need to feed each substituion pass by the result of the previous pass.

      For example:

      Reader in=...;
      CharArrayWriter out=new  CharArrayWriter();
      for (int i = 0; i < patternCount; i++)
      {
        replacers[i].replace(in, READ_ALL, out);
        in=new CharArrayReader(out.toCharArray());
      }

      If your 'in' and 'out' are bound to files, you need to
      provide some mechanism for swapping these files between substitutions.

      Regards

       
    • Nobody/Anonymous

      That is what I was looking for. I created a temporary solution by reading the whole InputStream into a String. And the replacing is done on that.

      Will using a CharArrayReader/Writer be more efficient in some way than reading it into a String?

      I guess it is recommended?

       
      • Sergey A. Samokhodkin

        Hi!
        > Will using a CharArrayReader/Writer be more efficient in some way than reading it into a String?

        Definitely, yes.  This will result in less data being copied and arrays allocated. But the real effect may be quite minor (it depends).

        Regards
        Sergey

         
    • Nobody/Anonymous

      One correction.

      I need to call CharArrayWriter.reset() before each replacement. Otherwise the result of the replacement is appended to the previous result.

      For example replacing 'a' with 'b' and '1' with '2' on the following text: 'aa11' resulted in 'bb11bb22' without the reset. With reset I got 'bb22'.

      Like this:

          public void replace(Reader in, Writer out)
              throws Exception
          {
              CharArrayWriter buffer = new CharArrayWriter();

              for (int i = 0; i < pattern.length; i++)
              {
                  buffer.reset();
                 
                  replacer[i].replace(in, READ_ALL, buffer);

                  in = new CharArrayReader(buffer.toCharArray());
              }

              out.write(buffer.toString());
          }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.