OSDN Git Service

Remove the types float and complex.
[pf3gnuchains/gcc-fork.git] / libgo / go / gob / decoder.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 // A Decoder manages the receipt of type and data information read from the
16 // remote side of a connection.
17 type Decoder struct {
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
25         buf          []byte
26         countBuf     [9]byte // counts may be uint64s (unlikely!), require 9 bytes
27         byteBuffer   *bytes.Buffer
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 = r
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)
39
40         return dec
41 }
42
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")
48                 return
49         }
50
51         // Type:
52         wire := new(wireType)
53         dec.err = dec.decode(tWireType, reflect.NewValue(wire))
54         if dec.err != nil {
55                 return
56         }
57         // Remember we've seen this type.
58         dec.wireType[id] = wire
59
60         // Load the next parcel.
61         dec.recv()
62 }
63
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")
74                 return dec.err
75         }
76         return dec.DecodeValue(value)
77 }
78
79 // recv reads the next count-delimited item from the input. It is the converse
80 // of Encoder.send.
81 func (dec *Decoder) recv() {
82         // Read a count.
83         var nbytes uint64
84         nbytes, dec.err = decodeUintReader(dec.r, dec.countBuf[0:])
85         if dec.err != nil {
86                 return
87         }
88         // Allocate the buffer.
89         if nbytes > uint64(len(dec.buf)) {
90                 dec.buf = make([]byte, nbytes+1000)
91         }
92         dec.byteBuffer = bytes.NewBuffer(dec.buf[0:nbytes])
93
94         // Read the data
95         _, dec.err = io.ReadFull(dec.r, dec.buf[0:nbytes])
96         if dec.err != nil {
97                 if dec.err == os.EOF {
98                         dec.err = io.ErrUnexpectedEOF
99                 }
100                 return
101         }
102 }
103
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())
111
112                 // Is it a new type?
113                 if id < 0 { // 0 is the error state, handled above
114                         // If the id is negative, we have a type.
115                         dec.recvType(-id)
116                         if dec.err != nil {
117                                 break
118                         }
119                         continue
120                 }
121
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 {
125                         dec.err = errBadType
126                         break
127                 }
128                 // An interface value is preceded by a byte count.
129                 if countPresent {
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))
134                                 break
135                         }
136                         // Otherwise fall through and decode it.
137                 }
138                 dec.err = dec.decode(id, value)
139                 break
140         }
141 }
142
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.
149         dec.mutex.Lock()
150         defer dec.mutex.Unlock()
151
152         dec.err = nil
153         dec.recv()
154         if dec.err != nil {
155                 return dec.err
156         }
157         dec.decodeValueFromBuffer(value, false, false)
158         return dec.err
159 }
160
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)