Fragment Description:



Writing into a zlib compress capable Writer and reader the compressed data from a zlib uncompress capable Reader.


zlibReaderWriter

Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:36

/* ... <== see fragment description ... */

package main

import (
    "bytes"
    "compress/zlib"
    "fmt"
    "io"
    "os"
    "time"
)

func main() {
    defer timeTrack(time.Now(), "task duration:")
    var in bytes.Buffer
    b := []byte(`{"Name":"Wednesday","Age":6,"Parents":["Father","Mother"],"test":{"test1":1,"test2":[1,2,3]}}`)
    w := zlib.NewWriter(&in) //w is zlib compress capable Writer into buffer 'in'
    w.Write(b)               //write/compress 'b' into 'in', through 'w'
    w.Close()
    fmt.Printf("the buffer 'in' contains\n%#v\n---\n", in)
    var out bytes.Buffer
    r, _ := zlib.NewReader(&in)
    // sign: copy(dst, src []Type) int
    io.Copy(&out, r)
    // os.Stdout is an io.Writer
    os.Stdout.Write(out.Bytes())
}
func timeTrack(start time.Time, name string) {
    elapsed := time.Since(start)
    fmt.Printf("\n--\nfunction %s took %v\n", name, elapsed)
}

/*  Expected Output:
the buffer 'in' contains
bytes.Buffer{buf:[]uint8{0x78, 0x9c, 0xaa, 0x56, 0xf2, 0x4b, 0xcc, 0x4d, 0x55, 0xb2, 0x52, 0xa, 0x4f, 0x4d, 0xc9, 0x4b, 0x2d, 0x4e, 0x49, 0xac, 0x54, 0xd2, 0x51, 0x72, 0x4c, 0x7, 0x8a, 0x98, 0xe9, 0x28, 0x5, 0x24, 0x16, 0xa5, 0xe6, 0x95, 0x14, 0x2b, 0x59, 0x45, 0x2b, 0xb9, 0x25, 0x96, 0x64, 0xa4, 0x16, 0x1, 0xe5, 0x7c, 0xf3, 0xc1, 0x8c, 0x58, 0x1d, 0xa5, 0x92, 0xd4, 0xe2, 0x12, 0x25, 0xab, 0x6a, 0x30, 0x6d, 0xa8, 0x64, 0x65, 0x8, 0x11, 0x31, 0x2, 0xaa, 0x36, 0xd4, 0x31, 0xd2, 0x31, 0x8e, 0xad, 0xad, 0x5, 0x4, 0x0, 0x0, 0xff, 0xff, 0x5a, 0xb9, 0x1c, 0xcd}, off:0, runeBytes:[4]uint8{0x0, 0x0, 0x0, 0x0}, bootstrap:[64]uint8{0x78, 0x9c, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, lastRead:0}
{"Name":"Wednesday","Age":6,"Parents":["Father","Mother"],"test":{"test1":1,"test2":[1,2,3]}}
function task duration: took 1ms
*/



Comments