Quick FilterInputStream newbie question

Can someone write a FilterInputStream to change all exclamation marks into periods.

Thanks...
LVL 2
Optimal_ITAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
Well with any InputStream You can read one char and if it's exclamation mark replace it with period and continue reading.
Or there is another way: first You read all the document the faster way and only then replace all exclamation marks with periods.
0
objectsCommented:
You should use a FilterReader instead of a FilterInputStream.
You should just need to overide read(), and if read character is a !, return a . otherwise return the read character.
0
frank_van_puffelenCommented:
This seems to work with some simple tests:
      static class ExlamationToDotFilter extends FilterInputStream {
            public ExlamationToDotFilter(InputStream stream) {
                  super(stream);
            }
            public int read() throws IOException {
                  int value = super.read();
                  if ((char)value == '!') {
                        value = '.';
                  }
                  return value;
            }
      };

      public static void main(String[] args) throws Exception {
            String input = "A!B!C.D@!";
            String output = "";
            InputStream stream = new ExlamationToDotFilter(new StringBufferInputStream(input));
            int b;
            while (-1 != (b = stream.read())) {
                  output += (char)b;
            }
            System.out.println(input+" -> "+output);
      }

Given that you're reading Strings, you should really consider using Reader to avoid problems with character encodings.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

frank_van_puffelenCommented:
This is the FilterReader version that both objects and myself seem to prefer:
      static class ExclamationToDotFilter extends FilterReader {
            public ExclamationToDotFilter(Reader input) {
                  super(input);
            }
            public int read() throws IOException {
                  int value = super.read();
                  if ((char)value == '!') {
                        value = '.';
                  }
                  return value;
            }
      }
0
objectsCommented:
import java.io.*;

public class MyReader extends FilterReader
{
   public MyReader(Reader in)
   {
      super(in);
   }

   public int read() throws IOException
   {
      char c = (char) super.read();
      if (c=='!') c = '.';
      return c;
   }
}
0
Optimal_ITAuthor Commented:
Thanks to both  frank_van_puffelen  and objects for the suggestions.

Moderator, can you please assign 500 points to objects too...

I was looking for the FilterInputStream and  frank_van_puffelen  gave me the answer...
0
objectsCommented:
> I was looking for the FilterInputStream and  frank_van_puffelen  gave me the answer...

Readers should be used to deal with character data correctly.
If you have a stream, the you can use a InputStreamReader to convert it to a Reader

Reader rin = new MyReader(new InputStreamReader(myinputstream));
0
Optimal_ITAuthor Commented:
Please split 500 points between frank_van_puffelen  and objects for the suggestions.

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.