// Copyright 2016 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // +build ignore package flate_test import ( "bytes" "compress/flate" "fmt" "io" "log" "os" "strings" "sync" ) // In performance critical applications, Reset can be used to discard the // current compressor or decompressor state and reinitialize them quickly // by taking advantage of previously allocated memory. func Example_reset() { proverbs := []string{ "Don't communicate by sharing memory, share memory by communicating.\n", "Concurrency is not parallelism.\n", "The bigger the interface, the weaker the abstraction.\n", "Documentation is for users.\n", } var r strings.Reader var b bytes.Buffer buf := make([]byte, 32<<10) zw, err := flate.NewWriter(nil, flate.DefaultCompression) if err != nil { log.Fatal(err) } zr := flate.NewReader(nil) for _, s := range proverbs { r.Reset(s) b.Reset() // Reset the compressor and encode from some input stream. zw.Reset(&b) if _, err := io.CopyBuffer(zw, &r, buf); err != nil { log.Fatal(err) } if err := zw.Close(); err != nil { log.Fatal(err) } // Reset the decompressor and decode to some output stream. if err := zr.(flate.Resetter).Reset(&b, nil); err != nil { log.Fatal(err) } if _, err := io.CopyBuffer(os.Stdout, zr, buf); err != nil { log.Fatal(err) } if err := zr.Close(); err != nil { log.Fatal(err) } } // Output: // Don't communicate by sharing memory, share memory by communicating. // Concurrency is not parallelism. // The bigger the interface, the weaker the abstraction. // Documentation is for users. } // A preset dictionary can be used to improve the compression ratio. // The downside to using a dictionary is that the compressor and decompressor // must agree in advance what dictionary to use. func Example_dictionary() { // The dictionary is a string of bytes. When compressing some input data, // the compressor will attempt to substitute substrings with matches found // in the dictionary. As such, the dictionary should only contain substrings // that are expected to be found in the actual data stream. const dict = `` + `` + `` + ` ... ` var b bytes.Buffer // Compress the data using the specially crafted dictionary. zw, err := flate.NewWriterDict(&b, flate.DefaultCompression, []byte(dict)) if err != nil { log.Fatal(err) } if _, err := io.Copy(zw, strings.NewReader(data)); err != nil { log.Fatal(err) } if err := zw.Close(); err != nil { log.Fatal(err) } // The decompressor must use the same dictionary as the compressor. // Otherwise, the input may appear as corrupted. fmt.Println("Decompressed output using the dictionary:") zr := flate.NewReaderDict(bytes.NewReader(b.Bytes()), []byte(dict)) if _, err := io.Copy(os.Stdout, zr); err != nil { log.Fatal(err) } if err := zr.Close(); err != nil { log.Fatal(err) } fmt.Println() // Substitute all of the bytes in the dictionary with a '#' to visually // demonstrate the approximate effectiveness of using a preset dictionary. fmt.Println("Substrings matched by the dictionary are marked with #:") hashDict := []byte(dict) for i := range hashDict { hashDict[i] = '#' } zr = flate.NewReaderDict(&b, hashDict) if _, err := io.Copy(os.Stdout, zr); err != nil { log.Fatal(err) } if err := zr.Close(); err != nil { log.Fatal(err) } // Output: // Decompressed output using the dictionary: // // // // // // // ... // // // Substrings matched by the dictionary are marked with #: // ##################### // ###### // ############title###########The Go Programming Language"/# // ############authors###########Alan Donovan and Brian Kernighan"/# // ############published###########2015-10-26"/# // ############isbn###########978-0134190440"/# // ######...