-
Notifications
You must be signed in to change notification settings - Fork 10
/
open.go
135 lines (123 loc) · 3.26 KB
/
open.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
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
// Package aio provides buffered file I/O.
package aio
import (
"bufio"
"compress/gzip"
"io"
"os"
"path/filepath"
)
const (
// If true, .gz files are automatically compressed/decompressed.
gzipSupport = true
)
// OpenRaw opens a file for reading, with a buffer.
func OpenRaw(file string) (*Reader, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
return &Reader{*bufio.NewReader(f), f}, nil
}
// CreateRaw opens a file for writing, with a buffer.
// Erases any previously existing content.
func CreateRaw(file string) (*Writer, error) {
f, err := os.Create(file)
if err != nil {
return nil, err
}
return &Writer{*bufio.NewWriter(f), f}, nil
}
// AppendRaw opens a file for writing, with a buffer.
// Appends to previously existing content if any.
func AppendRaw(file string) (*Writer, error) {
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return nil, err
}
return &Writer{*bufio.NewWriter(f), f}, nil
}
var (
rsuffixes = map[string]func(io.Reader) (io.Reader, error){}
wsuffixes = map[string]func(io.WriteCloser) (io.WriteCloser, error){}
)
// Open opens a file for reading, with a buffer.
// Decompresses the data according to the file's suffix.
func Open(file string) (*Reader, error) {
f, err := OpenRaw(file)
if err != nil {
return nil, err
}
fn := rsuffixes[filepath.Ext(file)]
if fn == nil {
return f, nil
}
ff, err := fn(f)
if err != nil {
return nil, err
}
return &Reader{*bufio.NewReader(ff), f}, nil
}
// Create opens a file for writing, with a buffer.
// Erases any previously existing content.
// Compresses the data according to the file's suffix.
func Create(file string) (*Writer, error) {
f, err := CreateRaw(file)
if err != nil {
return nil, err
}
fn := wsuffixes[filepath.Ext(file)]
if fn == nil {
return f, nil
}
ff, err := fn(f)
if err != nil {
return nil, err
}
wrapper := &writerWrapper{ff, f}
return &Writer{*bufio.NewWriter(ff), wrapper}, nil
}
// Append opens a file for writing, with a buffer.
// Appends to previously existing content if any.
// Compresses the data according to the file's suffix.
func Append(file string) (*Writer, error) {
f, err := AppendRaw(file)
if err != nil {
return nil, err
}
fn := wsuffixes[filepath.Ext(file)]
if fn == nil {
return f, nil
}
ff, err := fn(f)
if err != nil {
return nil, err
}
wrapper := &writerWrapper{ff, f}
return &Writer{*bufio.NewWriter(ff), wrapper}, nil
}
// AddReadSuffix adds a supported suffix for automatic decompression.
// suffix should include the dot. f should take a raw reader and return a reader
// that decompresses the data.
func AddReadSuffix(suffix string, f func(io.Reader) (io.Reader, error)) {
rsuffixes[suffix] = f
}
// AddWriteSuffix adds a supported suffix for automatic compression.
// suffix should include the dot. f should take a raw writer and return a writer
// that compresses the data.
func AddWriteSuffix(suffix string, f func(io.WriteCloser) (
io.WriteCloser, error)) {
wsuffixes[suffix] = f
}
func init() {
if gzipSupport {
AddReadSuffix(".gz", func(r io.Reader) (io.Reader, error) {
z, err := gzip.NewReader(r)
return z, err
})
AddWriteSuffix(".gz", func(w io.WriteCloser) (io.WriteCloser, error) {
z, err := gzip.NewWriterLevel(w, 1)
return z, err
})
}
}