Debian Codegraph @

flate/example_test.go

raw
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package flate_test
6
7 import (
8 "bytes"
9 "fmt"
10 "io"
11 "log"
12 "os"
13 "strings"
14 "sync"
15
16 "github.com/klauspost/compress/flate"
17 )
18
19 // In performance critical applications, Reset can be used to discard the
20 // current compressor or decompressor state and reinitialize them quickly
21 // by taking advantage of previously allocated memory.
22 func Example_reset() {
23 proverbs := []string{
24 "Don't communicate by sharing memory, share memory by communicating.\n",
25 "Concurrency is not parallelism.\n",
26 "The bigger the interface, the weaker the abstraction.\n",
27 "Documentation is for users.\n",
28 }
29
30 var r strings.Reader
31 var b bytes.Buffer
32 buf := make([]byte, 32<<10)
33
34 zw, err := flate.NewWriter(nil, flate.DefaultCompression)
35 if err != nil {
36 log.Fatal(err)
37 }
38 zr := flate.NewReader(nil)
39
40 for _, s := range proverbs {
41 r.Reset(s)
42 b.Reset()
43
44 // Reset the compressor and encode from some input stream.
45 zw.Reset(&b)
46 if _, err := io.CopyBuffer(zw, &r, buf); err != nil {
47 log.Fatal(err)
48 }
49 if err := zw.Close(); err != nil {
50 log.Fatal(err)
51 }
52
53 // Reset the decompressor and decode to some output stream.
54 if err := zr.(flate.Resetter).Reset(&b, nil); err != nil {
55 log.Fatal(err)
56 }
57 if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil {
58 log.Fatal(err)
59 }
60 if err := zr.Close(); err != nil {
61 log.Fatal(err)
62 }
63 }
64
65 // Output:
66 // Don't communicate by sharing memory, share memory by communicating.
67 // Concurrency is not parallelism.
68 // The bigger the interface, the weaker the abstraction.
69 // Documentation is for users.
70 }
71
72 // A preset dictionary can be used to improve the compression ratio.
73 // The downside to using a dictionary is that the compressor and decompressor
74 // must agree in advance what dictionary to use.
75 func Example_dictionary() {
76 // The dictionary is a string of bytes. When compressing some input data,
77 // the compressor will attempt to substitute substrings with matches found
78 // in the dictionary. As such, the dictionary should only contain substrings
79 // that are expected to be found in the actual data stream.
80 const dict = `<?xml version="1.0"?>` + `<book>` + `<data>` + `<meta name="` + `" content="`
81
82 // The data to compress should (but is not required to) contain frequent
83 // substrings that match those in the dictionary.
84 const data = `<?xml version="1.0"?>
85 <book>
86 <meta name="title" content="The Go Programming Language"/>
87 <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
88 <meta name="published" content="2015-10-26"/>
89 <meta name="isbn" content="978-0134190440"/>
90 <data>...</data>
91 </book>
92 `
93
94 var b bytes.Buffer
95
96 // Compress the data using the specially crafted dictionary.
97 zw, err := flate.NewWriterDict(&b, flate.BestCompression, []byte(dict))
98 if err != nil {
99 log.Fatal(err)
100 }
101 if _, err := io.Copy(zw, strings.NewReader(data)); err != nil {
102 log.Fatal(err)
103 }
104 if err := zw.Close(); err != nil {
105 log.Fatal(err)
106 }
107
108 // The decompressor must use the same dictionary as the compressor.
109 // Otherwise, the input may appear as corrupted.
110 fmt.Println("Decompressed output using the dictionary:")
111 zr := flate.NewReaderDict(bytes.NewReader(b.Bytes()), []byte(dict))
112 if _, err := io.Copy(os.Stdout, zr); err != nil {
113 log.Fatal(err)
114 }
115 if err := zr.Close(); err != nil {
116 log.Fatal(err)
117 }
118
119 fmt.Println()
120
121 // Substitute all of the bytes in the dictionary with a '#' to visually
122 // demonstrate the approximate effectiveness of using a preset dictionary.
123 fmt.Println("Substrings matched by the dictionary are marked with #:")
124 hashDict := []byte(dict)
125 for i := range hashDict {
126 hashDict[i] = '#'
127 }
128 zr = flate.NewReaderDict(&b, hashDict)
129 if _, err := io.Copy(os.Stdout, zr); err != nil {
130 log.Fatal(err)
131 }
132 if err := zr.Close(); err != nil {
133 log.Fatal(err)
134 }
135
136 // Output:
137 // Decompressed output using the dictionary:
138 // <?xml version="1.0"?>
139 // <book>
140 // <meta name="title" content="The Go Programming Language"/>
141 // <meta name="authors" content="Alan Donovan and Brian Kernighan"/>
142 // <meta name="published" content="2015-10-26"/>
143 // <meta name="isbn" content="978-0134190440"/>
144 // <data>...</data>
145 // </book>
146 //
147 // Substrings matched by the dictionary are marked with #:
148 // #####################
149 // ######
150 // ############title###########The Go Programming Language"/#
151 // ############authors###########Alan Donovan and Brian Kernighan"/#
152 // ############published###########2015-10-26"/#
153 // ############isbn###########978-0134190440"/#
154 // ######...</#####
155 // </#####
156 }
157
158 // DEFLATE is suitable for transmitting compressed data across the network.
159 func Example_synchronization() {
160 var wg sync.WaitGroup
161 defer wg.Wait()
162
163 // Use io.Pipe to simulate a network connection.
164 // A real network application should take care to properly close the
165 // underlying connection.
166 rp, wp := io.Pipe()
167
168 // Start a goroutine to act as the transmitter.
169 wg.Go(func() {
170 defer wp.Close()
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.FieldsSeq("A long time ago in a galaxy far, far away...") {
179 // We use a simple framing format where the first byte is the
180 // message length, followed the message itself.
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 // Flush ensures that the receiver can read all data sent so far.
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 // Start a goroutine to act as the receiver.
199 wg.Go(func() {
200
201 zr := flate.NewReader(rp)
202
203 b := make([]byte, 256)
204 for {
205 // Read the message length.
206 // This is guaranteed to return for every corresponding
207 // Flush and Close on the transmitter side.
208 if _, err := io.ReadFull(zr, b[:1]); err != nil {
209 if err == io.EOF {
210 break // The transmitter closed the stream
211 }
212 log.Fatal(err)
213 }
214
215 // Read the message content.
216 n := int(b[0])
217 if _, err := io.ReadFull(zr, b[:n]); err != nil {
218 log.Fatal(err)
219 }
220
221 fmt.Printf("Received %d bytes: %s\n", n, b[:n])
222 }
223 fmt.Println()
224
225 if err := zr.Close(); err != nil {
226 log.Fatal(err)
227 }
228 })
229
230 // Output:
231 // Received 1 bytes: A
232 // Received 4 bytes: long
233 // Received 4 bytes: time
234 // Received 3 bytes: ago
235 // Received 2 bytes: in
236 // Received 1 bytes: a
237 // Received 6 bytes: galaxy
238 // Received 4 bytes: far,
239 // Received 3 bytes: far
240 // Received 7 bytes: away...
241 }