forked from projectdiscovery/katana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_writer.go
39 lines (34 loc) · 885 Bytes
/
file_writer.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
package output
import (
"bufio"
"os"
)
// fileWriter is a concurrent file based output writer.
type fileWriter struct {
file *os.File
writer *bufio.Writer
}
// NewFileOutputWriter creates a new buffered writer for a file
func newFileOutputWriter(file string) (*fileWriter, error) {
output, err := os.Create(file)
if err != nil {
return nil, err
}
return &fileWriter{file: output, writer: bufio.NewWriter(output)}, nil
}
// WriteString writes an output to the underlying file
func (w *fileWriter) Write(data []byte) error {
_, err := w.writer.Write(data)
if err != nil {
return err
}
_, err = w.writer.WriteRune('\n')
return err
}
// Close closes the underlying writer flushing everything to disk
func (w *fileWriter) Close() error {
w.writer.Flush()
//nolint:errcheck // we don't care whether sync failed or succeeded.
w.file.Sync()
return w.file.Close()
}