diff options
Diffstat (limited to 'src/pkg/gob/encoder.go')
| -rw-r--r-- | src/pkg/gob/encoder.go | 116 |
1 files changed, 58 insertions, 58 deletions
diff --git a/src/pkg/gob/encoder.go b/src/pkg/gob/encoder.go index 3be455210..cf380cd56 100644 --- a/src/pkg/gob/encoder.go +++ b/src/pkg/gob/encoder.go @@ -182,34 +182,34 @@ package gob import ( - "bytes"; - "io"; - "os"; - "reflect"; - "sync"; + "bytes" + "io" + "os" + "reflect" + "sync" ) // An Encoder manages the transmission of type and data information to the // other side of a connection. type Encoder struct { - mutex sync.Mutex; // each item must be sent atomically - w io.Writer; // where to send the data - sent map[reflect.Type]typeId; // which types we've already sent - state *encoderState; // so we can encode integers, strings directly - countState *encoderState; // stage for writing counts - buf []byte; // for collecting the output. + mutex sync.Mutex // each item must be sent atomically + w io.Writer // where to send the data + sent map[reflect.Type]typeId // which types we've already sent + state *encoderState // so we can encode integers, strings directly + countState *encoderState // stage for writing counts + buf []byte // for collecting the output. } // NewEncoder returns a new encoder that will transmit on the io.Writer. func NewEncoder(w io.Writer) *Encoder { - enc := new(Encoder); - enc.w = w; - enc.sent = make(map[reflect.Type]typeId); - enc.state = new(encoderState); - enc.state.b = new(bytes.Buffer); // the rest isn't important; all we need is buffer and writer - enc.countState = new(encoderState); - enc.countState.b = new(bytes.Buffer); // the rest isn't important; all we need is buffer and writer - return enc; + enc := new(Encoder) + enc.w = w + enc.sent = make(map[reflect.Type]typeId) + enc.state = new(encoderState) + enc.state.b = new(bytes.Buffer) // the rest isn't important; all we need is buffer and writer + enc.countState = new(encoderState) + enc.countState.b = new(bytes.Buffer) // the rest isn't important; all we need is buffer and writer + return enc } func (enc *Encoder) badType(rt reflect.Type) { @@ -217,29 +217,29 @@ func (enc *Encoder) badType(rt reflect.Type) { } func (enc *Encoder) setError(err os.Error) { - if enc.state.err == nil { // remember the first. + if enc.state.err == nil { // remember the first. enc.state.err = err } - enc.state.b.Reset(); + enc.state.b.Reset() } // Send the data item preceded by a unsigned count of its length. func (enc *Encoder) send() { // Encode the length. - encodeUint(enc.countState, uint64(enc.state.b.Len())); + encodeUint(enc.countState, uint64(enc.state.b.Len())) // Build the buffer. - countLen := enc.countState.b.Len(); - total := countLen + enc.state.b.Len(); + countLen := enc.countState.b.Len() + total := countLen + enc.state.b.Len() if total > len(enc.buf) { - enc.buf = make([]byte, total+1000) // extra for growth + enc.buf = make([]byte, total+1000) // extra for growth } // Place the length before the data. // TODO(r): avoid the extra copy here. - enc.countState.b.Read(enc.buf[0:countLen]); + enc.countState.b.Read(enc.buf[0:countLen]) // Now the data. - enc.state.b.Read(enc.buf[countLen:total]); + enc.state.b.Read(enc.buf[countLen:total]) // Write the data. - _, err := enc.w.Write(enc.buf[0:total]); + _, err := enc.w.Write(enc.buf[0:total]) if err != nil { enc.setError(err) } @@ -247,7 +247,7 @@ func (enc *Encoder) send() { func (enc *Encoder) sendType(origt reflect.Type) { // Drill down to the base type. - rt, _ := indirect(origt); + rt, _ := indirect(origt) // We only send structs - everything else is basic or an error switch rt := rt.(type) { @@ -260,14 +260,14 @@ func (enc *Encoder) sendType(origt reflect.Type) { return } // Otherwise we do send. - break; + break // Struct types are not sent, only their element types. case *reflect.StructType: break case *reflect.ChanType, *reflect.FuncType, *reflect.MapType, *reflect.InterfaceType: // Probably a bad field in a struct. - enc.badType(rt); - return; + enc.badType(rt) + return } // Have we already sent this type? This time we ask about the base type. @@ -276,27 +276,27 @@ func (enc *Encoder) sendType(origt reflect.Type) { } // Need to send it. - typeLock.Lock(); - info, err := getTypeInfo(rt); - typeLock.Unlock(); + typeLock.Lock() + info, err := getTypeInfo(rt) + typeLock.Unlock() if err != nil { - enc.setError(err); - return; + enc.setError(err) + return } // Send the pair (-id, type) // Id: - encodeInt(enc.state, -int64(info.id)); + encodeInt(enc.state, -int64(info.id)) // Type: - encode(enc.state.b, info.wire); - enc.send(); + encode(enc.state.b, info.wire) + enc.send() if enc.state.err != nil { return } // Remember we've sent this type. - enc.sent[rt] = info.id; + enc.sent[rt] = info.id // Remember we've sent the top-level, possibly indirect type too. - enc.sent[origt] = info.id; + enc.sent[origt] = info.id // Now send the inner types switch st := rt.(type) { case *reflect.StructType: @@ -306,7 +306,7 @@ func (enc *Encoder) sendType(origt reflect.Type) { case reflect.ArrayOrSliceType: enc.sendType(st.Elem()) } - return; + return } // Encode transmits the data item represented by the empty interface value, @@ -314,40 +314,40 @@ func (enc *Encoder) sendType(origt reflect.Type) { func (enc *Encoder) Encode(e interface{}) os.Error { // Make sure we're single-threaded through here, so multiple // goroutines can share an encoder. - enc.mutex.Lock(); - defer enc.mutex.Unlock(); + enc.mutex.Lock() + defer enc.mutex.Unlock() - enc.state.err = nil; - rt, _ := indirect(reflect.Typeof(e)); + enc.state.err = nil + rt, _ := indirect(reflect.Typeof(e)) // Must be a struct if _, ok := rt.(*reflect.StructType); !ok { - enc.badType(rt); - return enc.state.err; + enc.badType(rt) + return enc.state.err } // Sanity check only: encoder should never come in with data present. if enc.state.b.Len() > 0 || enc.countState.b.Len() > 0 { - enc.state.err = os.ErrorString("encoder: buffer not empty"); - return enc.state.err; + enc.state.err = os.ErrorString("encoder: buffer not empty") + return enc.state.err } // Make sure the type is known to the other side. // First, have we already sent this type? if _, alreadySent := enc.sent[rt]; !alreadySent { // No, so send it. - enc.sendType(rt); + enc.sendType(rt) if enc.state.err != nil { - enc.countState.b.Reset(); - return enc.state.err; + enc.countState.b.Reset() + return enc.state.err } } // Identify the type of this top-level value. - encodeInt(enc.state, int64(enc.sent[rt])); + encodeInt(enc.state, int64(enc.sent[rt])) // Encode the object. - encode(enc.state.b, e); - enc.send(); + encode(enc.state.b, e) + enc.send() - return enc.state.err; + return enc.state.err } |
