-
Notifications
You must be signed in to change notification settings - Fork 63
/
writer.go
292 lines (251 loc) · 6.9 KB
/
writer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gozstd
/*
#define ZSTD_STATIC_LINKING_ONLY
#include "zstd.h"
#include <stdlib.h> // for malloc/free
*/
import "C"
import (
"fmt"
"io"
"runtime"
"unsafe"
)
var (
cstreamInBufSize = C.ZSTD_CStreamInSize()
cstreamOutBufSize = C.ZSTD_CStreamOutSize()
)
type cMemPtr *[1 << 30]byte
// Writer implements zstd writer.
type Writer struct {
w io.Writer
compressionLevel int
cs *C.ZSTD_CStream
cd *CDict
inBuf *C.ZSTD_inBuffer
outBuf *C.ZSTD_outBuffer
inBufGo cMemPtr
outBufGo cMemPtr
}
// NewWriter returns new zstd writer writing compressed data to w.
//
// The returned writer must be closed with Close call in order
// to finalize the compressed stream.
//
// Call Release when the Writer is no longer needed.
func NewWriter(w io.Writer) *Writer {
return newWriterDictLevel(w, nil, DefaultCompressionLevel)
}
// NewWriterLevel returns new zstd writer writing compressed data to w
// at the given compression level.
//
// The returned writer must be closed with Close call in order
// to finalize the compressed stream.
//
// Call Release when the Writer is no longer needed.
func NewWriterLevel(w io.Writer, compressionLevel int) *Writer {
return newWriterDictLevel(w, nil, compressionLevel)
}
// NewWriterDict returns new zstd writer writing compressed data to w
// using the given cd.
//
// The returned writer must be closed with Close call in order
// to finalize the compressed stream.
//
// Call Release when the Writer is no longer needed.
func NewWriterDict(w io.Writer, cd *CDict) *Writer {
return newWriterDictLevel(w, cd, 0)
}
func newWriterDictLevel(w io.Writer, cd *CDict, compressionLevel int) *Writer {
cs := C.ZSTD_createCStream()
initCStream(cs, cd, compressionLevel)
inBuf := (*C.ZSTD_inBuffer)(C.malloc(C.sizeof_ZSTD_inBuffer))
inBuf.src = C.malloc(cstreamInBufSize)
inBuf.size = 0
inBuf.pos = 0
outBuf := (*C.ZSTD_outBuffer)(C.malloc(C.sizeof_ZSTD_outBuffer))
outBuf.dst = C.malloc(cstreamOutBufSize)
outBuf.size = cstreamOutBufSize
outBuf.pos = 0
zw := &Writer{
w: w,
compressionLevel: compressionLevel,
cs: cs,
cd: cd,
inBuf: inBuf,
outBuf: outBuf,
}
zw.inBufGo = cMemPtr(zw.inBuf.src)
zw.outBufGo = cMemPtr(zw.outBuf.dst)
runtime.SetFinalizer(zw, freeCStream)
return zw
}
// Reset resets zw to write to w using the given dictionary cd and the given
// compressionLevel.
func (zw *Writer) Reset(w io.Writer, cd *CDict, compressionLevel int) {
zw.inBuf.size = 0
zw.inBuf.pos = 0
zw.outBuf.size = cstreamOutBufSize
zw.outBuf.pos = 0
zw.cd = cd
initCStream(zw.cs, zw.cd, compressionLevel)
zw.w = w
}
func initCStream(cs *C.ZSTD_CStream, cd *CDict, compressionLevel int) {
if cd != nil {
result := C.ZSTD_initCStream_usingCDict(cs, cd.p)
ensureNoError("ZSTD_initCStream_usingCDict", result)
} else {
result := C.ZSTD_initCStream(cs, C.int(compressionLevel))
ensureNoError("ZSTD_initCStream", result)
}
}
func freeCStream(v interface{}) {
v.(*Writer).Release()
}
// Release releases all the resources occupied by zw.
//
// zw cannot be used after the release.
func (zw *Writer) Release() {
if zw.cs == nil {
return
}
result := C.ZSTD_freeCStream(zw.cs)
ensureNoError("ZSTD_freeCStream", result)
zw.cs = nil
C.free(unsafe.Pointer(zw.inBuf.src))
C.free(unsafe.Pointer(zw.inBuf))
zw.inBuf = nil
C.free(unsafe.Pointer(zw.outBuf.dst))
C.free(unsafe.Pointer(zw.outBuf))
zw.outBuf = nil
zw.w = nil
zw.cd = nil
}
// ReadFrom reads all the data from r and writes it to zw.
//
// Returns the number of bytes read from r.
//
// ReadFrom may not flush the compressed data to the underlying writer
// due to performance reasons.
// Call Flush or Close when the compressed data must propagate
// to the underlying writer.
func (zw *Writer) ReadFrom(r io.Reader) (int64, error) {
nn := int64(0)
for {
// Fill the inBuf.
for zw.inBuf.size < cstreamInBufSize {
n, err := r.Read(zw.inBufGo[zw.inBuf.size:cstreamInBufSize])
if err != nil {
if err == io.EOF {
return nn, nil
}
return nn, err
}
zw.inBuf.size = C.size_t(n)
nn = int64(n)
}
// Flush the inBuf.
if err := zw.flushInBuf(); err != nil {
return nn, err
}
}
}
// Write writes p to zw.
//
// Write doesn't flush the compressed data to the underlying writer
// due to performance reasons.
// Call Flush or Close when the compressed data must propagate
// to the underlying writer.
func (zw *Writer) Write(p []byte) (int, error) {
pLen := len(p)
if pLen == 0 {
return 0, nil
}
for {
n := copy(zw.inBufGo[zw.inBuf.size:cstreamInBufSize], p)
zw.inBuf.size = C.size_t(n)
p = p[n:]
if len(p) == 0 {
// Fast path - just copy the data to input buffer.
return pLen, nil
}
if err := zw.flushInBuf(); err != nil {
return 0, err
}
}
}
func (zw *Writer) flushInBuf() error {
prevInBufPos := zw.inBuf.pos
result := C.ZSTD_compressStream(zw.cs, zw.outBuf, zw.inBuf)
ensureNoError("ZSTD_compressStream", result)
// Move the remaining data to the start of inBuf.
copy(zw.inBufGo[:cstreamInBufSize], zw.inBufGo[zw.inBuf.pos:zw.inBuf.size])
zw.inBuf.size -= zw.inBuf.pos
zw.inBuf.pos = 0
if zw.outBuf.size-zw.outBuf.pos > zw.outBuf.pos && prevInBufPos != zw.inBuf.pos {
// There is enough space in outBuf and the last compression
// succeeded, so don't flush outBuf yet.
return nil
}
// Flush outBuf, since there is low space in it or the last compression
// attempt was unsuccessful.
return zw.flushOutBuf()
}
func (zw *Writer) flushOutBuf() error {
if zw.outBuf.pos == 0 {
// Nothing to flush.
return nil
}
outBuf := zw.outBufGo[:zw.outBuf.pos]
n, err := zw.w.Write(outBuf)
zw.outBuf.pos = 0
if err != nil {
return fmt.Errorf("cannot flush internal buffer to the underlying writer: %s", err)
}
if n != len(outBuf) {
panic(fmt.Errorf("BUG: the underlying writer violated io.Writer contract and didn't return error after writing incomplete data; written %d bytes; want %d bytes",
n, len(outBuf)))
}
return nil
}
// Flush flushes the remaining data from zw to the underlying writer.
func (zw *Writer) Flush() error {
// Flush inBuf.
for zw.inBuf.size > 0 {
if err := zw.flushInBuf(); err != nil {
return err
}
}
// Flush the internal buffer to outBuf.
for {
result := C.ZSTD_flushStream(zw.cs, zw.outBuf)
ensureNoError("ZSTD_flushStream", result)
if err := zw.flushOutBuf(); err != nil {
return err
}
if result == 0 {
// No more data left in the internal buffer.
return nil
}
}
}
// Close finalizes the compressed stream and flushes all the compressed data
// to the underlying writer.
//
// It doesn't close the underlying writer passed to New* functions.
func (zw *Writer) Close() error {
if err := zw.Flush(); err != nil {
return err
}
for {
result := C.ZSTD_endStream(zw.cs, zw.outBuf)
ensureNoError("ZSTD_endStream", result)
if err := zw.flushOutBuf(); err != nil {
return err
}
if result == 0 {
return nil
}
}
}