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.
15 // A Decoder manages the receipt of type and data information read from the
16 // remote side of a connection.
18 mutex sync.Mutex // each item must be received atomically
19 r io.Reader // source of the data
20 wireType map[typeId]*wireType // map from remote ID to local description
21 decoderCache map[reflect.Type]map[typeId]**decEngine // cache of compiled engines
22 ignorerCache map[typeId]**decEngine // ditto for ignored objects
23 state *decodeState // reads data from in-memory buffer
24 countState *decodeState // reads counts from wire
26 countBuf [9]byte // counts may be uint64s (unlikely!), require 9 bytes
27 byteBuffer *bytes.Buffer
31 // NewDecoder returns a new decoder that reads from the io.Reader.
32 func NewDecoder(r io.Reader) *Decoder {
35 dec.wireType = make(map[typeId]*wireType)
36 dec.state = newDecodeState(dec, &dec.byteBuffer) // buffer set in Decode()
37 dec.decoderCache = make(map[reflect.Type]map[typeId]**decEngine)
38 dec.ignorerCache = make(map[typeId]**decEngine)
43 // recvType loads the definition of a type and reloads the Decoder's buffer.
44 func (dec *Decoder) recvType(id typeId) {
45 // Have we already seen this type? That's an error
46 if dec.wireType[id] != nil {
47 dec.err = os.ErrorString("gob: duplicate type received")
53 dec.err = dec.decode(tWireType, reflect.NewValue(wire))
57 // Remember we've seen this type.
58 dec.wireType[id] = wire
60 // Load the next parcel.
64 // Decode reads the next value from the connection and stores
65 // it in the data represented by the empty interface value.
66 // The value underlying e must be the correct type for the next
67 // data item received, and must be a pointer.
68 func (dec *Decoder) Decode(e interface{}) os.Error {
69 value := reflect.NewValue(e)
70 // If e represents a value as opposed to a pointer, the answer won't
71 // get back to the caller. Make sure it's a pointer.
72 if value.Type().Kind() != reflect.Ptr {
73 dec.err = os.ErrorString("gob: attempt to decode into a non-pointer")
76 return dec.DecodeValue(value)
79 // recv reads the next count-delimited item from the input. It is the converse
81 func (dec *Decoder) recv() {
84 nbytes, dec.err = decodeUintReader(dec.r, dec.countBuf[0:])
88 // Allocate the buffer.
89 if nbytes > uint64(len(dec.buf)) {
90 dec.buf = make([]byte, nbytes+1000)
92 dec.byteBuffer = bytes.NewBuffer(dec.buf[0:nbytes])
95 _, dec.err = io.ReadFull(dec.r, dec.buf[0:nbytes])
97 if dec.err == os.EOF {
98 dec.err = io.ErrUnexpectedEOF
104 // decodeValueFromBuffer grabs the next value from the input. The Decoder's
105 // buffer already contains data. If the next item in the buffer is a type
106 // descriptor, it may be necessary to reload the buffer, but recvType does that.
107 func (dec *Decoder) decodeValueFromBuffer(value reflect.Value, ignoreInterfaceValue, countPresent bool) {
108 for dec.state.b.Len() > 0 {
109 // Receive a type id.
110 id := typeId(dec.state.decodeInt())
113 if id < 0 { // 0 is the error state, handled above
114 // If the id is negative, we have a type.
122 // Make sure the type has been defined already or is a builtin type (for
123 // top-level singleton values).
124 if dec.wireType[id] == nil && builtinIdToType[id] == nil {
128 // An interface value is preceded by a byte count.
130 count := int(dec.state.decodeUint())
131 if ignoreInterfaceValue {
132 // An interface value is preceded by a byte count. Just skip that many bytes.
133 dec.state.b.Next(int(count))
136 // Otherwise fall through and decode it.
138 dec.err = dec.decode(id, value)
143 // DecodeValue reads the next value from the connection and stores
144 // it in the data represented by the reflection value.
145 // The value must be the correct type for the next
146 // data item received.
147 func (dec *Decoder) DecodeValue(value reflect.Value) os.Error {
148 // Make sure we're single-threaded through here.
150 defer dec.mutex.Unlock()
157 dec.decodeValueFromBuffer(value, false, false)
161 // If debug.go is compiled into the program , debugFunc prints a human-readable
162 // representation of the gob data read from r by calling that file's Debug function.
163 // Otherwise it is nil.
164 var debugFunc func(io.Reader)