当前位置: 首页>>代码示例>>Golang>>正文


Golang zlib.NewWriter函数代码示例

本文整理汇总了Golang中compress/zlib.NewWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriter函数的具体用法?Golang NewWriter怎么用?Golang NewWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewWriter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: encodeQuery

func encodeQuery(query []byte) string {
	var compressed_query bytes.Buffer
	w := zlib.NewWriter(&compressed_query)
	w.Write(query)
	w.Close()
	return base64.URLEncoding.EncodeToString(compressed_query.Bytes())
}
开发者ID:jstanley0,项目名称:stripe-ctf-3,代码行数:7,代码来源:server.go

示例2: compress

func compress(data []byte) []byte {
	var compressedData bytes.Buffer
	writer := zlib.NewWriter(&compressedData)
	writer.Write(data)
	writer.Close()
	return compressedData.Bytes()
}
开发者ID:adamkruger,项目名称:psiphon-tunnel-core,代码行数:7,代码来源:osl.go

示例3: SetActionPlan

func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) (err error) {
	if len(ats.ActionTimings) == 0 {
		// delete the key
		err = rs.db.Cmd("DEL", utils.ACTION_PLAN_PREFIX+key).Err
		cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
		return err
	}
	if !overwrite {
		// get existing action plan to merge the account ids
		if existingAts, _ := rs.GetActionPlan(key, true); existingAts != nil {
			if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
				ats.AccountIDs = make(utils.StringMap)
			}
			for accID := range existingAts.AccountIDs {
				ats.AccountIDs[accID] = true
			}
		}
	}

	result, err := rs.ms.Marshal(ats)
	if err != nil {
		return err
	}
	var b bytes.Buffer
	w := zlib.NewWriter(&b)
	w.Write(result)
	w.Close()
	return rs.db.Cmd("SET", utils.ACTION_PLAN_PREFIX+key, b.Bytes()).Err
}
开发者ID:bhepp,项目名称:cgrates,代码行数:29,代码来源:storage_redis.go

示例4: main

func main() {
	app := cli.NewApp()
	app.Name = "zlib"
	app.Usage = "A command-line tool for using the zlib compression algorithm."
	app.Action = func(c *cli.Context) {
		var reader io.Reader = os.Stdin
		if c.Bool("decompress") {
			compressorReadCloser, err := zlib.NewReader(reader)
			if err != nil {
				exit(err.Error(), 1)
			}
			if _, err := io.Copy(os.Stdout, compressorReadCloser); err != nil {
				exit(err.Error(), 1)
			}
			compressorReadCloser.Close()
		} else {
			var writer io.Writer = os.Stdout
			compressorWriteCloser := zlib.NewWriter(writer)
			if _, err := io.Copy(compressorWriteCloser, reader); err != nil {
				exit(err.Error(), 1)
			}
			compressorWriteCloser.Close()
		}
	}
	app.Flags = []cli.Flag{
		cli.BoolFlag{
			Name:  "d, decompress",
			Usage: "Decompresses the input instead of compressing the output.",
		},
	}
	app.Run(os.Args)
}
开发者ID:kevin-cantwell,项目名称:zlib,代码行数:32,代码来源:main.go

示例5: custom

func custom(log, cors, validate bool, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		addr := r.RemoteAddr
		if ip, found := header(r, "X-Forwarded-For"); found {
			addr = ip
		}
		// compress settings
		ioWriter := w.(io.Writer)
		for _, val := range misc.ParseCsvLine(r.Header.Get("Accept-Encoding")) {
			if val == "gzip" {
				w.Header().Set("Content-Encoding", "gzip")
				g := gzip.NewWriter(w)
				defer g.Close()
				ioWriter = g
				break
			}
			if val == "deflate" {
				w.Header().Set("Content-Encoding", "deflate")
				z := zlib.NewWriter(w)
				defer z.Close()
				ioWriter = z
				break
			}
		}
		writer := &customResponseWriter{Writer: ioWriter, ResponseWriter: w, status: http.StatusOK}

		// route to the controllers
		f(writer, r)

		// access log
		if log && cfg.AccessLog {
			logs.Info.Printf("%s %s %s %s", addr, strconv.Itoa(writer.status), r.Method, r.URL)
		}
	}
}
开发者ID:pottava,项目名称:golang-microservices,代码行数:35,代码来源:http.go

示例6: serializeChunkData

