1
2
3
4
5 package gob
6
7 import (
8 "errors"
9 "io"
10 "reflect"
11 "sync"
12 )
13
14
15
16
17 type Encoder struct {
18 mutex sync.Mutex
19 w []io.Writer
20 sent map[reflect.Type]typeId
21 countState *encoderState
22 freeList *encoderState
23 byteBuf encBuffer
24 err error
25 }
26
27
28
29
30 const maxLength = 9
31 var spaceForLength = make([]byte, maxLength)
32
33
34 func NewEncoder(w io.Writer) *Encoder {
35 enc := new(Encoder)
36 enc.w = []io.Writer{w}
37 enc.sent = make(map[reflect.Type]typeId)
38 enc.countState = enc.newEncoderState(new(encBuffer))
39 return enc
40 }
41
42
43 func (enc *Encoder) writer() io.Writer {
44 return enc.w[len(enc.w)-1]
45 }
46
47
48 func (enc *Encoder) pushWriter(w io.Writer) {
49 enc.w = append(enc.w, w)
50 }
51
52
53 func (enc *Encoder) popWriter() {
54 enc.w = enc.w[0 : len(enc.w)-1]
55 }
56
57 func (enc *Encoder) setError(err error) {
58 if enc.err == nil {
59 enc.err = err
60 }
61 }
62
63
64 func (enc *Encoder) writeMessage(w io.Writer, b *encBuffer) {
65
66
67
68 message := b.Bytes()
69 messageLen := len(message) - maxLength
70
71 if messageLen >= tooBig {
72 enc.setError(errors.New("gob: encoder: message too big"))
73 return
74 }
75
76 enc.countState.b.Reset()
77 enc.countState.encodeUint(uint64(messageLen))
78
79 offset := maxLength - enc.countState.b.Len()
80 copy(message[offset:], enc.countState.b.Bytes())
81
82 _, err := w.Write(message[offset:])
83
84 b.Reset()
85 b.Write(spaceForLength)
86 if err != nil {
87 enc.setError(err)
88 }
89 }
90
91
92
93 func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTypeInfo, actual reflect.Type) (sent bool) {
94 if _, alreadySent := enc.sent[actual]; alreadySent {
95 return false
96 }
97 info, err := getTypeInfo(ut)
98 if err != nil {
99 enc.setError(err)
100 return
101 }
102
103
104 state.encodeInt(-int64(info.id))
105
106 enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
107 enc.writeMessage(w, state.b)
108 if enc.err != nil {
109 return
110 }
111
112
113 enc.sent[ut.base] = info.id
114 if ut.user != ut.base {
115 enc.sent[ut.user] = info.id
116 }
117
118 switch st := actual; st.Kind() {
119 case reflect.Struct:
120 for i := 0; i < st.NumField(); i++ {
121 if isExported(st.Field(i).Name) {
122 enc.sendType(w, state, st.Field(i).Type)
123 }
124 }
125 case reflect.Array, reflect.Slice:
126 enc.sendType(w, state, st.Elem())
127 case reflect.Map:
128 enc.sendType(w, state, st.Key())
129 enc.sendType(w, state, st.Elem())
130 }
131 return true
132 }
133
134
135 func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
136 ut := userType(origt)
137 if ut.externalEnc != 0 {
138
139
140 return enc.sendActualType(w, state, ut, ut.base)
141 }
142
143
144 switch rt := ut.base; rt.Kind() {
145 default:
146
147 return
148 case reflect.Slice:
149
150 if rt.Elem().Kind() == reflect.Uint8 {
151 return
152 }
153
154 break
155 case reflect.Array:
156
157 break
158 case reflect.Map:
159
160 break
161 case reflect.Struct:
162
163 break
164 case reflect.Chan, reflect.Func:
165
166 return
167 }
168
169 return enc.sendActualType(w, state, ut, ut.base)
170 }
171
172
173
174
175 func (enc *Encoder) Encode(e interface{}) error {
176 return enc.EncodeValue(reflect.ValueOf(e))
177 }
178
179
180
181
182 func (enc *Encoder) sendTypeDescriptor(w io.Writer, state *encoderState, ut *userTypeInfo) {
183
184
185 rt := ut.base
186 if ut.externalEnc != 0 {
187 rt = ut.user
188 }
189 if _, alreadySent := enc.sent[rt]; !alreadySent {
190
191 sent := enc.sendType(w, state, rt)
192 if enc.err != nil {
193 return
194 }
195
196
197
198 if !sent {
199 info, err := getTypeInfo(ut)
200 if err != nil {
201 enc.setError(err)
202 return
203 }
204 enc.sent[rt] = info.id
205 }
206 }
207 }
208
209
210 func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) {
211
212 state.encodeInt(int64(enc.sent[ut.base]))
213 }
214
215
216
217
218 func (enc *Encoder) EncodeValue(value reflect.Value) error {
219 if value.Kind() == reflect.Invalid {
220 return errors.New("gob: cannot encode nil value")
221 }
222 if value.Kind() == reflect.Ptr && value.IsNil() {
223 panic("gob: cannot encode nil pointer of type " + value.Type().String())
224 }
225
226
227
228 enc.mutex.Lock()
229 defer enc.mutex.Unlock()
230
231
232 enc.w = enc.w[0:1]
233
234 ut, err := validUserType(value.Type())
235 if err != nil {
236 return err
237 }
238
239 enc.err = nil
240 enc.byteBuf.Reset()
241 enc.byteBuf.Write(spaceForLength)
242 state := enc.newEncoderState(&enc.byteBuf)
243
244 enc.sendTypeDescriptor(enc.writer(), state, ut)
245 enc.sendTypeId(state, ut)
246 if enc.err != nil {
247 return enc.err
248 }
249
250
251 enc.encode(state.b, value, ut)
252 if enc.err == nil {
253 enc.writeMessage(enc.writer(), state.b)
254 }
255
256 enc.freeEncoderState(state)
257 return enc.err
258 }
259
View as plain text