readerfile.go 966 Bytes
Newer Older
1 2
package files

3
import (
4
	"errors"
5 6 7
	"io"
	"os"
)
8 9 10 11

// ReaderFile is a implementation of File created from an `io.Reader`.
// ReaderFiles are never directories, and can be read from and closed.
type ReaderFile struct {
12 13 14 15 16 17 18
	filename string
	reader   io.ReadCloser
	stat     os.FileInfo
}

func NewReaderFile(filename string, reader io.ReadCloser, stat os.FileInfo) *ReaderFile {
	return &ReaderFile{filename, reader, stat}
19 20 21 22 23 24 25 26 27 28 29
}

func (f *ReaderFile) IsDirectory() bool {
	return false
}

func (f *ReaderFile) NextFile() (File, error) {
	return nil, ErrNotDirectory
}

func (f *ReaderFile) FileName() string {
30
	return f.filename
31 32 33
}

func (f *ReaderFile) Read(p []byte) (int, error) {
34
	return f.reader.Read(p)
35 36 37
}

func (f *ReaderFile) Close() error {
38 39 40 41 42
	return f.reader.Close()
}

func (f *ReaderFile) Stat() os.FileInfo {
	return f.stat
43
}
44 45 46 47 48 49 50

func (f *ReaderFile) Size() (int64, error) {
	if f.stat == nil {
		return 0, errors.New("File size unknown")
	}
	return f.stat.Size(), nil
}