aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/blockcodecs/encoder.go
blob: b7bb154f79483c4ada2525a61c741c5b084dffe9 (plain) (blame)
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
package blockcodecs

import (
	"encoding/binary"
	"errors"
	"fmt"
	"io"
)

type encoder struct {
	w     io.Writer
	codec Codec

	closed bool
	header [10]byte

	buf []byte
	pos int

	scratch []byte
}

const (
	// defaultBufferSize is 32KB, same as size of buffer used in io.Copy.
	defaultBufferSize = 32 << 10
)

var (
	_ io.WriteCloser = (*encoder)(nil)
)

func (e *encoder) Write(p []byte) (int, error) {
	if e.closed {
		return 0, errors.New("blockcodecs: encoder is closed")
	}

	n := len(p)

	// Complete current block
	if e.pos != 0 {
		m := copy(e.buf[e.pos:], p)
		p = p[m:]
		e.pos += m

		if e.pos == len(e.buf) {
			e.pos = 0

			if err := e.doFlush(e.buf); err != nil {
				return 0, err
			}
		}
	}

	// Copy huge input directly to output
	for len(p) >= len(e.buf) {
		if e.pos != 0 {
			panic("broken invariant")
		}

		var chunk []byte
		if len(p) > len(e.buf) {
			chunk = p[:len(e.buf)]
			p = p[len(e.buf):]
		} else {
			chunk = p
			p = nil
		}

		if err := e.doFlush(chunk); err != nil {
			return 0, err
		}
	}

	// Store suffix in buffer
	m := copy(e.buf, p)
	e.pos += m
	if m != len(p) {
		panic("broken invariant")
	}

	return n, nil
}

func (e *encoder) Close() error {
	if e.closed {
		return nil
	}

	if err := e.Flush(); err != nil {
		return err
	}

	e.closed = true

	return e.doFlush(nil)
}

func (e *encoder) doFlush(block []byte) error {
	var err error
	e.scratch, err = e.codec.Encode(e.scratch, block)
	if err != nil {
		return fmt.Errorf("blockcodecs: block compression error: %w", err)
	}

	binary.LittleEndian.PutUint16(e.header[:2], uint16(e.codec.ID()))
	binary.LittleEndian.PutUint64(e.header[2:], uint64(len(e.scratch)))

	if _, err := e.w.Write(e.header[:]); err != nil {
		return err
	}

	if _, err := e.w.Write(e.scratch); err != nil {
		return err
	}

	return nil
}

func (e *encoder) Flush() error {
	if e.closed {
		return errors.New("blockcodecs: flushing closed encoder")
	}

	if e.pos == 0 {
		return nil
	}

	err := e.doFlush(e.buf[:e.pos])
	e.pos = 0
	return err
}

func NewEncoder(w io.Writer, codec Codec) io.WriteCloser {
	return NewEncoderBuffer(w, codec, defaultBufferSize)
}

func NewEncoderBuffer(w io.Writer, codec Codec, bufferSize int) io.WriteCloser {
	return &encoder{w: w, codec: codec, buf: make([]byte, bufferSize)}
}