Skip to content

Conversation

emcfarlane
Copy link
Contributor

Using io.ReadFull() changes the error message to io.ErrUnexpectedEOF instead of the expected io.EOF. This fixes the behavior to be handled like os.FIle https://golang.org/pkg/os/#File.ReadAt

@colinmarc
Copy link
Owner

Good call! Tested with this code:

package main

import "os"

func main() {
	// foo.txt contains "foobar"
	f, err := os.Open("foo.txt")

	if err != nil {
		panic(err)
	}

	b := make([]byte, 10)
	_, err = f.ReadAt(b, 3)
	panic(err)
}

@colinmarc colinmarc closed this in 7cb3ffe May 11, 2018
@colinmarc
Copy link
Owner

Needed to make some tweaks - this change returned nil, not io.EOF, and we want the behavior of io.ReadFull where it calls Read in a loop.

@emcfarlane
Copy link
Contributor Author

@colinmarc awesome thanks, bit confused why the Read didn't work? Should this not behave like Read() with a seek method?

@emcfarlane
Copy link
Contributor Author

https://golang.org/pkg/os/#File.Read I think Read() should return io.EOF.

@colinmarc
Copy link
Owner

@afking File.ReadAt actually calls Read in a loop, just like ReadFull:

for len(b) > 0 {
  		m, e := f.pread(b, off)
  		if e != nil {
  			err = f.wrapErr("read", e)
  			break
  		}
  		n += m
  		b = b[m:]
  		off += int64(m)
  	}

And Read doesn't have to return io.EOF on a partial read - it can just return n < len(b) and nil.

@colinmarc
Copy link
Owner

https://golang.org/pkg/os/#File.Read I think Read() should return io.EOF.

It doesn't until the second call to Read. Try running this code:

package main

import "os"
import "log"

func main() {
	// foo.txt contains "foobar"
	f, err := os.Open("foo.txt")

	if err != nil {
		panic(err)
	}

	b := make([]byte, 10)
	n, err := f.Read(b)
	log.Fatal(n, err)
}

I get 7 <nil>.

@emcfarlane
Copy link
Contributor Author

@colinmarc yep it does 👍 thanks for taking the time to clarify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants