本文整理汇总了Golang中gob.NewDecoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDecoder函数的具体用法?Golang NewDecoder怎么用?Golang NewDecoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDecoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Refresh
func (store *RemoteStore) Refresh() (err os.Error) {
store.root = nil
store.index = nil
rootFile, err := store.FOpen(filepath.Join("/", r9p.ROOT_FILE), p.OREAD)
if err != nil {
return err
}
buffer := bytes.NewBuffer([]byte{})
chunk := make([]byte, fs.BLOCKSIZE)
for {
n, err := rootFile.Read(chunk)
if err != nil && err != os.EOF {
return err
}
if n == 0 {
err = nil
break
}
buffer.Write(chunk[0:n])
}
decoder := gob.NewDecoder(buffer)
store.root = &fs.Dir{}
err = decoder.Decode(store.root)
return err
}
示例2: Open
func Open(root string) (*Stash, os.Error) {
// attempt to open the index in the specified directory
if meta, err := os.Open(root + metaFile); err == nil {
// attempt to read in the index
s, dec := new(Stash), gob.NewDecoder(meta)
if err = dec.Decode(&s.meta); err == nil {
// attempt to open the data file
if s.store, err = os.OpenFile(root+dataFile, os.O_RDWR, 0640); err == nil {
s.init(root)
s.idx = meta
go s.sync()
} else {
s = nil
}
} else {
s = nil
}
if s == nil {
meta.Close()
}
return s, err
} else {
return nil, err
}
panic("Dead code reached!")
}
示例3: TestRatGobEncoding
func TestRatGobEncoding(t *testing.T) {
var medium bytes.Buffer
enc := gob.NewEncoder(&medium)
dec := gob.NewDecoder(&medium)
for i, test := range gobEncodingTests {
for j := 0; j < 4; j++ {
medium.Reset() // empty buffer for each test case (in case of failures)
stest := test
if j&1 != 0 {
// negative numbers
stest = "-" + test
}
if j%2 != 0 {
// fractions
stest = stest + "." + test
}
var tx Rat
tx.SetString(stest)
if err := enc.Encode(&tx); err != nil {
t.Errorf("#%d%c: encoding failed: %s", i, 'a'+j, err)
}
var rx Rat
if err := dec.Decode(&rx); err != nil {
t.Errorf("#%d%c: decoding failed: %s", i, 'a'+j, err)
}
if rx.Cmp(&tx) != 0 {
t.Errorf("#%d%c: transmission failed: got %s want %s", i, 'a'+j, &rx, &tx)
}
}
}
}
示例4: Get
func (s *Stash) Get(key string) (interface{}, os.Error) {
defer s.Flush()
// check the hot cache
if e := s.cache.Get(key); e != nil {
return e.Value, nil
}
// resort to the file
s.sMutex.Lock()
defer s.sMutex.Unlock()
if _, err := s.store.Seek(s.meta.Index.Search(key).Offset, 0); err == nil {
dec, tmp := gob.NewDecoder(io.LimitReader(s.store, s.meta.Index.PageSize)), &record{}
// place every key/value pair into the hot cache if it is missing
s.cMutex.Lock()
defer s.cMutex.Unlock()
for err = dec.Decode(tmp); err == nil; err = dec.Decode(tmp) {
if e := s.cache.check(tmp.Key); e == nil {
s.cache.Add(&entry{record{tmp.Key, tmp.Value}, time{0, 0}, false, nil})
}
}
// check to see if it got picked up out of the file
if e := s.cache.Get(key); e != nil {
return e.Value, nil
}
} else {
return nil, err
}
return nil, nil // couldn't find it
}
示例5: runFunc
func runFunc(w http.ResponseWriter, req *http.Request) {
c := appengine.NewContext(req)
defer req.Body.Close()
var inv invocation
if err := gob.NewDecoder(req.Body).Decode(&inv); err != nil {
c.Errorf("delay: failed decoding task payload: %v", err)
c.Warningf("delay: dropping task")
return
}
f := funcs[inv.Key]
if f == nil {
c.Errorf("delay: no func with key %q found", inv.Key)
c.Warningf("delay: dropping task")
return
}
// TODO: This is broken for variadic functions.
in := make([]reflect.Value, f.fv.Type().NumIn())
in[0] = reflect.ValueOf(c)
for i := 1; i < len(in); i++ {
in[i] = reflect.ValueOf(inv.Args[i-1])
}
f.fv.Call(in)
}
示例6: NewFlatStatCache
func NewFlatStatCache() *FlatStatCache {
filename := filepath.Join(osutil.CacheDir(), "camput.statcache")
fc := &FlatStatCache{
filename: filename,
m: make(map[string]fileInfoPutRes),
dirty: make(map[string]fileInfoPutRes),
}
if f, err := os.Open(filename); err == nil {
defer f.Close()
d := gob.NewDecoder(f)
for {
var key string
var val fileInfoPutRes
if d.Decode(&key) != nil || d.Decode(&val) != nil {
break
}
val.Pr.Skipped = true
fc.m[key] = val
log.Printf("Read %q: %v", key, val)
}
log.Printf("Flatcache read %d entries from %s", len(fc.m), filename)
}
return fc
}
示例7: runFunc
func runFunc(c appengine.Context, w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
var inv invocation
if err := gob.NewDecoder(req.Body).Decode(&inv); err != nil {
c.Errorf("delay: failed decoding task payload: %v", err)
c.Warningf("delay: dropping task")
return
}
f := funcs[inv.Key]
if f == nil {
c.Errorf("delay: no func with key %q found", inv.Key)
c.Warningf("delay: dropping task")
return
}
// TODO: This is broken for variadic functions.
ft := f.fv.Type()
in := make([]reflect.Value, ft.NumIn())
in[0] = reflect.ValueOf(c)
for i := 1; i < len(in); i++ {
in[i] = reflect.ValueOf(inv.Args[i-1])
}
out := f.fv.Call(in)
if n := ft.NumOut(); n > 0 && ft.Out(n-1) == osErrorType {
if errv := out[n-1]; !errv.IsNil() {
c.Errorf("delay: func failed (will retry): %v", errv.Interface())
w.WriteHeader(http.StatusInternalServerError)
return
}
}
}
示例8: RetrieveActDetails
func RetrieveActDetails(c appengine.Context, actId int) (res *ActDetail, err os.Error) {
var d DSActDetail
if itm, err := memcache.Get(c, "actId__"+strconv.Itoa(actId)); err != nil && err != memcache.ErrCacheMiss {
return nil, err
} else if err == nil {
// Cache hit
buf := bytes.NewBuffer(itm.Value)
dec := gob.NewDecoder(buf)
dec.Decode(&d)
} else {
// Cache miss
key := datastore.NewKey(c, "DSActDetail", "", int64(actId), nil)
if err := datastore.Get(c, key, &d); err == datastore.ErrNoSuchEntity {
return &ActDetail{ActId: actId}, nil
} else if err != nil {
return nil, err
}
buf := bytes.NewBufferString("")
enc := gob.NewEncoder(buf)
enc.Encode(d)
itm := &memcache.Item{
Key: "actId__" + strconv.Itoa(actId),
Value: buf.Bytes(),
}
err = memcache.Set(c, itm)
c.Debugf("Request cache to memcache")
}
return d.fromDS(), nil
}
示例9: decodeGob
func decodeGob(value string) (result map[string]interface{}) {
buffer := bytes.NewBufferString(value)
decoder := gob.NewDecoder(buffer)
result = make(map[string]interface{})
decoder.Decode(&result)
return result
}
示例10: SeekToLatestFile
func (fr *FileReader) SeekToLatestFile() os.Error {
fr.lk.Lock()
defer fr.lk.Unlock()
// Find the file
dir, pre := path.Split(fr.prefix)
if dir == "" {
dir = "."
}
d, err := os.Open(dir, os.O_RDONLY, 0)
if err != nil {
return err
}
files, err := d.Readdirnames(-1)
if err != nil {
return err
}
d.Close()
files = filterAndSort(files, pre)
if len(files) == 0 {
return os.EOF
}
// Open it
file := path.Join(dir, files[len(files)-1])
fr.file, err = os.Open(file, os.O_RDONLY, 0)
if err != nil {
fr.file, fr.dec = nil, nil
return err
}
fr.dec = gob.NewDecoder(fr.file)
return nil
}
示例11: recoverLatestDecode
func recoverLatestDecode(fprefix string, e interface{}) os.Error {
r, err := MakeFileReader(fprefix)
if err != nil {
return err
}
err = r.SeekToLatestFile()
if err != nil {
return err
}
dec := gob.NewDecoder(r)
// TODO: Deep copy the latest successful read, so the last
// unsuccessful read does not garble the good data
ok := false
for {
err = dec.Decode(e)
if err != nil {
break
}
ok = true
}
if ok {
return nil
}
return os.EOF
}
示例12: decodePassword
func decodePassword(b []byte) *passwordhash.PasswordHash {
hash := new(passwordhash.PasswordHash)
err := gob.NewDecoder(bytes.NewBuffer(b)).Decode(hash)
if err == nil {
return hash
}
panic(err)
}
示例13: GobDecode
func (k *Key) GobDecode(buf []byte) error {
gk := new(gobKey)
if err := gob.NewDecoder(bytes.NewBuffer(buf)).Decode(gk); err != nil {
return err
}
*k = *gobKeyToKey(gk)
return nil
}
示例14: deserialize
// deserialize decodes a session value using gob.
func deserialize(value []byte) (SessionData, error) {
var session SessionData
b := bytes.NewBuffer(value)
d := gob.NewDecoder(b)
if err := d.Decode(&session); err != nil {
return nil, err
}
return session, nil
}
示例15: NewClient
// NewClient returns a new Client to handle requests to the
// set of services at the other end of the connection.
func NewClient(conn io.ReadWriteCloser) *Client {
client := new(Client);
client.conn = conn;
client.enc = gob.NewEncoder(conn);
client.dec = gob.NewDecoder(conn);
client.pending = make(map[uint64]*Call);
go client.input();
return client;
}