OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package gob | 5 package gob |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "io" | 9 "io" |
10 "os" | 10 "os" |
(...skipping 30 matching lines...) Expand all Loading... |
41 func (enc *Encoder) setError(err os.Error) { | 41 func (enc *Encoder) setError(err os.Error) { |
42 if enc.err == nil { // remember the first. | 42 if enc.err == nil { // remember the first. |
43 enc.err = err | 43 enc.err = err |
44 } | 44 } |
45 enc.state.b.Reset() | 45 enc.state.b.Reset() |
46 } | 46 } |
47 | 47 |
48 // Send the data item preceded by a unsigned count of its length. | 48 // Send the data item preceded by a unsigned count of its length. |
49 func (enc *Encoder) send() { | 49 func (enc *Encoder) send() { |
50 // Encode the length. | 50 // Encode the length. |
51 » encodeUint(enc.countState, uint64(enc.state.b.Len())) | 51 » enc.countState.encodeUint(uint64(enc.state.b.Len())) |
52 // Build the buffer. | 52 // Build the buffer. |
53 countLen := enc.countState.b.Len() | 53 countLen := enc.countState.b.Len() |
54 total := countLen + enc.state.b.Len() | 54 total := countLen + enc.state.b.Len() |
55 if total > len(enc.buf) { | 55 if total > len(enc.buf) { |
56 enc.buf = make([]byte, total+1000) // extra for growth | 56 enc.buf = make([]byte, total+1000) // extra for growth |
57 } | 57 } |
58 // Place the length before the data. | 58 // Place the length before the data. |
59 // TODO(r): avoid the extra copy here. | 59 // TODO(r): avoid the extra copy here. |
60 enc.countState.b.Read(enc.buf[0:countLen]) | 60 enc.countState.b.Read(enc.buf[0:countLen]) |
61 // Now the data. | 61 // Now the data. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // Need to send it. | 105 // Need to send it. |
106 typeLock.Lock() | 106 typeLock.Lock() |
107 info, err := getTypeInfo(rt) | 107 info, err := getTypeInfo(rt) |
108 typeLock.Unlock() | 108 typeLock.Unlock() |
109 if err != nil { | 109 if err != nil { |
110 enc.setError(err) | 110 enc.setError(err) |
111 return | 111 return |
112 } | 112 } |
113 // Send the pair (-id, type) | 113 // Send the pair (-id, type) |
114 // Id: | 114 // Id: |
115 » encodeInt(enc.state, -int64(info.id)) | 115 » enc.state.encodeInt(-int64(info.id)) |
116 // Type: | 116 // Type: |
117 enc.encode(enc.state.b, reflect.NewValue(info.wire)) | 117 enc.encode(enc.state.b, reflect.NewValue(info.wire)) |
118 enc.send() | 118 enc.send() |
119 if enc.err != nil { | 119 if enc.err != nil { |
120 return | 120 return |
121 } | 121 } |
122 | 122 |
123 // Remember we've sent this type. | 123 // Remember we've sent this type. |
124 enc.sent[rt] = info.id | 124 enc.sent[rt] = info.id |
125 // Remember we've sent the top-level, possibly indirect type too. | 125 // Remember we've sent the top-level, possibly indirect type too. |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 typeLock.Unlock() | 163 typeLock.Unlock() |
164 if err != nil { | 164 if err != nil { |
165 enc.setError(err) | 165 enc.setError(err) |
166 return | 166 return |
167 } | 167 } |
168 enc.sent[rt] = info.id | 168 enc.sent[rt] = info.id |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 // Identify the type of this top-level value. | 172 // Identify the type of this top-level value. |
173 » encodeInt(enc.state, int64(enc.sent[rt])) | 173 » enc.state.encodeInt(int64(enc.sent[rt])) |
174 } | 174 } |
175 | 175 |
176 // EncodeValue transmits the data item represented by the reflection value, | 176 // EncodeValue transmits the data item represented by the reflection value, |
177 // guaranteeing that all necessary type information has been transmitted first. | 177 // guaranteeing that all necessary type information has been transmitted first. |
178 func (enc *Encoder) EncodeValue(value reflect.Value) os.Error { | 178 func (enc *Encoder) EncodeValue(value reflect.Value) os.Error { |
179 // Make sure we're single-threaded through here, so multiple | 179 // Make sure we're single-threaded through here, so multiple |
180 // goroutines can share an encoder. | 180 // goroutines can share an encoder. |
181 enc.mutex.Lock() | 181 enc.mutex.Lock() |
182 defer enc.mutex.Unlock() | 182 defer enc.mutex.Unlock() |
183 | 183 |
(...skipping 14 matching lines...) Expand all Loading... |
198 // Encode the object. | 198 // Encode the object. |
199 err := enc.encode(enc.state.b, value) | 199 err := enc.encode(enc.state.b, value) |
200 if err != nil { | 200 if err != nil { |
201 enc.setError(err) | 201 enc.setError(err) |
202 } else { | 202 } else { |
203 enc.send() | 203 enc.send() |
204 } | 204 } |
205 | 205 |
206 return enc.err | 206 return enc.err |
207 } | 207 } |
OLD | NEW |