LEFT | RIGHT |
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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 // writeMessage sends the data item preceded by a unsigned count of its length. | 67 // writeMessage sends the data item preceded by a unsigned count of its length. |
68 func (enc *Encoder) writeMessage(w io.Writer, b *bytes.Buffer) { | 68 func (enc *Encoder) writeMessage(w io.Writer, b *bytes.Buffer) { |
69 // Space has been reserved for the length at the head of the message. | 69 // Space has been reserved for the length at the head of the message. |
70 // This is a little dirty: we grab the slice from the bytes.Buffer and m
assage | 70 // This is a little dirty: we grab the slice from the bytes.Buffer and m
assage |
71 // it by hand. | 71 // it by hand. |
72 message := b.Bytes() | 72 message := b.Bytes() |
73 messageLen := len(message) - maxLength | 73 messageLen := len(message) - maxLength |
74 » // Encode the length at the beginning of the buffer. | 74 » // Encode the length. |
75 enc.countState.b.Reset() | 75 enc.countState.b.Reset() |
76 enc.countState.encodeUint(uint64(messageLen)) | 76 enc.countState.encodeUint(uint64(messageLen)) |
77 » // Copy the message to the buffer. | 77 » // Copy the length to be a prefix of the message. |
78 » countLen := copy(message, enc.countState.b.Bytes()) | 78 » offset := maxLength - enc.countState.b.Len() |
79 » // Close the gap | 79 » copy(message[offset:], enc.countState.b.Bytes()) |
80 » if countLen < maxLength { | |
81 » » copy(message[countLen:], message[maxLength:]) | |
82 » } | |
83 // Write the data. | 80 // Write the data. |
84 » _, err := w.Write(message[:countLen+len(message)-maxLength]) | 81 » _, err := w.Write(message[offset:]) |
85 // Drain the buffer and restore the space at the front for the count of
the next message. | 82 // Drain the buffer and restore the space at the front for the count of
the next message. |
86 b.Reset() | 83 b.Reset() |
87 b.Write(spaceForLength) | 84 b.Write(spaceForLength) |
88 if err != nil { | 85 if err != nil { |
89 enc.setError(err) | 86 enc.setError(err) |
90 } | 87 } |
91 } | 88 } |
92 | 89 |
93 // sendActualType sends the requested type, without further investigation, unles
s | 90 // sendActualType sends the requested type, without further investigation, unles
s |
94 // it's been sent before. | 91 // it's been sent before. |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 242 |
246 // Encode the object. | 243 // Encode the object. |
247 enc.encode(state.b, value, ut) | 244 enc.encode(state.b, value, ut) |
248 if enc.err == nil { | 245 if enc.err == nil { |
249 enc.writeMessage(enc.writer(), state.b) | 246 enc.writeMessage(enc.writer(), state.b) |
250 } | 247 } |
251 | 248 |
252 enc.freeEncoderState(state) | 249 enc.freeEncoderState(state) |
253 return enc.err | 250 return enc.err |
254 } | 251 } |
LEFT | RIGHT |