-
Notifications
You must be signed in to change notification settings - Fork 5
/
fileWithSizeDecorator.go
38 lines (30 loc) · 1.03 KB
/
fileWithSizeDecorator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package commands
import "os"
// Implements the SizedReadCloser interface for a file.
// Decorates an os.File and adds a Size function which determines the length of the file
type fileWithSizeDecorator struct {
file *os.File
}
func readFile(path string) (*fileWithSizeDecorator, error) {
content, fileErr := os.Open(path)
if fileErr != nil {
return nil, fileErr
}
return &fileWithSizeDecorator{content}, nil
}
func (fileWithSizeDecorator *fileWithSizeDecorator) Size() (int64, error) {
fi, err := fileWithSizeDecorator.file.Stat()
if err != nil {
return 0, err
}
return fi.Size(), nil
}
func (fileWithSizeDecorator *fileWithSizeDecorator) Read(p []byte) (int, error) {
return fileWithSizeDecorator.file.Read(p)
}
func (fileWithSizeDecorator *fileWithSizeDecorator) Seek(offset int64, whence int) (int64, error) {
return fileWithSizeDecorator.file.Seek(offset, whence)
}
func (fileWithSizeDecorator *fileWithSizeDecorator) Close() error {
return fileWithSizeDecorator.file.Close()
}