-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathmain.go
More file actions
137 lines (109 loc) · 2.72 KB
/
main.go
File metadata and controls
137 lines (109 loc) · 2.72 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
// "sync"
)
func bufferRead(file string, buffersize int) []byte {
data := []byte{}
fi, err := os.Open(file)
if err != nil {
panic(err)
}
// make a read buffer
r := bufio.NewReader(fi)
// make a buffer to keep chunks that are read
buf := make([]byte, buffersize)
for {
n, err := r.Read(buf)
if err != nil && err != io.EOF {
fi.Close()
panic(err)
}
if n == 0 {
break
} else {
fmt.Println("///////////////////////////////////////////////")
fmt.Println(string(buf))
data = append(data, buf...)
}
}
fmt.Println("///////////////////////////////////////////////")
fi.Close()
return data
}
// func parallel() {
// const BufferSize = 100
// file, err := os.Open("./main.go")
// if err != nil {
// fmt.Println(err)
// return
// }
// defer file.Close()
// fileinfo, err := file.Stat()
// if err != nil {
// fmt.Println(err)
// return
// }
// filesize := int(fileinfo.Size())
// // Number of go routines we need to spawn.
// concurrency := filesize / BufferSize
// // check for any left over bytes. Add one more go routine if required.
// if remainder := filesize % BufferSize; remainder != 0 {
// concurrency++
// }
// var wg sync.WaitGroup
// wg.Add(concurrency)
// for i := 0; i < concurrency; i++ {
// go func(chunksizes []chunk, i int) {
// defer wg.Done()
// chunk := chunksizes[i]
// buffer := make([]byte, chunk.bufsize)
// bytesread, err := file.ReadAt(buffer, chunk.offset)
// // As noted above, ReadAt differs slightly compared to Read when the
// // output buffer provided is larger than the data that's available
// // for reading. So, let's return early only if the error is
// // something other than an EOF. Returning early will run the
// // deferred function above
// if err != nil && err != io.EOF {
// fmt.Println(err)
// return
// }
// fmt.Println("bytes read, string(bytestream): ", bytesread)
// fmt.Println("bytestream to string: ", string(buffer[:bytesread]))
// }(chunksizes, i)
// }
// wg.Wait()
// }
func bufferedReadFile(fileLocation string, buffersize int) []byte {
file, err := os.Open(fileLocation)
if err != nil {
fmt.Println(err)
return nil
}
defer file.Close()
output := []byte{}
buffer := make([]byte, buffersize)
for {
bytesread, err := file.Read(buffer)
if err != nil {
if err != io.EOF {
fmt.Println(err)
}
break
}
output = append(output, buffer[:bytesread]...)
}
return output
}
func main() {
// data := bufferRead("./main.go", 300)
// fmt.Println(len(data))
// res := bufferedReadFile("./textfile.json", 300000)
res, _ := ioutil.ReadFile("./textfile.json")
fmt.Println(string(res))
// parallel()
}