Click here to Skip to main content
15,176,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have stumbled upon this problem:

So I have created a class for a custom filtered input stream and overridden the read() method. I also need to override the read(byte[] b) method in a way that it uses the criteria that I had defined for the read() method but it does not do. I have tried many ways.
Java
public class CustomInput extends FilterInputStream{

    protected CustomInput(InputStream in) {
        super(in);
    }
    
    @Override
    public int read() throws IOException {
        int y=super.read();
        if(y==65) {
            return 194;
        } else {
            return y;
        }
    }

    @Override
    public int read(byte[] b) throws IOException{
    }
}

I do not know how to make each byte that the method reads whether it is a 65 and return a 194, and if it is not a 65 return whatever it reads just like I have defined in the read() method.

What I have tried:

I have tried calling the read() method for each byte but I can not get it working.

I made it work using a while loop but I really want to have a separate custom method for the read(byte[] b) method.
Posted
Updated 4-Nov-21 3:21am
v2
Comments
Richard MacCutchan 4-Nov-21 5:18am
   
Don't use numbers to represent character values. Use the proper characters, e.g. 'A' and 'Â', so people can understand what the code is supposed to be doing.

Also, you have not explained what the actual problem is.

1 solution

You cannot use your read method as it gets its input from super.read. So you need to do the same for an array:
Java
public int read(byte[] b) throws IOException{
    super.read(b);
    for (int i = 0; i < b.length; ++i){
        if (b[i] = 'A'){
            b[i] = 'Â';
        }
    }
}
   
Comments
Eric1899599 4-Nov-21 18:09pm
   
Thank you very much.
I have changed it a little and it worked like this.

I was just wondering why does it still work if it says "return 0".
It does not work if I do not put a return statement obviously.

@Override
public int read(byte[] b) throws IOException{
super.read(b);
for (int i = 0; i < b.length; i++){
if (b[i] ==65){
b[i] = (byte) 194;
} if(b[i]==101) {
b[i]=(byte) 233;
}
}
return 0;
}
Richard MacCutchan 5-Nov-21 4:04am
   
Why are you still using decimal numbers instead of characters in your code?

The reason it will not work if you do not have return 0 at the end is because the method's declaration states that it returns an integer. So maybe you should check the documentation to see what actual value it is supposed to return.
Eric1899599 6-Nov-21 9:56am
   
Thank you very much for your response, could you please tell me why I should use characters instead of decimal numbers?
Richard MacCutchan 6-Nov-21 10:12am
   
Because it is a) common sense to use the correct variable type, and b) it makes your code easier to understand. How many people looking at that would know that 194 represents the character 'Â'?
Eric1899599 6-Nov-21 11:06am
   
Got that, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900