...
Run Format

Source file src/encoding/gob/encoder.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package gob
     6	
     7	import (
     8		"errors"
     9		"io"
    10		"reflect"
    11		"sync"
    12	)
    13	
    14	// An Encoder manages the transmission of type and data information to the
    15	// other side of a connection.
    16	type Encoder struct {
    17		mutex      sync.Mutex              // each item must be sent atomically
    18		w          []io.Writer             // where to send the data
    19		sent       map[reflect.Type]typeId // which types we've already sent
    20		countState *encoderState           // stage for writing counts
    21		freeList   *encoderState           // list of free encoderStates; avoids reallocation
    22		byteBuf    encBuffer               // buffer for top-level encoderState
    23		err        error
    24	}
    25	
    26	// Before we encode a message, we reserve space at the head of the
    27	// buffer in which to encode its length. This means we can use the
    28	// buffer to assemble the message without another allocation.
    29	const maxLength = 9 // Maximum size of an encoded length.
    30	var spaceForLength = make([]byte, maxLength)
    31	
    32	// NewEncoder returns a new encoder that will transmit on the io.Writer.
    33	func NewEncoder(w io.Writer) *Encoder {
    34		enc := new(Encoder)
    35		enc.w = []io.Writer{w}
    36		enc.sent = make(map[reflect.Type]typeId)
    37		enc.countState = enc.newEncoderState(new(encBuffer))
    38		return enc
    39	}
    40	
    41	// writer() returns the innermost writer the encoder is using
    42	func (enc *Encoder) writer() io.Writer {
    43		return enc.w[len(enc.w)-1]
    44	}
    45	
    46	// pushWriter adds a writer to the encoder.
    47	func (enc *Encoder) pushWriter(w io.Writer) {
    48		enc.w = append(enc.w, w)
    49	}
    50	
    51	// popWriter pops the innermost writer.
    52	func (enc *Encoder) popWriter() {
    53		enc.w = enc.w[0 : len(enc.w)-1]
    54	}
    55	
    56	func (enc *Encoder) setError(err error) {
    57		if enc.err == nil { // remember the first.
    58			enc.err = err
    59		}
    60	}
    61	
    62	// writeMessage sends the data item preceded by a unsigned count of its length.
    63	func (enc *Encoder) writeMessage(w io.Writer, b *encBuffer) {
    64		// Space has been reserved for the length at the head of the message.
    65		// This is a little dirty: we grab the slice from the bytes.Buffer and massage
    66		// it by hand.
    67		message := b.Bytes()
    68		messageLen := len(message) - maxLength
    69		// Length cannot be bigger than the decoder can handle.
    70		if messageLen >= tooBig {
    71			enc.setError(errors.New("gob: encoder: message too big"))
    72			return
    73		}
    74		// Encode the length.
    75		enc.countState.b.Reset()
    76		enc.countState.encodeUint(uint64(messageLen))
    77		// Copy the length to be a prefix of the message.
    78		offset := maxLength - enc.countState.b.Len()
    79		copy(message[offset:], enc.countState.b.Bytes())
    80		// Write the data.
    81		_, err := w.Write(message[offset:])
    82		// Drain the buffer and restore the space at the front for the count of the next message.
    83		b.Reset()
    84		b.Write(spaceForLength)
    85		if err != nil {
    86			enc.setError(err)
    87		}
    88	}
    89	
    90	// sendActualType sends the requested type, without further investigation, unless
    91	// it's been sent before.
    92	func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTypeInfo, actual reflect.Type) (sent bool) {
    93		if _, alreadySent := enc.sent[actual]; alreadySent {
    94			return false
    95		}
    96		info, err := getTypeInfo(ut)
    97		if err != nil {
    98			enc.setError(err)
    99			return
   100		}
   101		// Send the pair (-id, type)
   102		// Id:
   103		state.encodeInt(-int64(info.id))
   104		// Type:
   105		enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
   106		enc.writeMessage(w, state.b)
   107		if enc.err != nil {
   108			return
   109		}
   110	
   111		// Remember we've sent this type, both what the user gave us and the base type.
   112		enc.sent[ut.base] = info.id
   113		if ut.user != ut.base {
   114			enc.sent[ut.user] = info.id
   115		}
   116		// Now send the inner types
   117		switch st := actual; st.Kind() {
   118		case reflect.Struct:
   119			for i := 0; i < st.NumField(); i++ {
   120				if isExported(st.Field(i).Name) {
   121					enc.sendType(w, state, st.Field(i).Type)
   122				}
   123			}
   124		case reflect.Array, reflect.Slice:
   125			enc.sendType(w, state, st.Elem())
   126		case reflect.Map:
   127			enc.sendType(w, state, st.Key())
   128			enc.sendType(w, state, st.Elem())
   129		}
   130		return true
   131	}
   132	
   133	// sendType sends the type info to the other side, if necessary.
   134	func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
   135		ut := userType(origt)
   136		if ut.externalEnc != 0 {
   137			// The rules are different: regardless of the underlying type's representation,
   138			// we need to tell the other side that the base type is a GobEncoder.
   139			return enc.sendActualType(w, state, ut, ut.base)
   140		}
   141	
   142		// It's a concrete value, so drill down to the base type.
   143		switch rt := ut.base; rt.Kind() {
   144		default:
   145			// Basic types and interfaces do not need to be described.
   146			return
   147		case reflect.Slice:
   148			// If it's []uint8, don't send; it's considered basic.
   149			if rt.Elem().Kind() == reflect.Uint8 {
   150				return
   151			}
   152			// Otherwise we do send.
   153			break
   154		case reflect.Array:
   155			// arrays must be sent so we know their lengths and element types.
   156			break
   157		case reflect.Map:
   158			// maps must be sent so we know their lengths and key/value types.
   159			break
   160		case reflect.Struct:
   161			// structs must be sent so we know their fields.
   162			break
   163		case reflect.Chan, reflect.Func:
   164			// If we get here, it's a field of a struct; ignore it.
   165			return
   166		}
   167	
   168		return enc.sendActualType(w, state, ut, ut.base)
   169	}
   170	
   171	// Encode transmits the data item represented by the empty interface value,
   172	// guaranteeing that all necessary type information has been transmitted first.
   173	func (enc *Encoder) Encode(e interface{}) error {
   174		return enc.EncodeValue(reflect.ValueOf(e))
   175	}
   176	
   177	// sendTypeDescriptor makes sure the remote side knows about this type.
   178	// It will send a descriptor if this is the first time the type has been
   179	// sent.
   180	func (enc *Encoder) sendTypeDescriptor(w io.Writer, state *encoderState, ut *userTypeInfo) {
   181		// Make sure the type is known to the other side.
   182		// First, have we already sent this type?
   183		rt := ut.base
   184		if ut.externalEnc != 0 {
   185			rt = ut.user
   186		}
   187		if _, alreadySent := enc.sent[rt]; !alreadySent {
   188			// No, so send it.
   189			sent := enc.sendType(w, state, rt)
   190			if enc.err != nil {
   191				return
   192			}
   193			// If the type info has still not been transmitted, it means we have
   194			// a singleton basic type (int, []byte etc.) at top level.  We don't
   195			// need to send the type info but we do need to update enc.sent.
   196			if !sent {
   197				info, err := getTypeInfo(ut)
   198				if err != nil {
   199					enc.setError(err)
   200					return
   201				}
   202				enc.sent[rt] = info.id
   203			}
   204		}
   205	}
   206	
   207	// sendTypeId sends the id, which must have already been defined.
   208	func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) {
   209		// Identify the type of this top-level value.
   210		state.encodeInt(int64(enc.sent[ut.base]))
   211	}
   212	
   213	// EncodeValue transmits the data item represented by the reflection value,
   214	// guaranteeing that all necessary type information has been transmitted first.
   215	func (enc *Encoder) EncodeValue(value reflect.Value) error {
   216		// Gobs contain values. They cannot represent nil pointers, which
   217		// have no value to encode.
   218		if value.Kind() == reflect.Ptr && value.IsNil() {
   219			panic("gob: cannot encode nil pointer of type " + value.Type().String())
   220		}
   221	
   222		// Make sure we're single-threaded through here, so multiple
   223		// goroutines can share an encoder.
   224		enc.mutex.Lock()
   225		defer enc.mutex.Unlock()
   226	
   227		// Remove any nested writers remaining due to previous errors.
   228		enc.w = enc.w[0:1]
   229	
   230		ut, err := validUserType(value.Type())
   231		if err != nil {
   232			return err
   233		}
   234	
   235		enc.err = nil
   236		enc.byteBuf.Reset()
   237		enc.byteBuf.Write(spaceForLength)
   238		state := enc.newEncoderState(&enc.byteBuf)
   239	
   240		enc.sendTypeDescriptor(enc.writer(), state, ut)
   241		enc.sendTypeId(state, ut)
   242		if enc.err != nil {
   243			return enc.err
   244		}
   245	
   246		// Encode the object.
   247		enc.encode(state.b, value, ut)
   248		if enc.err == nil {
   249			enc.writeMessage(enc.writer(), state.b)
   250		}
   251	
   252		enc.freeEncoderState(state)
   253		return enc.err
   254	}
   255	

View as plain text