Click here to Skip to main content
15,947,958 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello)

So I have the read(read[] ) method overridden on my CustomInputStream which extends the FilterInputStream. The programm works wxactly the way I want it to.
I was wondering how does it even work if it says "return 0"?
What is the concept behind this?


Java
public class CustomInput extends FilterInputStream{
 
    protected CustomInput(InputStream in) {
        super(in);
 
    @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;
    }


What I have tried:

Honestly , It works fine, It is just that I could not understand why does It work?
Posted
Updated 4-Nov-21 23:43pm
v2

You already posted this question at Overriding methods for a custom filter input stream in java[^], and I gave you a solution. Please do not repost.
 
Share this answer
 
FilterInputStream.read(byte[])[^]

The method returns the total number of bytes read. If your method returns 0 and the calling code still works, then there is a bug in the calling code.

You need to store the value returned by the super method call, use that as the limit in your loop, and return it.
Java
@Override
public int read(byte[] b) throws IOException{
    int bytesRead = super.read(b);
    for (int i = 0; i < bytesRead; i++){
        if (b[i] ==65){
            b[i] = (byte) 194;
        } else if(b[i]==101) {
            b[i]=(byte) 233;
        }
    }
    return bytesRead;
}
 
Share this answer
 

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