Debian Codegraph @

zlib/example_test.go

raw
1 // Copyright 2012 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 zlib_test
6
7 import (
8 "bytes"
9 "compress/zlib"
10 "io"
11 "os"
12 )
13
14 func ExampleNewWriter() {
15 var b bytes.Buffer
16
17 w := zlib.NewWriter(&b)
18 w.Write([]byte("hello, world\n"))
19 w.Close()
20
21 // The stdlib flate encoder's exact byte output for a given input is not
22 // part of the Go compatibility promise (only decodability is), and has
23 // changed between Go versions (e.g. its tiny-input store-vs-compress
24 // heuristic). So round-trip through a Reader instead of asserting the
25 // compressed bytes verbatim.
26 r, err := zlib.NewReader(&b)
27 if err != nil {
28 panic(err)
29 }
30 if _, err := io.Copy(os.Stdout, r); err != nil {
31 panic(err)
32 }
33 if err := r.Close(); err != nil {
34 panic(err)
35 }
36 // Output: hello, world
37 }
38
39 func ExampleNewReader() {
40 buff := []byte{120, 156, 202, 72, 205, 201, 201, 215, 81, 40, 207,
41 47, 202, 73, 225, 2, 4, 0, 0, 255, 255, 33, 231, 4, 147}
42 b := bytes.NewReader(buff)
43
44 r, err := zlib.NewReader(b)
45 if err != nil {
46 panic(err)
47 }
48 io.Copy(os.Stdout, r)
49 // Output: hello, world
50 r.Close()
51 }