// serializeChunkData produces the compressed chunk NBT data.
func serializeChunkData(w *nbtChunkWriter) (chunkData []byte, err os.Error) {
	// Reserve room for the chunk data header at the start.
	buffer := bytes.NewBuffer(make([]byte, chunkDataHeaderSize, chunkDataGuessSize))

	if zlibWriter, err := zlib.NewWriter(buffer); err != nil {
		return nil, err
	} else {
		if err = nbt.Write(zlibWriter, w.RootTag()); err != nil {
			zlibWriter.Close()
			return nil, err
		}
		if err = zlibWriter.Close(); err != nil {
			return nil, err
		}
	}
	chunkData = buffer.Bytes()

	// Write chunk data header
	header := chunkDataHeader{
		DataSize: uint32(len(chunkData)) - chunkDataHeaderSize,
		Version:  chunkCompressionZlib,
	}
	buffer = bytes.NewBuffer(chunkData[:0])
	if err = binary.Write(buffer, binary.BigEndian, header); err != nil {
		return nil, err
	}

	return chunkData, nil
}
开发者ID:huwenshu,项目名称:chunkymonkey,代码行数:30,代码来源:beta_region.go

示例7: custom

func custom(log, cors, validate bool, f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {

		// compress settings
		ioWriter := w.(io.Writer)
		for _, val := range misc.ParseCsvLine(r.Header.Get("Accept-Encoding")) {
			if val == "gzip" {
				w.Header().Set("Content-Encoding", "gzip")
				g := gzip.NewWriter(w)
				defer g.Close()
				ioWriter = g
				break
			}
			if val == "deflate" {
				w.Header().Set("Content-Encoding", "deflate")
				z := zlib.NewWriter(w)
				defer z.Close()
				ioWriter = z
				break
			}
		}
		writer := &customResponseWriter{Writer: ioWriter, ResponseWriter: w, status: 200}

		// route to the controllers
		f(writer, r)
	}
}
开发者ID:pottava,项目名称:docker-webui,代码行数:27,代码来源:http.go

示例8: Marshal

func Marshal(compression Compression, out io.Writer, v interface{}) (err error) {
	defer func() {
		if r := recover(); r != nil {
			if s, ok := r.(string); ok {
				err = fmt.Errorf(s)
			} else {
				err = r.(error)
			}
		}
	}()

	if out == nil {
		panic(fmt.Errorf("nbt: Output stream is nil"))
	}

	switch compression {
	case Uncompressed:
		break
	case GZip:
		w := gzip.NewWriter(out)
		defer w.Close()
		out = w
	case ZLib:
		w := zlib.NewWriter(out)
		defer w.Close()
		out = w
	default:
		panic(fmt.Errorf("nbt: Unknown compression type: %d", compression))
	}

	writeRootTag(out, reflect.ValueOf(v))

	return
}
开发者ID:Nightgunner5,项目名称:go.nbt,代码行数:34,代码来源:encode.go

示例9: committer

func (f *ZlibFilter) committer(fr FilterRunner, h PluginHelper, wg *sync.WaitGroup) {
	initBatch := make([]byte, 0, 10000)
	f.backChan <- initBatch
	var (
		tag string
		//ok bool
		outBatch []byte
	)
	tag = f.ZlibTag

	for outBatch = range f.batchChan {
		pack, e := h.PipelinePack(f.msgLoopCount)
		if e != nil {
			fr.LogError(e)
			break
		}
		var b bytes.Buffer
		w := zlib.NewWriter(&b)
		w.Write(outBatch)
		w.Close()

		tagField, _ := message.NewField("ZlibTag", tag, "")
		pack.Message.AddField(tagField)
		pack.Message.SetUuid(uuid.NewRandom())
		pack.Message.SetPayload(b.String())
		fr.Inject(pack)

		outBatch = outBatch[:0]
		f.backChan <- outBatch
	}
	wg.Done()
}
开发者ID:huhongbo,项目名称:heka-zlib,代码行数:32,代码来源:zlib_filter.go

示例10: SetActionPlan

