-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
49 lines (39 loc) · 743 Bytes
/
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
package utils
import (
"bufio"
"bytes"
"fmt"
"io"
"os"
"github.com/macinnir/dvc/core/lib/utils/errors"
)
const (
chunkSize = 1024
)
// OpenFile opens a file in chunks
func OpenFile(filePath string) (byteCount int, buffer *bytes.Buffer, e error) {
var part []byte
var data *os.File
var count int
data, e = os.Open(filePath)
if e != nil {
return
}
defer data.Close()
reader := bufio.NewReader(data)
buffer = bytes.NewBuffer(make([]byte, 0))
part = make([]byte, chunkSize)
for {
if count, e = reader.Read(part); e != nil {
break
}
buffer.Write(part[:count])
}
if e != io.EOF {
e = errors.NewError(fmt.Sprint("Error Reading ", filePath, ": ", e))
return
}
e = nil
byteCount = buffer.Len()
return
}