?
Solved

Quick FilterInputStream newbie question

Posted on 2005-04-04
10
Medium Priority
?
270 Views
Last Modified: 2008-02-01
Can someone write a FilterInputStream to change all exclamation marks into periods.

Thanks...
0
Comment
Question by:Optimal_IT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:StillUnAware
ID: 13704465
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
 
LVL 92

Expert Comment

by:objects
ID: 13704496
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
 
LVL 1

Accepted Solution

by:
frank_van_puffelen earned 1000 total points
ID: 13704509
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 13704524
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
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 13704528
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
 
LVL 2

Author Comment

by:Optimal_IT
ID: 13706370
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
 
LVL 92

Expert Comment

by:objects
ID: 13712492
> 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
 
LVL 2

Author Comment

by:Optimal_IT
ID: 13713102
Please split 500 points between frank_van_puffelen  and objects for the suggestions.

Thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question