github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/compress/flate/decompressor.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package flate 4 5 import ( 6 "bytes" 7 "compress/flate" 8 "io" 9 10 compress ".." 11 "../../protocol" 12 ) 13 14 type Decompressor struct { 15 source protocol.Codec 16 decompressedData []byte 17 } 18 19 func (d *Decompressor) init() { 20 var comBuf = bytes.NewBuffer(d.source.Marshal()) 21 var def = flate.NewReader(comBuf) 22 23 // TODO::: which solution? 24 // d.decompressedData, _ = io.ReadAll(def) 25 var decomBuf bytes.Buffer 26 decomBuf.Grow(d.source.Len()) 27 decomBuf.ReadFrom(def) 28 d.decompressedData = decomBuf.Bytes() 29 } 30 31 /* 32 ********** protocol.Codec interface ********** 33 */ 34 35 func (d *Decompressor) MediaType() protocol.MediaType { return d.source.MediaType() } 36 func (d *Decompressor) CompressType() protocol.CompressType { return nil } 37 38 func (d *Decompressor) Decode(reader protocol.Reader) (err protocol.Error) { 39 err = compress.ErrSourceNotChangeable 40 return 41 } 42 func (d *Decompressor) Encode(writer protocol.Writer) (err protocol.Error) { 43 var _, goErr = d.WriteTo(writer) 44 if goErr != nil { 45 // err = 46 } 47 return 48 } 49 func (d *Decompressor) Marshal() (data []byte) { 50 if d.decompressedData == nil { 51 d.init() 52 } 53 return d.decompressedData 54 } 55 func (d *Decompressor) MarshalTo(data []byte) []byte { 56 if d.decompressedData == nil { 57 d.init() 58 } 59 return append(data, d.decompressedData...) 60 } 61 func (d *Decompressor) Unmarshal(data []byte) (err protocol.Error) { 62 err = compress.ErrSourceNotChangeable 63 return 64 } 65 func (d *Decompressor) UnmarshalFrom(data []byte) (remaining []byte, err protocol.Error) { 66 err = compress.ErrSourceNotChangeable 67 return 68 } 69 70 // Len return length of decompressed data 71 func (d *Decompressor) Len() (ln int) { 72 if d.decompressedData == nil { 73 d.init() 74 } 75 return len(d.decompressedData) 76 } 77 78 /* 79 ********** io package interfaces ********** 80 */ 81 82 func (d *Decompressor) ReadFrom(reader io.Reader) (n int64, err error) { 83 err = compress.ErrSourceNotChangeable 84 return 85 } 86 func (d *Decompressor) WriteTo(w io.Writer) (totalWrite int64, err error) { 87 var buf = bytes.NewBuffer(d.source.Marshal()) 88 var def = flate.NewReader(buf) 89 return io.Copy(w, def) 90 }