java 底层流返回零字节

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10917814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:04:46  来源:igfitidea点击:

Underlying stream returned zero bytes

javaioinputstreamfilestream

提问by IchBinKeinBaum

I'm currently writing my own Brainfwor interpreter in Java and because I want it to be able to read code from files I wrote a BFInputStreamthat filters unnecessary symbols out. It looks like this:

我目前正在用 Java 编写自己的 Brainfwor 解释器,因为我希望它能够从文件中读取代码,所以我编写了一个BFInputStream过滤掉不必要符号的文件。它看起来像这样:

import java.io.FilterInputStream;
import java.io.InputStream;
import java.io.IOException;

public class BFInputStream extends FilterInputStream {
    public BFInputStream(InputStream in) {
        super(in);
    }
    public int read() throws IOException {
        while(true) {
            int i = super.read();
            // keep LF for line number checking.
            if(i == -1 || i == 10 ||( i >= 43 && i <= 46) || i == 60 || i == 62 || i == 91 || i == 93)
                return i;
        }
    }
    public int read(byte[] b, int off, int len) throws IOException {
        if(off < 0 || len < 0 || len > b.length - off) throw new IndexOutOfBoundsException();
        for(int i=0; i<len; i++) {
            int j = read();
            if(j < 1) return i;
            b[off+i] = (byte)j;
        }
        return len;
    }
}

My Interpreter uses LineNumberReader<-InputStreamReader<-BFInputStream<-FileInputStreamto read the file. But everytime it reaches the end of the file it throws:

我的解释器使用LineNumberReader<- InputStreamReader<- BFInputStream<-FileInputStream来读取文件。但每次到达文件末尾时,它都会抛出:

java.io.IOException: Underlying input stream returned zero bytes

  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
  at java.io.InputStreamReader.read(InputStreamReader.java:167)
  at java.io.BufferedReader.fill(BufferedReader.java:136)
  at java.io.BufferedReader.read(BufferedReader.java:157)
  at java.io.LineNumberReader.read(LineNumberReader.java:108)
  at Interpreter.run(Interpreter.java:101)
  at Interpreter.main(Interpreter.java:180)

java.io.IOException:底层输入流返回零字节

  at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
  at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
  at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
  at java.io.InputStreamReader.read(InputStreamReader.java:167)
  at java.io.BufferedReader.fill(BufferedReader.java:136)
  at java.io.BufferedReader.read(BufferedReader.java:157)
  at java.io.LineNumberReader.read(LineNumberReader.java:108)
  at Interpreter.run(Interpreter.java:101)
  at Interpreter.main(Interpreter.java:180)

Interpreter.java:101 contains a call to BFInputStream.read().

Interpreter.java:101 包含对 的调用BFInputStream.read()

I'm not sure if I understand the exception correctly. I think the stream returned no bytes at all (because of a timeout). I guess if there was a huge chunk of non-BF characters then that could be possible but at the end of the file? FileInputStreamand therefore FilterInputStreamand BFInputStreamshould return -1.

我不确定我是否正确理解异常。我认为流根本没有返回字节(因为超时)。我想如果有一大块非 BF 字符,那么这可能是可能的,但是在文件的末尾?FileInputStream因此FilterInputStream并且BFInputStream应该返回-1。

What is wrong with my code?

我的代码有什么问题?

回答by axtavt

if (j < 1)should be if (j < 0), because EOF is indicated by -1. Also, your method never returns -1 at EOF.

if (j < 1)应该是if (j < 0),因为 EOF 由 表示-1。此外,您的方法永远不会在 EOF 返回 -1。

The correct implementation would look like this:

正确的实现如下所示:

public int read(byte[] b, int off, int len) throws IOException {
    if(off < 0 || len <= 0 || len > b.length - off) throw new IndexOutOfBoundsException();         
    int i = 0;
    while (i < len) {
        int j = read();
        if (j < 0) break; // Stop reading at EOF
        b[off + i] = (byte) j;
        i++;
    }
    if (i == 0) return -1; // If we get EOF with no data, return it to the caller
    else return i;
}

回答by Kumar Vivek Mitra

Well did same kinda thing before but using File, FileReader/Writer and BufferReader/Writer. I am giving you the snippet of code i use as guidelines.. try it

之前也做过同样的事情,但使用 File、FileReader/Writer 和 BufferReader/Writer。我给你我用作指导的代码片段..试试看

For Reading from a file

用于从文件中读取

File f = new File("my.txt");
FileReader fr = new FileReader(f);
BufferedReader br  = new BufferedReader(fr);

String s = null;

while ((br=readLine())!=null) {

// Do whatever u want to do with the content of the file.

}

br.close();

For Writing to a file:

写入文件:

Boolean isDone = true;
Scanner scan = new Scanner(System.in);
File f = new File("my.txt");
FileWriter fr = new FileWriter(f);
BufferedWriter br  = new BufferedWriter(fr);

while (b) {

   if (!b) {

 br.write(new Scanner(System.in).nextLine());

 }


}