StackOverflow递归错误

时间:2014-05-03 17:22:11

标签: for-loop recursion encoding stack-overflow

我在此代码中收到StackOverflow错误:

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;
    }
}

它表示它与写入行(output,((int)(buffer&amp; 25))); 它说它在这条线上载满了很多次。 有什么想法发生了什么? 感谢

1 个答案:

答案 0 :(得分:1)

由于除了调用方法本身之外,你实际上并没有在方法中使用output,我认为你真的试图调用output.write(int)并且没有调用方法本身;

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;
}