Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Quick FilterInputStream newbie question

Posted on 2005-04-04
10
Medium Priority
?
271 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
  • 3
  • 2
  • 2
  • +1
8 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

580 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