dongshengyin0147 2016-12-18 12:33
浏览 605
已采纳

如何在Go中的io.Reader上测试EOF?

The Go's io.Reader documentation states that a Read() may return a non zero n value and an io.EOF at the same time. Unfortunately, the Read() method of a File doesn't do that.

When the EOF is reached and some bytes could still be read, the Read method of file returns non zero n and nil error. It is only when we try to read when already at the end of the file that we get back zero n and io.EOF as error.

I couldn't find a simple method to test if the EOF is reached without trying to read data from the file. If we perform a Read() with a buffer of 0 byte, we get back zero n and nil error although we are at the end of file.

To avoid this last read, the only solution I have found is to keep track myself of the number of bytes remaining to read in the file. Is there a simpler solution ?

  • 写回答

1条回答 默认 最新

  • dongtuo4723 2016-12-19 02:51
    关注

    You could create a new type, that keeps track of the number of bytes read so far. Then, at EOF check time, you could compare the expected number of bytes read with the actual number of bytes read. Here is a sample implementation. The eofReader keeps track of the number of bytes read and compares it to the file size, in case the underlying type is a file:

    package main
    
    // ... imports 
    
    // eofReader can be checked for EOF, without a Read. 
    type eofReader struct {
        r     io.Reader
        count uint64
    }
    
    // AtEOF returns true, if the number of bytes read equals the file size.
    func (r *eofReader) AtEOF() (bool, error) {
        f, ok := r.r.(*os.File)
        if !ok {
            return false, nil
        }
        fi, err := f.Stat()
        if err != nil {
            return false, err
        }
        return r.Count() == uint64(fi.Size()), nil
    }
    
    // Read reads and counts.
    func (r *eofReader) Read(buf []byte) (int, error) {
        n, err := r.r.Read(buf)
        atomic.AddUint64(&r.count, uint64(n))
        return n, err
    }
    
    // Count returns the count.
    func (r *eofReader) Count() uint64 {
        return atomic.LoadUint64(&r.count)
    }
    

    You could use this type by wrapping any reader in an eofReader:

    func main() {
        f, err := os.Open("main.go")
        if err != nil {
            log.Fatal(err)
        }
    
        r := &eofReader{r: f}
        log.Println(r.AtEOF())
    
        if _, err = ioutil.ReadAll(r); err != nil {
            log.Fatal(err)
        }
    
        log.Println(r.AtEOF())
    }
    
    // 2016/12/19 03:49:35 false <nil>
    // 2016/12/19 03:49:35 true <nil>
    

    Code as gist.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)