func (ms *MongoStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool) error {
	// clean dots from account ids map
	if len(ats.ActionTimings) == 0 {
		cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
		err := ms.db.C(colApl).Remove(bson.M{"key": key})
		if err != mgo.ErrNotFound {
			return err
		}
		return nil
	}
	if !overwrite {
		// get existing action plan to merge the account ids
		if existingAts, _ := ms.GetActionPlan(key, true); existingAts != nil {
			if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
				ats.AccountIDs = make(utils.StringMap)
			}
			for accID := range existingAts.AccountIDs {
				ats.AccountIDs[accID] = true
			}
		}
	}
	result, err := ms.ms.Marshal(ats)
	if err != nil {
		return err
	}
	var b bytes.Buffer
	w := zlib.NewWriter(&b)
	w.Write(result)
	w.Close()
	_, err = ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct {
		Key   string
		Value []byte
	}{Key: key, Value: b.Bytes()})
	return err
}
开发者ID:bhepp,项目名称:cgrates,代码行数:35,代码来源:storage_mongo_datadb.go

示例11: Compress

func Compress(data []byte) bytes.Buffer {
	var b bytes.Buffer
	w := zlib.NewWriter(&b)
	w.Write(data)
	w.Close()
	return b
}
开发者ID:pageben,项目名称:assemble-web-chat,代码行数:7,代码来源:data.go

示例12: Close

func (f *file) Close() error {
	if !f.closed {
		f.f.Lock()
		defer f.f.Unlock()
		if !f.closed {
			if f.f.Mode&ModeCompress != 0 {
				var buf bytes.Buffer
				zw := zlib.NewWriter(&buf)
				if _, err := zw.Write(f.data); err != nil {
					return err
				}
				if err := zw.Close(); err != nil {
					return err
				}
				if buf.Len() < len(f.data) {
					f.f.Data = buf.Bytes()
				} else {
					f.f.Mode &= ^ModeCompress
					f.f.Data = f.data
				}
			} else {
				f.f.Data = f.data
			}
			f.closed = true
		}
	}
	return nil
}
开发者ID:knutsel,项目名称:httpcache,代码行数:28,代码来源:file_util.go

示例13: compress

func compress(data []byte) []byte {
	var b bytes.Buffer
	w := zlib.NewWriter(&b)
	w.Write(data)
	w.Close()
	return b.Bytes()
}
开发者ID:Cristofori,项目名称:kmud,代码行数:7,代码来源:utils.go

示例14: SetActionPlan

func (rs *RedisStorage) SetActionPlan(key string, ats *ActionPlan, overwrite bool, transactionID string) (err error) {
	cCommit := cacheCommit(transactionID)
	if len(ats.ActionTimings) == 0 {
		// delete the key
		err = rs.Cmd("DEL", utils.ACTION_PLAN_PREFIX+key).Err
		cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
		return err
	}
	if !overwrite {
		// get existing action plan to merge the account ids
		if existingAts, _ := rs.GetActionPlan(key, true, transactionID); existingAts != nil {
			if ats.AccountIDs == nil && len(existingAts.AccountIDs) > 0 {
				ats.AccountIDs = make(utils.StringMap)
			}
			for accID := range existingAts.AccountIDs {
				ats.AccountIDs[accID] = true
			}
		}
		// do not keep this in cache (will be obsolete)
		cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
	}
	result, err := rs.ms.Marshal(ats)
	if err != nil {
		return err
	}
	var b bytes.Buffer
	w := zlib.NewWriter(&b)
	w.Write(result)
	w.Close()
	err = rs.Cmd("SET", utils.ACTION_PLAN_PREFIX+key, b.Bytes()).Err
	cache.RemKey(utils.ACTION_PLAN_PREFIX+key, cCommit, transactionID)
	return
}
开发者ID:rinor,项目名称:cgrates,代码行数:33,代码来源:storage_redis.go

示例15: save

func save(r redis.AsyncClient, key string, obj interface{}, w http.ResponseWriter) {
	var b bytes.Buffer

	z := zlib.NewWriter(&b)
	defer z.Close()

	je := json.NewEncoder(z)

	err := je.Encode(obj)
	if err != nil {
		log.Fatal("Failed to json Encode with error: ", err)
	}
	z.Flush()

	f, rerr := r.Set(key, b.Bytes())
	if rerr != nil {
		panic(rerr)
	}
	_, rerr, timeout := f.TryGet(50000000000)
	if rerr != nil {
		panic(rerr)
	}
	if timeout {
		savetimeout++
		log.Println("save timeout! count: ", savetimeout)
		fmt.Fprintf(w, "Save failed for %s", key)
	}
}
开发者ID:rmoorman,项目名称:web-bench,代码行数:28,代码来源:app.go


注:本文中的compress/zlib.NewWriter函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。