-
Notifications
You must be signed in to change notification settings - Fork 1
/
writer.go
213 lines (182 loc) · 4.52 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
package bzip2
import (
"compress/flate"
"fmt"
"io"
"github.com/larzconwell/bzip2/internal/bits"
)
const (
// fileMagic is the bzip2 magic number, BZ.
fileMagic = 0x425a
// finalMagic signifies the end of the block data.
finalMagic = 0x177245385090
// baseBlockSize is the base for block sizes.
baseBlockSize = 100000
)
// These constants are copied from the flate package, so that
// code does not also have to import "compress/flate".
const (
BestSpeed = flate.BestSpeed
BestCompression = flate.BestCompression
)
// Writer is an io.WriteCloser. Writes to a Writer are
// compressed and written to an underlying io.Writer.
type Writer struct {
bw *bits.Writer
block *block
crc uint32
wroteHeader bool
closed bool
err error
}
// NewWriter returns a new Writer. Writes to the returned
// writer are compressed and written to w.
//
// It is the caller's responsibility to call Close on the
// Writer when done. Writes may be buffered and not
// flushed until Close.
func NewWriter(w io.Writer) *Writer {
return &Writer{
bw: bits.NewWriter(w),
block: newBlock(6 * baseBlockSize),
}
}
// NewWriterLevel is like NewWriter but specifies the
// compression level.
//
// The levels range from 1 (BestSpeed) to 9 (BestCompression);
// higher levels typically run slower but compress more.
//
// If level is in the range [1, 9] then the error returned will
// be nil. Otherwise the error returned will be non-nil.
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
if level < BestSpeed || level > BestCompression {
return nil, fmt.Errorf("bzip2: invalid compression level: %d", level)
}
return &Writer{
bw: bits.NewWriter(w),
block: newBlock(level * baseBlockSize),
}, nil
}
// Write writes a compressed form of p to the underlying
// io.Writer. The compressed bytes are not necessarily
// flushed until the Writer is closed.
func (w *Writer) Write(p []byte) (int, error) {
if w.err != nil {
return 0, w.err
}
var n int
// Handle writing the file header.
if !w.wroteHeader {
w.err = w.writeHeader()
if w.err != nil {
return 0, w.err
}
w.wroteHeader = true
}
n, w.err = w.write(p)
return n, w.err
}
// writeHeader writes the file header.
func (w *Writer) writeHeader() error {
w.bw.WriteBits(16, fileMagic)
w.bw.WriteBits(8, 'h')
w.bw.WriteBits(8, uint64('0'+w.block.size/baseBlockSize))
return w.bw.Err()
}
// write handles the writing of block data and writing
// completed blocks to the underlying io.Writer.
func (w *Writer) write(p []byte) (int, error) {
n, err := w.block.Write(p)
if err != errBlockSizeReached {
return n, err
}
// Write the complete block, left over
// bytes being written to a new block.
err = w.writeBlock()
if err != nil {
return n, err
}
if n != len(p) {
var nn int
nn, err = w.write(p[n:])
n += nn
}
return n, err
}
// writeBlock writes the current block to the
// underlying io.Writer and updates the files crc.
func (w *Writer) writeBlock() error {
err := w.block.WriteBlock(w.bw)
if err != nil {
return err
}
w.crc = ((w.crc << 1) | (w.crc >> 31)) ^ w.block.crc
w.block = newBlock(w.block.size)
return nil
}
// Flush flushes any pending compressed data
// to the underlying writer.
func (w *Writer) Flush() error {
if w.err != nil {
return w.err
}
if w.closed || w.block.Len() == 0 {
return nil
}
// Handle writing the file header.
if !w.wroteHeader {
w.err = w.writeHeader()
if w.err != nil {
return w.err
}
w.wroteHeader = true
}
w.err = w.writeBlock()
return w.err
}
// Reset discards the state of Writer and makes it equivalent
// to the result of NewWriter or NewWriterLevel, but writing
// to dst instead.
func (w *Writer) Reset(dst io.Writer) {
w.bw = bits.NewWriter(dst)
w.block = newBlock(w.block.size)
w.crc = 0
w.wroteHeader = false
w.closed = false
w.err = nil
}
// Close closes the Writer, flushing any unwritten data to the
// underlying io.Writer, but does not close the underlying io.Writer.
func (w *Writer) Close() error {
if w.err != nil {
return w.err
}
if w.closed {
return nil
}
w.closed = true
// Handle writing the file header.
if !w.wroteHeader {
w.err = w.writeHeader()
if w.err != nil {
return w.err
}
w.wroteHeader = true
}
// Flush the current block.
if w.block.Len() != 0 {
w.err = w.writeBlock()
if w.err != nil {
return w.err
}
}
w.bw.WriteBits(48, finalMagic)
w.bw.WriteBits(32, uint64(w.crc))
bufferedBits := w.bw.Buffered()
if bufferedBits != 0 {
w.bw.WriteBits(8-bufferedBits, 0)
}
w.err = w.bw.Err()
return w.err
}