The Go Programming Language

Source file src/pkg/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		"bytes"
     9		"io"
    10		"os"
    11		"reflect"
    12		"sync"
    13	)
    14	
    15	// An Encoder manages the transmission of type and data information to the
    16	// other side of a connection.
    17	type Encoder struct {
    18		mutex      sync.Mutex              // each item must be sent atomically
    19		w          []io.Writer             // where to send the data
    20		sent       map[reflect.Type]typeId // which types we've already sent
    21		countState *encoderState           // stage for writing counts
    22		freeList   *encoderState           // list of free encoderStates; avoids reallocation
    23		buf        []byte                  // for collecting the output.
    24		byteBuf    bytes.Buffer            // buffer for top-level encoderState
    25		err        os.Error
    26	}
    27	
    28	// NewEncoder returns a new encoder that will transmit on the io.Writer.
    29	func NewEncoder(w io.Writer) *Encoder {
    30		enc := new(Encoder)
    31		enc.w = []io.Writer{w}
    32		enc.sent = make(map[reflect.Type]typeId)
    33		enc.countState = enc.newEncoderState(new(bytes.Buffer))
    34		return enc
    35	}
    36	
    37	// writer() returns the innermost writer the encoder is using
    38	func (enc *Encoder) writer() io.Writer {
    39		return enc.w[len(enc.w)-1]
    40	}
    41	
    42	// pushWriter adds a writer to the encoder.
    43	func (enc *Encoder) pushWriter(w io.Writer) {
    44		enc.w = append(enc.w, w)
    45	}
    46	
    47	// popWriter pops the innermost writer.
    48	func (enc *Encoder) popWriter() {
    49		enc.w = enc.w[0 : len(enc.w)-1]
    50	}
    51	
    52	func (enc *Encoder) badType(rt reflect.Type) {
    53		enc.setError(os.NewError("gob: can't encode type " + rt.String()))
    54	}
    55	
    56	func (enc *Encoder) setError(err os.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 *bytes.Buffer) {
    64		enc.countState.encodeUint(uint64(b.Len()))
    65		// Build the buffer.
    66		countLen := enc.countState.b.Len()
    67		total := countLen + b.Len()
    68		if total > len(enc.buf) {
    69			enc.buf = make([]byte, total+1000) // extra for growth
    70		}
    71		// Place the length before the data.
    72		// TODO(r): avoid the extra copy here.
    73		enc.countState.b.Read(enc.buf[0:countLen])
    74		// Now the data.
    75		b.Read(enc.buf[countLen:total])
    76		// Write the data.
    77		_, err := w.Write(enc.buf[0:total])
    78		if err != nil {
    79			enc.setError(err)
    80		}
    81	}
    82	
    83	// sendActualType sends the requested type, without further investigation, unless
    84	// it's been sent before.
    85	func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTypeInfo, actual reflect.Type) (sent bool) {
    86		if _, alreadySent := enc.sent[actual]; alreadySent {
    87			return false
    88		}
    89		typeLock.Lock()
    90		info, err := getTypeInfo(ut)
    91		typeLock.Unlock()
    92		if err != nil {
    93			enc.setError(err)
    94			return
    95		}
    96		// Send the pair (-id, type)
    97		// Id:
    98		state.encodeInt(-int64(info.id))
    99		// Type:
   100		enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
   101		enc.writeMessage(w, state.b)
   102		if enc.err != nil {
   103			return
   104		}
   105	
   106		// Remember we've sent this type, both what the user gave us and the base type.
   107		enc.sent[ut.base] = info.id
   108		if ut.user != ut.base {
   109			enc.sent[ut.user] = info.id
   110		}
   111		// Now send the inner types
   112		switch st := actual; st.Kind() {
   113		case reflect.Struct:
   114			for i := 0; i < st.NumField(); i++ {
   115				enc.sendType(w, state, st.Field(i).Type)
   116			}
   117		case reflect.Array, reflect.Slice:
   118			enc.sendType(w, state, st.Elem())
   119		case reflect.Map:
   120			enc.sendType(w, state, st.Key())
   121			enc.sendType(w, state, st.Elem())
   122		}
   123		return true
   124	}
   125	
   126	// sendType sends the type info to the other side, if necessary. 
   127	func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
   128		ut := userType(origt)
   129		if ut.isGobEncoder {
   130			// The rules are different: regardless of the underlying type's representation,
   131			// we need to tell the other side that this exact type is a GobEncoder.
   132			return enc.sendActualType(w, state, ut, ut.user)
   133		}
   134	
   135		// It's a concrete value, so drill down to the base type.
   136		switch rt := ut.base; rt.Kind() {
   137		default:
   138			// Basic types and interfaces do not need to be described.
   139			return
   140		case reflect.Slice:
   141			// If it's []uint8, don't send; it's considered basic.
   142			if rt.Elem().Kind() == reflect.Uint8 {
   143				return
   144			}
   145			// Otherwise we do send.
   146			break
   147		case reflect.Array:
   148			// arrays must be sent so we know their lengths and element types.
   149			break
   150		case reflect.Map:
   151			// maps must be sent so we know their lengths and key/value types.
   152			break
   153		case reflect.Struct:
   154			// structs must be sent so we know their fields.
   155			break
   156		case reflect.Chan, reflect.Func:
   157			// Probably a bad field in a struct.
   158			enc.badType(rt)
   159			return
   160		}
   161	
   162		return enc.sendActualType(w, state, ut, ut.base)
   163	}
   164	
   165	// Encode transmits the data item represented by the empty interface value,
   166	// guaranteeing that all necessary type information has been transmitted first.
   167	func (enc *Encoder) Encode(e interface{}) os.Error {
   168		return enc.EncodeValue(reflect.ValueOf(e))
   169	}
   170	
   171	// sendTypeDescriptor makes sure the remote side knows about this type.
   172	// It will send a descriptor if this is the first time the type has been
   173	// sent.
   174	func (enc *Encoder) sendTypeDescriptor(w io.Writer, state *encoderState, ut *userTypeInfo) {
   175		// Make sure the type is known to the other side.
   176		// First, have we already sent this type?
   177		rt := ut.base
   178		if ut.isGobEncoder {
   179			rt = ut.user
   180		}
   181		if _, alreadySent := enc.sent[rt]; !alreadySent {
   182			// No, so send it.
   183			sent := enc.sendType(w, state, rt)
   184			if enc.err != nil {
   185				return
   186			}
   187			// If the type info has still not been transmitted, it means we have
   188			// a singleton basic type (int, []byte etc.) at top level.  We don't
   189			// need to send the type info but we do need to update enc.sent.
   190			if !sent {
   191				typeLock.Lock()
   192				info, err := getTypeInfo(ut)
   193				typeLock.Unlock()
   194				if err != nil {
   195					enc.setError(err)
   196					return
   197				}
   198				enc.sent[rt] = info.id
   199			}
   200		}
   201	}
   202	
   203	// sendTypeId sends the id, which must have already been defined.
   204	func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) {
   205		// Identify the type of this top-level value.
   206		state.encodeInt(int64(enc.sent[ut.base]))
   207	}
   208	
   209	// EncodeValue transmits the data item represented by the reflection value,
   210	// guaranteeing that all necessary type information has been transmitted first.
   211	func (enc *Encoder) EncodeValue(value reflect.Value) os.Error {
   212		// Make sure we're single-threaded through here, so multiple
   213		// goroutines can share an encoder.
   214		enc.mutex.Lock()
   215		defer enc.mutex.Unlock()
   216	
   217		// Remove any nested writers remaining due to previous errors.
   218		enc.w = enc.w[0:1]
   219	
   220		ut, err := validUserType(value.Type())
   221		if err != nil {
   222			return err
   223		}
   224	
   225		enc.err = nil
   226		enc.byteBuf.Reset()
   227		state := enc.newEncoderState(&enc.byteBuf)
   228	
   229		enc.sendTypeDescriptor(enc.writer(), state, ut)
   230		enc.sendTypeId(state, ut)
   231		if enc.err != nil {
   232			return enc.err
   233		}
   234	
   235		// Encode the object.
   236		enc.encode(state.b, value, ut)
   237		if enc.err == nil {
   238			enc.writeMessage(enc.writer(), state.b)
   239		}
   240	
   241		enc.freeEncoderState(state)
   242		return enc.err
   243	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.