github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/fix/testdata/reflect.decoder.go.out (about)

     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  	"bufio"
     9  	"bytes"
    10  	"io"
    11  	"os"
    12  	"reflect"
    13  	"sync"
    14  )
    15  
    16  // A Decoder manages the receipt of type and data information read from the
    17  // remote side of a connection.
    18  type Decoder struct {
    19  	mutex        sync.Mutex                              // each item must be received atomically
    20  	r            io.Reader                               // source of the data
    21  	buf          bytes.Buffer                            // buffer for more efficient i/o from r
    22  	wireType     map[typeId]*wireType                    // map from remote ID to local description
    23  	decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
    24  	ignorerCache map[typeId]**decEngine                  // ditto for ignored objects
    25  	freeList     *decoderState                           // list of free decoderStates; avoids reallocation
    26  	countBuf     []byte                                  // used for decoding integers while parsing messages
    27  	tmp          []byte                                  // temporary storage for i/o; saves reallocating
    28  	err          os.Error
    29  }
    30  
    31  // NewDecoder returns a new decoder that reads from the io.Reader.
    32  func NewDecoder(r io.Reader) *Decoder {
    33  	dec := new(Decoder)
    34  	dec.r = bufio.NewReader(r)
    35  	dec.wireType = make(map[typeId]*wireType)
    36  	dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
    37  	dec.ignorerCache = make(map[typeId]**decEngine)
    38  	dec.countBuf = make([]byte, 9) // counts may be uint64s (unlikely!), require 9 bytes
    39  
    40  	return dec
    41  }
    42  
    43  // recvType loads the definition of a type.
    44  func (dec *Decoder) recvType(id typeId) {
    45  	// Have we already seen this type?  That's an error
    46  	if id < firstUserId || dec.wireType[id] != nil {
    47  		dec.err = os.NewError("gob: duplicate type received")
    48  		return
    49  	}
    50  
    51  	// Type:
    52  	wire := new(wireType)
    53  	dec.decodeValue(tWireType, reflect.ValueOf(wire))
    54  	if dec.err != nil {
    55  		return
    56  	}
    57  	// Remember we've seen this type.
    58  	dec.wireType[id] = wire
    59  }
    60  
    61  // recvMessage reads the next count-delimited item from the input. It is the converse
    62  // of Encoder.writeMessage. It returns false on EOF or other error reading the message.
    63  func (dec *Decoder) recvMessage() bool {
    64  	// Read a count.
    65  	nbytes, _, err := decodeUintReader(dec.r, dec.countBuf)
    66  	if err != nil {
    67  		dec.err = err
    68  		return false
    69  	}
    70  	dec.readMessage(int(nbytes))
    71  	return dec.err == nil
    72  }
    73  
    74  // readMessage reads the next nbytes bytes from the input.
    75  func (dec *Decoder) readMessage(nbytes int) {
    76  	// Allocate the buffer.
    77  	if cap(dec.tmp) < nbytes {
    78  		dec.tmp = make([]byte, nbytes+100) // room to grow
    79  	}
    80  	dec.tmp = dec.tmp[:nbytes]
    81  
    82  	// Read the data
    83  	_, dec.err = io.ReadFull(dec.r, dec.tmp)
    84  	if dec.err != nil {
    85  		if dec.err == os.EOF {
    86  			dec.err = io.ErrUnexpectedEOF
    87  		}
    88  		return
    89  	}
    90  	dec.buf.Write(dec.tmp)
    91  }
    92  
    93  // toInt turns an encoded uint64 into an int, according to the marshaling rules.
    94  func toInt(x uint64) int64 {
    95  	i := int64(x >> 1)
    96  	if x&1 != 0 {
    97  		i = ^i
    98  	}
    99  	return i
   100  }
   101  
   102  func (dec *Decoder) nextInt() int64 {
   103  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   104  	if err != nil {
   105  		dec.err = err
   106  	}
   107  	return toInt(n)
   108  }
   109  
   110  func (dec *Decoder) nextUint() uint64 {
   111  	n, _, err := decodeUintReader(&dec.buf, dec.countBuf)
   112  	if err != nil {
   113  		dec.err = err
   114  	}
   115  	return n
   116  }
   117  
   118  // decodeTypeSequence parses:
   119  // TypeSequence
   120  //	(TypeDefinition DelimitedTypeDefinition*)?
   121  // and returns the type id of the next value.  It returns -1 at
   122  // EOF.  Upon return, the remainder of dec.buf is the value to be
   123  // decoded.  If this is an interface value, it can be ignored by
   124  // simply resetting that buffer.
   125  func (dec *Decoder) decodeTypeSequence(isInterface bool) typeId {
   126  	for dec.err == nil {
   127  		if dec.buf.Len() == 0 {
   128  			if !dec.recvMessage() {
   129  				break
   130  			}
   131  		}
   132  		// Receive a type id.
   133  		id := typeId(dec.nextInt())
   134  		if id >= 0 {
   135  			// Value follows.
   136  			return id
   137  		}
   138  		// Type definition for (-id) follows.
   139  		dec.recvType(-id)
   140  		// When decoding an interface, after a type there may be a
   141  		// DelimitedValue still in the buffer.  Skip its count.
   142  		// (Alternatively, the buffer is empty and the byte count
   143  		// will be absorbed by recvMessage.)
   144  		if dec.buf.Len() > 0 {
   145  			if !isInterface {
   146  				dec.err = os.NewError("extra data in buffer")
   147  				break
   148  			}
   149  			dec.nextUint()
   150  		}
   151  	}
   152  	return -1
   153  }
   154  
   155  // Decode reads the next value from the connection and stores
   156  // it in the data represented by the empty interface value.
   157  // If e is nil, the value will be discarded. Otherwise,
   158  // the value underlying e must either be the correct type for the next
   159  // data item received, and must be a pointer.
   160  func (dec *Decoder) Decode(e interface{}) os.Error {
   161  	if e == nil {
   162  		return dec.DecodeValue(reflect.Value{})
   163  	}
   164  	value := reflect.ValueOf(e)
   165  	// If e represents a value as opposed to a pointer, the answer won't
   166  	// get back to the caller.  Make sure it's a pointer.
   167  	if value.Type().Kind() != reflect.Ptr {
   168  		dec.err = os.NewError("gob: attempt to decode into a non-pointer")
   169  		return dec.err
   170  	}
   171  	return dec.DecodeValue(value)
   172  }
   173  
   174  // DecodeValue reads the next value from the connection and stores
   175  // it in the data represented by the reflection value.
   176  // The value must be the correct type for the next
   177  // data item received, or it may be nil, which means the
   178  // value will be discarded.
   179  func (dec *Decoder) DecodeValue(value reflect.Value) os.Error {
   180  	// Make sure we're single-threaded through here.
   181  	dec.mutex.Lock()
   182  	defer dec.mutex.Unlock()
   183  
   184  	dec.buf.Reset() // In case data lingers from previous invocation.
   185  	dec.err = nil
   186  	id := dec.decodeTypeSequence(false)
   187  	if dec.err == nil {
   188  		dec.decodeValue(id, value)
   189  	}
   190  	return dec.err
   191  }
   192  
   193  // If debug.go is compiled into the program , debugFunc prints a human-readable
   194  // representation of the gob data read from r by calling that file's Debug function.
   195  // Otherwise it is nil.
   196  var debugFunc func(io.Reader)