【问题标题】:StackOverflow recursion errorStackOverflow 递归错误
【发布时间】:2014-05-04 01:29:16
【问题描述】:

我在这段代码中遇到了 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;
    }
}

它说它与 write(output, ((int) (buffer & 25))); 它在这条线上说了很多次。 有什么想法吗? 谢谢

【问题讨论】:

    标签: for-loop recursion encoding stack-overflow


    【解决方案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;
    }
    

    【讨论】:

    • 你的权利,我刚刚意识到我的意思是为输出流调用 write 方法。输出的文件是空的,所以不确定发生了什么
    猜你喜欢
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多