1
2
3
4
5 package flate_test
6
7 import (
8 "bytes"
9 "compress/flate"
10 "fmt"
11 "io"
12 "log"
13 "os"
14 "strings"
15 "sync"
16 )
17
18
19
20
21 func Example_reset() {
22 proverbs := []string{
23 "Don't communicate by sharing memory, share memory by communicating.\n",
24 "Concurrency is not parallelism.\n",
25 "The bigger the interface, the weaker the abstraction.\n",
26 "Documentation is for users.\n",
27 }
28
29 var r strings.Reader
30 var b bytes.Buffer
31 buf := make([]byte, 32<<10)
32
33 zw, err := flate.NewWriter(nil, flate.DefaultCompression)
34 if err != nil {
35 log.Fatal(err)
36 }
37 zr := flate.NewReader(nil)
38
39 for _, s := range proverbs {
40 r.Reset(s)
41 b.Reset()
42
43
44 zw.Reset(&b)
45 if _, err := io.CopyBuffer(zw, &r, buf); err != nil {
46 log.Fatal(err)
47 }
48 if err := zw.Close(); err != nil {
49 log.Fatal(err)
50 }
51
52
53 if err := zr.(flate.Resetter).Reset(&b, nil); err != nil {
54 log.Fatal(err)
55 }
56 if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil {
57 log.Fatal(err)
58 }
59 if err := zr.Close(); err != nil {
60 log.Fatal(err)
61 }
62 }
63
64
65
66
67
68
69 }
70
71
72
73
74 func Example_dictionary() {
75
76
77
78
79 const dict = `<?xml version="1.0"?>` + `<book>` + `<data>` + `<meta name="` + `" content="`
80
81
82
83 const data = `<?xml version="1.0"?>
84 <book>
85 <meta name="title" content="The Go Programming Language"/>
86 <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
87 <meta name="published" content="2015-10-26"/>
88 <meta name="isbn" content="978-0134190440"/>
89 <data>...</data>
90 </book>
91 `
92
93 var b bytes.Buffer
94
95
96 zw, err := flate.NewWriterDict(&b, flate.DefaultCompression, []byte(dict))
97 if err != nil {
98 log.Fatal(err)
99 }
100 if _, err := io.Copy(zw, strings.NewReader(data)); err != nil {
101 log.Fatal(err)
102 }
103 if err := zw.Close(); err != nil {
104 log.Fatal(err)
105 }
106
107
108
109 fmt.Println("Decompressed output using the dictionary:")
110 zr := flate.NewReaderDict(bytes.NewReader(b.Bytes()), []byte(dict))
111 if _, err := io.Copy(os.Stdout, zr); err != nil {
112 log.Fatal(err)
113 }
114 if err := zr.Close(); err != nil {
115 log.Fatal(err)
116 }
117
118 fmt.Println()
119
120
121
122 fmt.Println("Substrings matched by the dictionary are marked with #:")
123 hashDict := []byte(dict)
124 for i := range hashDict {
125 hashDict[i] = '#'
126 }
127 zr = flate.NewReaderDict(&b, hashDict)
128 if _, err := io.Copy(os.Stdout, zr); err != nil {
129 log.Fatal(err)
130 }
131 if err := zr.Close(); err != nil {
132 log.Fatal(err)
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 }
156
157
158 func Example_synchronization() {
159 var wg sync.WaitGroup
160 defer wg.Wait()
161
162
163
164
165 rp, wp := io.Pipe()
166
167
168 wg.Add(1)
169 go func() {
170 defer wg.Done()
171
172 zw, err := flate.NewWriter(wp, flate.BestSpeed)
173 if err != nil {
174 log.Fatal(err)
175 }
176
177 b := make([]byte, 256)
178 for _, m := range strings.Fields("A long time ago in a galaxy far, far away...") {
179
180
181 b[0] = uint8(copy(b[1:], m))
182
183 if _, err := zw.Write(b[:1+len(m)]); err != nil {
184 log.Fatal(err)
185 }
186
187
188 if err := zw.Flush(); err != nil {
189 log.Fatal(err)
190 }
191 }
192
193 if err := zw.Close(); err != nil {
194 log.Fatal(err)
195 }
196 }()
197
198
199 wg.Add(1)
200 go func() {
201 defer wg.Done()
202
203 zr := flate.NewReader(rp)
204
205 b := make([]byte, 256)
206 for {
207
208
209
210 if _, err := io.ReadFull(zr, b[:1]); err != nil {
211 if err == io.EOF {
212 break
213 }
214 log.Fatal(err)
215 }
216
217
218 n := int(b[0])
219 if _, err := io.ReadFull(zr, b[:n]); err != nil {
220 log.Fatal(err)
221 }
222
223 fmt.Printf("Received %d bytes: %s\n", n, b[:n])
224 }
225 fmt.Println()
226
227 if err := zr.Close(); err != nil {
228 log.Fatal(err)
229 }
230 }()
231
232
233
234
235
236
237
238
239
240
241
242
243 }
244
View as plain text