Question

I'm getting a StackOverflow error in this code:

public void write(OutputStream output, int code) throws IOException{
    code = (code & (1 << 12) - 1) << ((writtenToOutput) * 12);
    buffer |= code;
    writtenToOutput++;

    if(writtenToOutput >= bufUsageSymbols){
        for(int i = 0; i < bufUsageBytes; i++){
            write(output, ((int) (buffer & 255)));
            buffer >>= 8;
        }

        writtenToOutput = 0;
        buffer = 0;
    }
}

It says its on the line with the write(output, ((int) (buffer & 25))); It says its on this line loads of times. Any ideas what's going on? Thanks

Was it helpful?

Solution

Since you're not actually using output inside the method besides to call the method itself, I assume you're really trying to call output.write(int) and not have the method call itself;

for(int i = 0; i < bufUsageBytes; i++){

    // will call the method you're already in, probably not intended
    // write(output, ((int) (buffer & 255))); 

    // will call write on the outputstream, probably what you're intending
    output.write((int) (buffer & 255));      

    buffer >>= 8;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top