forked from bmaupin/go-epub
/
file.go
76 lines (62 loc) · 1.11 KB
/
file.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package memory
import (
"io"
"io/fs"
"time"
)
type file struct {
name string
modTime time.Time
offset int
content []byte
mode fs.FileMode
}
func (f *file) Info() (fs.FileInfo, error) {
return f, nil
}
func (f *file) Stat() (fs.FileInfo, error) {
return f, nil
}
func (f *file) Read(b []byte) (int, error) {
length := len(f.content)
start := f.offset
if start == length {
return 0, io.EOF
}
end := start + len(b)
if end > length {
end = length
}
f.offset = end
count := copy(b, f.content[start:end])
return count, nil
}
func (f *file) Close() error {
f.offset = 0
return nil
}
func (f *file) Write(p []byte) (n int, err error) {
f.content = append(f.content, p...)
return len(p), nil
}
func (f *file) Name() string {
return f.name
}
func (f *file) Size() int64 {
return int64(len(f.content))
}
func (f *file) Type() fs.FileMode {
return f.mode & fs.ModeType
}
func (f *file) Mode() fs.FileMode {
return f.mode
}
func (f *file) ModTime() time.Time {
return f.modTime
}
func (f *file) IsDir() bool {
return f.mode&fs.ModeDir != 0
}
func (f *file) Sys() interface{} {
return nil
}