Esempio n. 1
1
func TestJsonHelperGobEncoding(t *testing.T) {
	raw := `{"testing": 123,"name":"bob & more"}`
	reader := strings.NewReader(raw)
	jh, err := NewJsonHelperReader(reader)
	assert.Tf(t, err == nil, "Unexpected error decoding gob: %s", err)
	assert.Tf(t, jh.Int("testing") == 123, "Unexpected value in gob: %d", jh.Int("testing"))
	var buf bytes.Buffer
	err = gob.NewEncoder(&buf).Encode(&jh)
	assert.T(t, err == nil, err)

	var jhNew JsonHelper
	err = gob.NewDecoder(&buf).Decode(&jhNew)
	assert.T(t, err == nil, err)
	assert.Tf(t, jhNew.Int("testing") == 123, "Unexpected value in gob: %d", jhNew.Int("testing"))
	assert.Tf(t, jhNew.String("name") == "bob & more", "Unexpected value in gob: %d", jhNew.String("name"))

	buf2 := bytes.Buffer{}
	gt := GobTest{"Hello", jh}
	err = gob.NewEncoder(&buf2).Encode(&gt)
	assert.T(t, err == nil, err)

	var gt2 GobTest
	err = gob.NewDecoder(&buf2).Decode(&gt2)
	assert.T(t, err == nil, err)
	assert.Tf(t, gt2.Name == "Hello", "Unexpected value in gob: %d", gt2.Name)
	assert.Tf(t, gt2.Data.Int("testing") == 123, "Unexpected value in gob: %d", gt2.Data.Int("testing"))
	assert.Tf(t, gt2.Data.String("name") == "bob & more", "Unexpected value in gob: %d", gt2.Data.String("name"))
}
Esempio n. 2
0
func ReadFromDisk() {
	file, err := os.Open("data/datafile.gob")
	if err == nil {
		dec := gob.NewDecoder(file)
		err = dec.Decode(&data)
		file.Close()
		if err != nil {
			log.Fatal("Unable to decode:", err)
		}
	}
	var packet WritePacket
	for ii := 0; ii < 128; ii++ {
		file, err := os.Open("data/datafile.log." + fmt.Sprintf("%03d", ii))
		if err != nil {
			break
		} else {
			dec := gob.NewDecoder(file)
			for dec.Decode(&packet) == nil {
				data[packet.Key] = packet.Val
				changeCount++
			}
			file.Close()
		}
	}
}
Esempio n. 3
0
func receiveDecrypt(conn net.Conn) (Message, error) {
	// Our work is:
	// (receive) -> [de-GOB] -> [DECRYPT] -> [de-GOB] -> msg

	// Receive data and de-serialize to get the encrypted message
	encMsg := new([]byte)
	receive := gob.NewDecoder(conn)
	if err := receive.Decode(encMsg); err != nil {
		return Message{}, err
	}

	// Create decrypter and pass it the encrypted message
	r := bytes.NewReader(*encMsg)
	decrypter, err := saltsecret.NewReader(r, conf.Key, saltsecret.DECRYPT, false)
	if err != nil {
		return Message{}, err
	}

	// Read unencrypted serialized message and de-serialize it
	msg := new(Message)
	dec := gob.NewDecoder(decrypter)
	if err = dec.Decode(msg); err != nil {
		return Message{}, err
	}

	return *msg, nil
}
Esempio n. 4
0
func (gobCoder) DecodeValue(r io.Reader, dst reflect.Value) error {
	if _, ok := r.(io.ByteReader); !ok {
		return gob.NewDecoder(byteReader{r}).DecodeValue(dst)
	}

	return gob.NewDecoder(r).DecodeValue(dst)
}
Esempio n. 5
0
func (gobCoder) Decode(r io.Reader, dst interface{}) error {
	if _, ok := r.(io.ByteReader); !ok {
		return gob.NewDecoder(byteReader{r}).Decode(dst)
	}

	return gob.NewDecoder(r).Decode(dst)
}
Esempio n. 6
0
func (cursor *Cursor) GetGo(set_key interface{}, op uint, key, val interface{}) error {
	var err error
	var bset_key []byte
	if set_key != nil {
		var key_buffer bytes.Buffer
		encoder := gob.NewEncoder(&key_buffer)
		err = encoder.Encode(set_key)
		if err != nil {
			return err
		}
		bset_key = key_buffer.Bytes()
	}
	bkey, bval, err := cursor.Get(bset_key, op)
	if err != nil {
		return err
	}
	buf := bytes.NewReader(bkey)
	decoder := gob.NewDecoder(buf)
	err = decoder.Decode(key)
	if err != nil {
		return err
	}
	buf = bytes.NewReader(bval)
	decoder = gob.NewDecoder(buf)
	err = decoder.Decode(val)
	if err != nil {
		return err
	}
	return nil
}
Esempio n. 7
0
// Load returns a *fuzz.fuzz.FuzzReport that corresponds to the passed in revision,
// and the fuzz names associated with the report.
// It returns an error if such a Report does not exist.
func (b *FuzzReportCache) Load(revision string) (*fuzz.FuzzReportTree, []string, error) {
	var report fuzz.FuzzReportTree
	var binaryFuzzNames []string
	loadFunc := func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte(revision))
		if b == nil {
			return fmt.Errorf("Cache for revision %s does not exist", revision)
		}
		c := b.Get(REPORT_KEY)
		if c == nil {
			return fmt.Errorf("Could not find report for revision %s", revision)
		}
		dec := gob.NewDecoder(bytes.NewBuffer(c))
		if err := dec.Decode(&report); err != nil {
			return fmt.Errorf("Could not decode report: %s", err)
		}

		c = b.Get(BINARY_FUZZES_KEY)
		if c == nil {
			return fmt.Errorf("Could not find stored binary fuzzes for revision %s", revision)
		}
		dec = gob.NewDecoder(bytes.NewBuffer(c))
		if err := dec.Decode(&binaryFuzzNames); err != nil {
			return fmt.Errorf("Could not decode binaryFuzzNames: %s", err)
		}
		return nil
	}
	return &report, binaryFuzzNames, b.DB.View(loadFunc)
}
Esempio n. 8
0
func (log *Log) appendEntries(s *Server, prevIndex int64, logEntries []*LogEntry) {
	log.Lock()
	defer log.Unlock()
	if len(log.entries) == 0 {
		log.entries = logEntries
		return
	}
	log.entries = log.entries[:prevIndex-log.startIndex()+1]
	// log.entries = append(log.entries[:(index-log.startIndex())+1], logEntries...)
	for _, entry := range logEntries {
		if entry.GetCommandName() == cOldNewStr {
			nodes := []Node{}
			if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
				logger.Println("decode Cold,new config err: ", err.Error())
			}
			s.config.setState(cOldNew)
			s.config.cNewNode = make(nodeMap)
			for _, node := range nodes {
				s.config.cNewNode[node.id()] = node
			}
		} else if entry.GetCommandName() == cNewStr {
			nodes := []Node{}
			if err := gob.NewDecoder(bytes.NewReader(entry.GetCommand())).Decode(&nodes); err != nil {
				logger.Println("decode Cnew config err: ", err.Error())
			}
			s.config.setState(cOld)
			s.config.cOldNode = makeNodeMap(nodes...)
			s.config.cNewNode = nil
		}
		log.entries = append(log.entries, entry)
	}
}
Esempio n. 9
0
func (zip GobMeddler) PostRead(fieldAddr, scanTarget interface{}) error {
	ptr := scanTarget.(*[]byte)
	if ptr == nil {
		return fmt.Errorf("GobMeddler.PostRead: nil pointer")
	}
	raw := *ptr

	if zip {
		// un-gzip and decode gob
		gzipReader, err := gzip.NewReader(bytes.NewReader(raw))
		if err != nil {
			return fmt.Errorf("Error creating gzip Reader: %v", err)
		}
		defer gzipReader.Close()
		gobDecoder := gob.NewDecoder(gzipReader)
		if err := gobDecoder.Decode(fieldAddr); err != nil {
			return fmt.Errorf("Gob decoder/gzip error: %v", err)
		}
		if err := gzipReader.Close(); err != nil {
			return fmt.Errorf("Closing gzip reader: %v", err)
		}

		return nil
	}

	// decode gob
	gobDecoder := gob.NewDecoder(bytes.NewReader(raw))
	if err := gobDecoder.Decode(fieldAddr); err != nil {
		return fmt.Errorf("Gob decode error: %v", err)
	}

	return nil
}
Esempio n. 10
0
File: db.go Progetto: mbrodala/otto
func (this *DB) getData(bucket *bolt.Bucket) (map[string]int, ipQueue, error) {
	var addrQ ipQueue
	heapRaw := bucket.Get(boltAddrHeapKey)
	if heapRaw == nil {
		addrQ = ipQueue(make([]*ipEntry, 0, 1))
	} else {
		dec := gob.NewDecoder(bytes.NewReader(heapRaw))
		if err := dec.Decode(&addrQ); err != nil {
			return nil, nil, err
		}
	}

	var addrMap map[string]int
	mapRaw := bucket.Get(boltAddrMapKey)
	if mapRaw == nil {
		addrMap = make(map[string]int)
	} else {
		dec := gob.NewDecoder(bytes.NewReader(mapRaw))
		if err := dec.Decode(&addrMap); err != nil {
			return nil, nil, err
		}
	}

	return addrMap, addrQ, nil
}
Esempio n. 11
0
func (self *GlobalState) read(r *os.File) error {
	// skip the version
	reader := bufio.NewReader(r)
	// read the version line
	_, err := reader.ReadString('\n')
	if err != nil {
		return err
	}
	err = gob.NewDecoder(reader).Decode(self)

	// from version 0.7 to 0.8 the type of the Suffix variables
	// changed to uint32. Catch this and convert to a new GlobalState object.
	if err != nil {
		old := &oldGlobalState{}
		r.Seek(int64(0), 0)
		reader := bufio.NewReader(r)
		// read the version line
		_, err := reader.ReadString('\n')
		if err != nil {
			return err
		}
		err = gob.NewDecoder(reader).Decode(old)
		if err != nil {
			return err
		}
		self.CurrentFileOffset = old.CurrentFileOffset
		self.CurrentFileSuffix = uint32(old.CurrentFileSuffix)
		self.LargestRequestNumber = old.LargestRequestNumber
		self.FirstSuffix = uint32(old.FirstSuffix)
		self.ShardLastSequenceNumber = old.ShardLastSequenceNumber
		self.ServerLastRequestNumber = old.ServerLastRequestNumber
	}
	return nil
}
Esempio n. 12
0
func (db *dbImpl) loadDirectory() (*directory, error) {
	dir := &directory{
		Usernames: make(map[uID]string),
		AIs:       make(map[aiID]*aiInfo),
		AIStats:   make(map[aiID]*aiStats),
	}
	err := db.View(func(tx *bolt.Tx) error {
		// Load AIs
		b := tx.Bucket(AIBucket)
		err := b.ForEach(func(k, v []byte) error {
			var a aiInfo
			buf := bytes.NewReader(v)
			dec := gob.NewDecoder(buf)

			if err := dec.Decode(&a); err != nil {
				return err
			}
			dir.AIs[a.ID] = &a
			return nil
		})
		if err != nil {
			return err
		}

		// Load AIStats
		b = tx.Bucket(AIStatsBucket)
		err = b.ForEach(func(k, v []byte) error {
			var a aiStats
			buf := bytes.NewReader(v)
			dec := gob.NewDecoder(buf)

			if err := dec.Decode(&a); err != nil {
				return err
			}
			dir.AIStats[aiID(k)] = &a
			return nil
		})
		if err != nil {
			return err
		}

		// Load usernames
		b = tx.Bucket(UserBucket)
		return b.ForEach(func(k, v []byte) error {
			var u userInfo
			buf := bytes.NewReader(v)
			dec := gob.NewDecoder(buf)

			if err := dec.Decode(&u); err != nil {
				return err
			}
			dir.Usernames[u.ID] = u.Name
			return nil
		})
	})
	return dir, err
}
func NewGobConnection(conn net.Conn) GobConnection {
	if gcn, ok := gobPool.Get().(*gobConnection); ok {
		gcn.rwc = conn
		gcn.enc = gob.NewEncoder(conn)
		gcn.dec = gob.NewDecoder(conn)
		return gcn
	}
	return &gobConnection{rwc: conn, enc: gob.NewEncoder(conn), dec: gob.NewDecoder(conn)}
}
Esempio n. 14
0
File: codec.go Progetto: vgp/gogp
func NewGobCodecs(conn io.ReadWriteCloser) (*gobServerCodec, *gobClientCodec) {
	sbuf := bufio.NewWriter(conn)
	srv := &gobServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(sbuf), sbuf}

	cbuf := bufio.NewWriter(conn)
	cli := &gobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(cbuf), cbuf}

	return srv, cli
}
Esempio n. 15
0
func TestPublisherClosesDuplicateConnections(t *testing.T) {
	log.SetOutput(ioutil.Discard)

	// Choose a port
	port, err := getFreePort()
	if err != nil {
		t.Fatal(err)
	}

	// Start a publisher
	p, err := xfer.NewTCPPublisher(port)
	if err != nil {
		t.Fatal(err)
	}
	defer p.Close()

	// Connect a listener
	conn, err := net.Dial("tcp4", "127.0.0.1"+port)
	if err != nil {
		t.Fatal(err)
	}
	defer conn.Close()
	if err := gob.NewEncoder(conn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
		t.Fatal(err)
	}
	time.Sleep(time.Millisecond)

	// Try to connect the same listener
	dupconn, err := net.Dial("tcp4", "127.0.0.1"+port)
	if err != nil {
		t.Fatal(err)
	}
	// Send handshake
	if err := gob.NewEncoder(dupconn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
		t.Fatal(err)
	}
	defer dupconn.Close()

	// Publish a message
	p.Publish(report.Report{})

	// The first listener should receive it
	var r report.Report
	if err := gob.NewDecoder(conn).Decode(&r); err != nil {
		t.Fatal(err)
	}

	// The duplicate listener should have an error
	if err := gob.NewDecoder(dupconn).Decode(&r); err == nil {
		t.Errorf("expected error, got none")
	} else {
		t.Logf("dupconn got expected error: %v", err)
	}
}
Esempio n. 16
0
func (c *Connection) Init() {
	c.send_buffer = new(bytes.Buffer)
	c.recv_buffer = new(bytes.Buffer)
	c.send_buffer_decoder = gob.NewDecoder(c.send_buffer)
	c.send_buffer_encoder = gob.NewEncoder(c.send_buffer)
	c.recv_buffer_decoder = gob.NewDecoder(c.recv_buffer)
	c.recv_buffer_encoder = gob.NewEncoder(c.recv_buffer)

	c.Quit = make(chan bool)
	c.Outgoing = make(chan interface{}, 1000)
}
Esempio n. 17
0
func bytesToObject(v interface{}, b []byte) error {
	f := flate.NewReader(bytes.NewReader(b))
	defer f.Close()

	err := gob.NewDecoder(f).Decode(v)
	if err != nil {
		// try the old, uncompressed format.
		err = gob.NewDecoder(bytes.NewReader(b)).Decode(v)
	}
	return err
}
Esempio n. 18
0
func (it *Iterator) Next(imm interface{}) (*KeyType, bool) {

	if it == nil {
		return nil, false
	}

	if it.i >= len(it.entries) {
		it.entries = nil
		return nil, false
	}
	entry := it.entries[it.i]

	// immutable
	buff := bytes.NewBuffer(entry.imm.Val)
	dec := gob.NewDecoder(buff)

	if err := dec.Decode(imm); err != nil {
		fmt.Println(err)
		return nil, false
	}
	it.i++

	m, ok := imm.(Mutable)
	if ok {
		mut := m.Mutable()
		if mut == nil {
			return entry.kt, true
		}

		if entry.mut == nil {
			panic("there should be something here!")
			return entry.kt, true
		}

		mutv := reflect.ValueOf(mut)
		if err := validateMutableEntry(mutv); err != nil {
			return nil, false
		}

		// update the v with the new pointer value
		entry.mut.v = mutv

		// mutable
		buff = bytes.NewBuffer(entry.mut.Val)
		dec = gob.NewDecoder(buff)

		if err := dec.Decode(mut); err != nil {
			fmt.Println(err)
			return nil, false
		}
	}
	return entry.kt, true
}
Esempio n. 19
0
func (d *FileTreeCache) AddEntry(entry protocol.FileInfo, peer protocol.DeviceID) {
	d.db.Update(func(tx *bolt.Tx) error {
		eb := tx.Bucket(d.folderBucketKey).Bucket(entriesBucket)

		/* save entry */
		var buf bytes.Buffer
		enc := gob.NewEncoder(&buf)
		enc.Encode(entry)
		eb.Put([]byte(entry.Name), buf.Bytes()) // TODO handle error?

		/* add peer */
		edb := tx.Bucket(d.folderBucketKey).Bucket(entryDevicesBucket)
		v := edb.Get([]byte(entry.Name))
		var devices map[string]bool
		if v == nil {
			devices = make(map[string]bool)
		} else {
			rbuf := bytes.NewBuffer(v)
			dec := gob.NewDecoder(rbuf)
			dec.Decode(&devices)
		}
		devices[peer.String()] = true
		var dbuf bytes.Buffer
		enc = gob.NewEncoder(&dbuf)
		enc.Encode(devices)
		edb.Put([]byte(entry.Name), dbuf.Bytes())

		/* add child lookup */
		dir := path.Dir(entry.Name)
		clb := tx.Bucket(d.folderBucketKey).Bucket(childLookupBucket)
		v = clb.Get([]byte(dir))
		if debug {
			l.Debugln("Adding child", entry.Name, "for dir", dir)
		}

		var children map[string]bool
		if v == nil {
			children = make(map[string]bool)
		} else {
			rbuf := bytes.NewBuffer(v)
			dec := gob.NewDecoder(rbuf)
			dec.Decode(&children)
		}
		children[entry.Name] = true

		var cbuf bytes.Buffer
		enc = gob.NewEncoder(&cbuf)
		enc.Encode(children)
		clb.Put([]byte(dir), cbuf.Bytes())

		return nil
	})
}
Esempio n. 20
0
// NewConsumer return a pointer of Consumer instance,
// compression is a optional, it can be true if need zlib compression
// else false.
func NewConsumer(conn net.Conn, compression bool) (*Consumer, error) {
	if !compression {
		dec := gob.NewDecoder(conn)
		return &Consumer{conn, dec, nil}, nil
	}

	compR, err := zlib.NewReader(conn)
	if err != nil {
		return nil, err
	}
	dec := gob.NewDecoder(compR)
	return &Consumer{conn, dec, compR}, nil
}
Esempio n. 21
0
// Load the frozen data store from disk.
func (ds *DataStore) Load(dsFile string) error {
	if dsFile == "" {
		err := fmt.Errorf("Yikes! Cannot load data store from disk because no file was specified.")
		return err
	}

	fp, err := os.Open(dsFile)
	if err != nil {
		// It's fine for the file not to exist on startup
		if os.IsNotExist(err) {
			return nil
		}
		return err
	}
	zfp, zerr := zlib.NewReader(fp)
	if zerr != nil {
		fp.Close()
		return zerr
	}
	dec := gob.NewDecoder(zfp)
	ds.m.Lock()
	defer ds.m.Unlock()
	fstore := new(dsFileStore)
	err = dec.Decode(&fstore)
	zfp.Close()
	if err != nil {
		fp.Close()
		log.Printf("error at fstore")
		return err
	}

	dscache := bytes.NewBuffer(fstore.Cache)
	objList := bytes.NewBuffer(fstore.ObjList)

	err = ds.dsc.Load(dscache)
	if err != nil {
		log.Println("error at dscache")
		fp.Close()
		return err
	}
	dec = gob.NewDecoder(objList)
	err = dec.Decode(&ds.objList)
	if err != nil {
		log.Println("error at objList")
		fp.Close()
		return err
	}
	return fp.Close()
}
Esempio n. 22
0
func (c *Client) response() (*Response, error) {
	log.ProtoLevel().Tag("client", "discover").Printf("Waiting response...")
	buf := make([]byte, c.BufSize)
	err := c.conn.SetDeadline(time.Now().Add(c.Deadline))
	if err != nil {
		return nil, e.New(err)
	}
	n, addr, err := c.conn.ReadFromUDP(buf)
	if err != nil {
		return nil, e.New(err)
	}
	log.ProtoLevel().Tag("client", "discover").Printf("Response from %v with size %v.", addr, n)
	err = c.conn.SetDeadline(time.Time{})
	if err != nil {
		return nil, e.New(err)
	}

	dec := gob.NewDecoder(bytes.NewReader(buf[:n]))
	var msg Msg
	err = dec.Decode(&msg)
	if err != nil {
		return nil, e.Push(err, e.New("error decoding response"))
	}

	if msg.Err != nil {
		return nil, e.Forward(msg.Err)
	}

	if msg.From != c.ServerName {
		return nil, e.New("wrong server name")
	}
	if msg.To != c.Name {
		return nil, e.New("message isn't for me")
	}

	buf, err = msg.Message(c.ServerKey, c.PrivateKey)
	if err != nil {
		return nil, e.Push(err, e.New("error decrypting response"))
	}

	dec = gob.NewDecoder(bytes.NewReader(buf))
	var resp Response
	err = dec.Decode(&resp)
	if err != nil {
		return nil, e.Push(err, e.New("error decoding response"))
	}
	return &resp, nil
}
Esempio n. 23
0
func (conn *LocalConnection) run(actionChan <-chan ConnectionAction, finished chan<- struct{}, acceptNewPeer bool) {
	var err error // important to use this var and not create another one with 'err :='
	defer func() { conn.shutdown(err) }()
	defer close(finished)

	tcpConn := conn.TCPConn
	tcpConn.SetLinger(0)
	enc := gob.NewEncoder(tcpConn)
	dec := gob.NewDecoder(tcpConn)

	if err = conn.handshake(enc, dec, acceptNewPeer); err != nil {
		return
	}
	conn.Log("completed handshake")

	// The ordering of the following is very important. [1]

	if conn.remoteUDPAddr != nil {
		if err = conn.ensureForwarders(); err != nil {
			return
		}
	}
	if err = conn.Router.Ourself.AddConnection(conn); err != nil {
		return
	}
	if err = conn.initHeartbeats(); err != nil {
		return
	}
	go conn.receiveTCP(dec)
	err = conn.actorLoop(actionChan)
}
Esempio n. 24
0
//This actually does the deserializing of a Gob encoded classifier
func NewClassifierFromReader(r io.Reader) (c *Classifier, err error) {
	dec := gob.NewDecoder(r)
	w := new(serializableClassifier)
	err = dec.Decode(w)

	return &Classifier{w.Classes, w.Learned, w.Seen, w.Datas}, err
}
Esempio n. 25
0
func makePkt(buf []byte) pkt {
	var p pkt
	if err := gob.NewDecoder(bytes.NewBuffer(buf)).Decode(&p); err != nil {
		panic(err)
	}
	return p
}
Esempio n. 26
0
func (c *Client) handle(conn net.Conn, ws *WorkServer) {
	defer conn.Close()

	reader := gob.NewDecoder(conn)

	c.ClientInfo = new(Hyades.ClientInfo)
	err := reader.Decode(c.ClientInfo)
	if err != nil {
		c.FrameWorkError(err)
		return
	}
	c.Owner.Stats.Announced(c.ClientInfo)

	log.Println(c.ClientInfo.ComputerName, " Connected")

	c.Owner.Stats.Connected()
	defer c.Owner.Stats.Disconnected(c.ClientInfo)

	lc := logicsocket.Wrap(conn)

	workConn := lc.NewConnection(WORK)
	go c.ServiceWork(workConn)

	heartbeatConn := lc.NewConnection(HEARTBEAT)
	c.ServiceHeartBeat(heartbeatConn)

}
func (mfsm *MyFsm) Restore(inp io.ReadCloser) error {
	defer inp.Close()
	fmt.Printf("Restore......................\n")
	mfsm.mutex.Lock()
	defer mfsm.mutex.Unlock()
	var buffer bytes.Buffer
	readdata := make([]byte, 1024)
	for {
		n, err := inp.Read(readdata)
		if err != nil {
			panic(err)
		}
		if n < 1024 {
			if n > 0 {
				lastbytes := make([]byte, n)
				copy(readdata, lastbytes)
				buffer.Write(lastbytes)
			}
			break
		} else {
			buffer.Write(readdata)
		}
	}
	dec := gob.NewDecoder(&buffer)
	err := dec.Decode(&mfsm.data)
	errorOnExit(err)
	return nil
}
Esempio n. 28
0
// Read reads the index from r into x; x must not be nil.
// If r does not also implement io.ByteReader, it will be wrapped in a bufio.Reader.
func (x *Index) Read(r io.Reader) error {
	// We use the ability to read bytes as a plausible surrogate for buffering.
	if _, ok := r.(io.ByteReader); !ok {
		r = bufio.NewReader(r)
	}
	var fx fileIndex
	if err := fx.Read(r); err != nil {
		return err
	}
	x.words = fx.Words
	x.alts = fx.Alts
	x.snippets = fx.Snippets
	if fx.Fulltext {
		x.fset = token.NewFileSet()
		decode := func(x interface{}) error {
			return gob.NewDecoder(r).Decode(x)
		}
		if err := x.fset.Read(decode); err != nil {
			return err
		}
		x.suffixes = new(suffixarray.Index)
		if err := x.suffixes.Read(r); err != nil {
			return err
		}
	}
	return nil
}
Esempio n. 29
0
func (ms *ManaSource) GobDecode(data []byte) error {
	base.Log().Printf("GobDecode")
	dec := gob.NewDecoder(bytes.NewBuffer(data))
	err := dec.Decode(&ms.options)
	var d1, d2 int
	if err == nil {
		var d uint32
		err = dec.Decode(&d)
		ms.thinks = int(d)
		base.Log().Printf("Decoded %d", d)
	}
	if err == nil {
		var d uint32
		err = dec.Decode(&d)
		d1 = int(d)
		base.Log().Printf("Decoded %d", d1)
	}
	if err == nil {
		var d uint32
		err = dec.Decode(&d)
		d2 = int(d)
		base.Log().Printf("Decoded %d", d2)
	}
	if err == nil {
		err = dec.Decode(&ms.rawNodes)
	}
	if err == nil {
		ms.nodes = make([][]node, d1)
		for i := range ms.nodes {
			ms.nodes[i] = ms.rawNodes[i*d2 : (i+1)*d2]
		}
	}
	return err
}
Esempio n. 30
0
//深拷贝一个对象
func DeepCopy(dst, src interface{}) error {
	var buf bytes.Buffer
	if err := gob.NewEncoder(&buf).Encode(src); err != nil {
		return err
	}
	return gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(dst)
}