forked from go-sql-driver/mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzlibcompress_sys.go
92 lines (77 loc) · 2.09 KB
/
zlibcompress_sys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2020 huiyi<yi.webmaster@hotmail.com>. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at http://mozilla.org/MPL/2.0/.
// $Id: zlibcompress_sys.go
package mysql
import (
"bufio"
"bytes"
"compress/zlib"
"io"
)
// Compress & decompress mysql packet using golang zlib
type sysZLibCompressor struct {
zlibWriter *zlib.Writer
}
type sysZLibDecompressor struct {
bytesReader bytes.Reader
bufferedReader bufio.Reader // Make zlib.flate.makeReader happy, but 1 more time memory copy!!
zlibReader io.ReadCloser
}
func (zc *sysZLibCompressor) compress(input []byte, output *bytes.Buffer) (int, error) {
var err error
var lenSave int
// Initialize zlib writer
if zc.zlibWriter == nil {
zc.zlibWriter, err = zlib.NewWriterLevel(output, defaultPacketCompressLevel)
} else {
// Reuse zlib writer
zc.zlibWriter.Reset(output)
}
if err != nil {
return 0, err
}
// Save old length
lenSave = output.Len()
// Compress data
for len(input) > 0 {
var n int
n, err = zc.zlibWriter.Write(input)
if err != nil {
return 0, err
}
input = input[n:]
}
err = zc.zlibWriter.Close()
if err != nil {
return 0, err
}
// Returns compressed length
return output.Len() - lenSave, nil
}
// In mysql compress protocol, we always know the decompressed length for input
func (zd *sysZLibDecompressor) decompress(input []byte, output []byte) error {
var err error
// Reuse bytes.Buffer
zd.bytesReader.Reset(input)
// Initialize zlib reader
if zd.zlibReader == nil {
zd.bufferedReader = *bufio.NewReaderSize(&zd.bytesReader, 512)
zd.zlibReader, err = zlib.NewReader(&zd.bufferedReader)
} else {
// Reuse zlib reader
zd.bufferedReader.Reset(&zd.bytesReader)
err = zd.zlibReader.(zlib.Resetter).Reset(&zd.bufferedReader, nil)
}
if err != nil {
return err
}
// n of io.ReadFull = len(output) when err = null, so the return value can be ignored
_, err = io.ReadFull(zd.zlibReader, output)
if err != nil {
return err
}
return nil
}