当读取文件时,为什么ReadBytes会影响后续的Read操作?

huangapple go评论82阅读模式
英文:

When reading file, Why ReadBytes affect the following Read?

问题

当以块方式读取文件时,使用reader.Read方法,每次调用读取32KB的数据。
但是如果使用reader.ReadBytes方法,下一次调用reader.Read时读取的字节数会减少到3KB。

另一个问题是:为什么使用reader.Readbufio.NewReaderSize无法读取超过32KB的数据?

我的代码:

func SomeFunc() {
    gzfile, err := os.Open(SomeFile)
    ErrorPanic(err)
    input_file, err := gzip.NewReader(gzfile)
    ErrorPanic(err)
    // reader := bufio.NewReaderSize(input_file, 120 * 1024 * 1024) //120MB
    reader := bufio.NewReader(input_file)

    .....

    eof := false
    for !eof {
        var n int
        // line := make([]byte, 100 * 1024 * 1024) //100MB
        line := make([]byte, 32 * 1024) //32KB
        n, err = reader.Read(line)
        eof = is_eof(err)
        _, err = buffer.Write(line)
        /***/ fmt.Println("len(line)=", len(line), "; n=", n)
        line = []byte{}
        ErrorNotice(err)

        eos := false
        for !eof && !eos {
            line, err = reader.ReadBytes('\n')
            eof = is_eof(err)

            if SomeCond {
                .....

                buffer.Reset()
                eos = true
            }
            _, err = buffer.Write(line)
            ErrorNotice(err)
        }
    }
}

func ErrorPanic(err error) .....
func ErrorNotice(err error) .....
func is_eof(err error) bool .....

调用ReadBytes('\n')时的输出结果:

len(line)= 32768 ; n= 32768
len(line)= 32768 ; n= 3782
len(line)= 32768 ; n= 2966
len(line)= 32768 ; n= 2835
len(line)= 32768 ; n= 2756
len(line)= 32768 ; n= 3513
len(line)= 32768 ; n= 2583
len(line)= 32768 ; n= 3247

不调用ReadBytes('\n')时的输出结果:

len(line)= 32768 ; n= 32768
len(line)= 32768 ; n= 32768
len(line)= 32768 ; n= 32768
英文:

When reading file by chunks, using reader.Read, it reads 32KB for each calling.
But if I use reader.ReadBytes, it reduces the number of bytes reads by next reader.Read to 3KB.

Another question: Why can't I read more than 32KB using reader.Read and bufio.NewReaderSize ?

My Code:

func SomeFunc () {
	gzfile , err := os.Open(SomeFile)
	ErrorPanic (err)
	input_file , err := gzip.NewReader(gzfile)
	ErrorPanic (err)
	// reader := bufio.NewReaderSize(input_file, 120 * 1024 * 1024) //120MB
	reader := bufio.NewReader(input_file)

	.....

	eof := false
	for !eof {
		var n int
		// line := make ([]byte,100 * 1024 * 1024) //100MB
		line := make ([]byte,32 * 1024) //32KB
		n , err = reader.Read (line)
		eof = is_eof (err)
		_ , err = buffer.Write (line)
	/***/ fmt.Println("len (line)= " , len (line) , " ; n= ",n)
		line = []byte{}
		ErrorNotice (err)

		eos := false
		for !eof && !eos {
			line , err = reader.ReadBytes('\n')
			eof = is_eof (err)

			if SomeCond {
				.....

				buffer.Reset()
				eos = true
			}
			_ , err = buffer.Write (line)
			ErrorNotice (err)
		}
	}
}

func ErrorPanic(err error) .....
func ErrorNotice (err error) .....
func is_eof (err error) bool .....

The output when calling ReadBytes('\n'):

len (line)=  32768  ; n=  32768
len (line)=  32768  ; n=  3782
len (line)=  32768  ; n=  2966
len (line)=  32768  ; n=  2835
len (line)=  32768  ; n=  2756
len (line)=  32768  ; n=  3513
len (line)=  32768  ; n=  2583
len (line)=  32768  ; n=  3247

The output when not calling ReadBytes('\n'):

len (line)=  32768  ; n=  32768
len (line)=  32768  ; n=  32768
len (line)=  32768  ; n=  32768

答案1

得分: 1

因为它是这样设计的。在读取时可以读取任意字节数。认为reader是一个接口,而reader.Read()需要满足所有的实现。

英文:

Because it designed like this. On read may read any byte count. thinking reader is a interface, and the reader.Read() need satisfy all implement.

huangapple
  • 本文由 发表于 2015年7月31日 07:11:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/31735589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定