• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang flate.NewWriter函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: Deflate

// Deflate utility that compresses a string using the flate algo
func Deflate(inflated string) []byte {
	var b bytes.Buffer
	w, _ := flate.NewWriter(&b, -1)
	w.Write([]byte(inflated))
	w.Close()
	return b.Bytes()
}
开发者ID:wayf-dk,项目名称:gosaml,代码行数:8,代码来源:gosaml.go


示例2: Body

func (output *BeegoOutput) Body(content []byte) {
	output_writer := output.res.(io.Writer)
	if output.EnableGzip == true && output.Context.Input.Header("Accept-Encoding") != "" {
		splitted := strings.SplitN(output.Context.Input.Header("Accept-Encoding"), ",", -1)
		encodings := make([]string, len(splitted))

		for i, val := range splitted {
			encodings[i] = strings.TrimSpace(val)
		}
		for _, val := range encodings {
			if val == "gzip" {
				output.Header("Content-Encoding", "gzip")
				output_writer, _ = gzip.NewWriterLevel(output.res, gzip.BestSpeed)

				break
			} else if val == "deflate" {
				output.Header("Content-Encoding", "deflate")
				output_writer, _ = flate.NewWriter(output.res, flate.BestSpeed)
				break
			}
		}
	} else {
		output.Header("Content-Length", strconv.Itoa(len(content)))
	}
	output_writer.Write(content)
	switch output_writer.(type) {
	case *gzip.Writer:
		output_writer.(*gzip.Writer).Close()
	case *flate.Writer:
		output_writer.(*flate.Writer).Close()
	}
}
开发者ID:Neeke,项目名称:beego,代码行数:32,代码来源:output.go


示例3: ExampleWriter_RegisterCompressor

func ExampleWriter_RegisterCompressor() {
	// Override the default Deflate compressor with a higher compression
	// level.

	// Create a buffer to write our archive to.
	buf := new(bytes.Buffer)

	// Create a new zip archive.
	w := zip.NewWriter(buf)

	var fw *flate.Writer

	// Register the deflator.
	w.RegisterCompressor(zip.Deflate, func(out io.Writer) (io.WriteCloser, error) {
		var err error
		if fw == nil {
			// Creating a flate compressor for every file is
			// expensive, create one and reuse it.
			fw, err = flate.NewWriter(out, flate.BestCompression)
		} else {
			fw.Reset(out)
		}
		return fw, err
	})

	// Proceed to add files to w.
}
开发者ID:rogpeppe,项目名称:zip,代码行数:27,代码来源:example_test.go


示例4: compressor

func compressor(fw *Writer, toCompress <-chan *writerBlock, toWrite chan<- *writerBlock) {
	switch fw.CompressionCodec {
	case CompressionDeflate:
		bb := new(bytes.Buffer)
		comp, _ := flate.NewWriter(bb, flate.DefaultCompression)
		for block := range toCompress {
			_, block.err = comp.Write(block.encoded.Bytes())
			block.err = comp.Close()
			if block.err == nil {
				block.compressed = bb.Bytes()
				toWrite <- block
			}
			bb = new(bytes.Buffer)
			comp.Reset(bb)
		}
	case CompressionNull:
		for block := range toCompress {
			block.compressed = block.encoded.Bytes()
			toWrite <- block
		}
	case CompressionSnappy:
		for block := range toCompress {
			block.compressed = snappy.Encode(block.compressed, block.encoded.Bytes())
			toWrite <- block
		}
	}
	close(toWrite)
}
开发者ID:hhkbp2,项目名称:goavro,代码行数:28,代码来源:ocf_writer.go


示例5: Save

// Save persists a Siegfried struct to disk (path)
func (s *Siegfried) Save(path string) error {
	ls := persist.NewLoadSaver(nil)
	ls.SaveTime(s.C)
	s.em.Save(ls)
	s.cm.Save(ls)
	s.bm.Save(ls)
	s.tm.Save(ls)
	ls.SaveTinyUInt(len(s.ids))
	for _, i := range s.ids {
		i.Save(ls)
	}
	if ls.Err != nil {
		return ls.Err
	}
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.Write(append(config.Magic(), byte(config.Version()[0]), byte(config.Version()[1])))
	if err != nil {
		return err
	}
	z, err := flate.NewWriter(f, 1)
	if err != nil {
		return err
	}
	_, err = z.Write(ls.Bytes())
	z.Close()
	return err
}
开发者ID:TidyHuang,项目名称:siegfried,代码行数:32,代码来源:siegfried.go


示例6: writeTo

func (ent *RawEntry) writeTo(out io.Writer) error {
	var buf bytes.Buffer
	z, err := flate.NewWriter(&buf, 8)
	if err != nil {
		return err
	}
	if err := binary.Write(z, binary.LittleEndian, uint32(len(ent.LeafInput))); err != nil {
		return err
	}
	if _, err := z.Write(ent.LeafInput); err != nil {
		return err
	}
	if err := binary.Write(z, binary.LittleEndian, uint32(len(ent.ExtraData))); err != nil {
		return err
	}
	if _, err := z.Write(ent.ExtraData); err != nil {
		return err
	}
	if err := z.Close(); err != nil {
		return err
	}

	bytes := buf.Bytes()
	if err := binary.Write(out, binary.LittleEndian, uint32(len(bytes))); err != nil {
		return err
	}
	if _, err := out.Write(bytes); err != nil {
		return err
	}

	return nil
}
开发者ID:mozkeeler,项目名称:certificatetransparency,代码行数:32,代码来源:ct.go


示例7: CompressString

func CompressString(in string) string {
	buf := new(bytes.Buffer)
	compressor, _ := flate.NewWriter(buf, 9)
	compressor.Write([]byte(in))
	compressor.Close()
	return buf.String()
}
开发者ID:skimata,项目名称:go-saml,代码行数:7,代码来源:compress.go


示例8: compressResponse

// conditionally compress the HTTP response. this function must be executed
// after all response headers have been set by the underlying handler (because
// it needs to inspect them) but before they have been written to the client
// (because it needs to change the headers and the data writer).
func compressResponse(w *ResponseWriter, req *http.Request) {
	// rudimentary "can this be compressed" check
	if !compressable(w.Header().Get("Content-Type")) {
		return
	}
	// do not re-encode
	if w.Header().Get("Content-Encoding") != "" {
		return
	}
	ae := req.Header.Get("Accept-Encoding")
	// no q for u
	switch {
	case strings.Contains(ae, "gzip"):
		w.WrapBodyWriter(func(w io.Writer) io.Writer {
			return gzip.NewWriter(w)
		})
		w.Header().Set("Content-Encoding", "gzip")
		w.Header().Del("content-length")
		break
	case strings.Contains(ae, "deflate"):
		w.WrapBodyWriter(func(w io.Writer) io.Writer {
			def, _ := flate.NewWriter(w, flate.DefaultCompression)
			return def
		})
		w.Header().Set("Content-Encoding", "deflate")
		w.Header().Del("content-length")
		break
	}
}
开发者ID:xyproto,项目名称:web,代码行数:33,代码来源:compress.go


示例9: Compress

func (data *Data) Compress() error {
	if data.IsCompressed() || len(data.Content) < 32 {
		// already compressed, or not worth compressing
		// because data is nil or too short
		return nil
	}

	buf := bytes.Buffer{}
	w, err := flate.NewWriter(&buf, flate.BestCompression)
	if err != nil {
		return err
	}
	n, err := w.Write(data.Content)
	if err != nil {
		return err
	}
	if n != len(data.Content) {
		return errs.ServerError("cannot compress")
	}
	err = w.Close()
	if err != nil {
		return err
	}
	compressedBytes := buf.Bytes()

	if len(compressedBytes) < len(data.Content) {
		data.UncompressedLength = len(data.Content)
		data.Content = compressedBytes
		data.ContentEncoding = ceDeflate
	}

	return nil
}
开发者ID:software-projects,项目名称:go-exp,代码行数:33,代码来源:data.go


示例10: NewLogger

//NewLogger creates a new Logger instance
func NewLogger(directory string) (*Logger, error) {
	var err error
	if directory == "" {
		if directory, err = ioutil.TempDir("", "dlog"); err != nil {
			return nil, err
		}
	}
	os.MkdirAll(directory, 0755)
	filePath := filepath.Join(directory, "dlog.bin")

	wFile, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		log.Fatal(err)
	}
	w, _ := flate.NewWriter(wFile, flate.BestCompression)

	l := &Logger{
		wChan: make(chan model.LogEntry, 1000),
		wFile: wFile,
	}

	go l.writeRoutine(w)

	return l, nil
}
开发者ID:netbrain,项目名称:goatpad,代码行数:26,代码来源:dlog.go


示例11: handlerError

func handlerError(rw http.ResponseWriter, html string, code int) {
	var b bytes.Buffer
	w, err := flate.NewWriter(&b, flate.BestCompression)
	if err != nil {
		rw.Header().Set("Content-Type", "text/plain")
		rw.WriteHeader(http.StatusBadGateway)
		io.WriteString(rw, err.Error())
	}

	fmt.Fprintf(w, "HTTP/1.1 %d\r\n", code)
	fmt.Fprintf(w, "Content-Type: text/html; charset=utf-8\r\n")
	fmt.Fprintf(w, "Content-Length: %d\r\n", len(html))
	io.WriteString(w, "\r\n")
	io.WriteString(w, html)
	w.Close()

	b0 := make([]byte, 2)
	binary.BigEndian.PutUint16(b0, uint16(b.Len()))

	rw.Header().Set("Content-Type", "image/gif")
	rw.Header().Set("Content-Length", strconv.Itoa(len(b0)+b.Len()))
	rw.WriteHeader(http.StatusOK)
	rw.Write(b0)
	rw.Write(b.Bytes())
}
开发者ID:sunjiahz,项目名称:goproxy,代码行数:25,代码来源:gae.go


示例12: BenchmarkWriteWithBestDeflateCompression

func BenchmarkWriteWithBestDeflateCompression(b *testing.B) {
	file := benchFile()
	w, _ := flate.NewWriter(file, flate.BestSpeed)
	benchmarkWrite(b, w)
	fi, _ := file.Stat()
	b.SetBytes(int64(int(fi.Size()) / 1024 / 1024))
}
开发者ID:netbrain,项目名称:goatpad,代码行数:7,代码来源:dlog_test.go


示例13: Zip

func Zip(unBytes []byte, contentEncoding string) (zipBytes []byte, err error) {

	switch contentEncoding {
	case "":
		zipBytes = unBytes
		break

	case "gzip":
		var bf bytes.Buffer
		gw := gzip.NewWriter(&bf)
		gw.Write(unBytes)
		gw.Close()

		zipBytes = bf.Bytes()

	case "deflate":
		var bf bytes.Buffer
		var gw *flate.Writer
		gw, err = flate.NewWriter(&bf, -1)
		if err != nil {
			return
		}
		gw.Write(unBytes)
		gw.Close()

		zipBytes = bf.Bytes()

	default:
		err = fmt.Errorf("Zip Unkown Content-Encoding: %v", contentEncoding)
	}

	return
}
开发者ID:uileyar,项目名称:helloworld,代码行数:33,代码来源:network.go


示例14: Flate

func Flate(r io.Writer) (c Compressor) {
	var err error
	if c, err = flate.NewWriter(r, -1); err != nil {
		panic(err)
	}
	return
}
开发者ID:TShadwell,项目名称:fweight,代码行数:7,代码来源:compression.go


示例15: deflate

func deflate(inp []byte) ([]byte, error) {
	out := new(bytes.Buffer)
	writer, _ := flate.NewWriter(out, 1)
	io.Copy(writer, bytes.NewBuffer(inp))
	err := writer.Close()
	return out.Bytes(), err
}
开发者ID:blinkat,项目名称:go-invoker,代码行数:7,代码来源:encoding.go


示例16: NewWriterLevel

// NewWriterLevel creates a new io.WriteCloser that satisfies writes by compressing data written to w.
// It is the caller's responsibility to call Close on the WriteCloser when done.
// level is the compression level, which can be DefaultCompression, NoCompression,
// or any integer value between BestSpeed and BestCompression (inclusive).
func NewWriterLevel(w io.Writer, level int) (io.WriteCloser, os.Error) {
	z := new(writer)
	// ZLIB has a two-byte header (as documented in RFC 1950).
	// The first four bits is the CINFO (compression info), which is 7 for the default deflate window size.
	// The next four bits is the CM (compression method), which is 8 for deflate.
	z.scratch[0] = 0x78
	// The next two bits is the FLEVEL (compression level). The four values are:
	// 0=fastest, 1=fast, 2=default, 3=best.
	// The next bit, FDICT, is unused, in this implementation.
	// The final five FCHECK bits form a mod-31 checksum.
	switch level {
	case 0, 1:
		z.scratch[1] = 0x01
	case 2, 3, 4, 5:
		z.scratch[1] = 0x5e
	case 6, -1:
		z.scratch[1] = 0x9c
	case 7, 8, 9:
		z.scratch[1] = 0xda
	default:
		return nil, os.NewError("level out of range")
	}
	_, err := w.Write(z.scratch[0:2])
	if err != nil {
		return nil, err
	}
	z.w = w
	z.compressor = flate.NewWriter(w, level)
	z.digest = adler32.New()
	return z, nil
}
开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:35,代码来源:writer.go


示例17: Encrypt

func Encrypt(key string, privatekey PrivateKey, win io.Writer, length, sequence int64) (w io.Writer, e os.Error) {
	c, e := simpleCipher(key)
	if e != nil {
		return
	}
	priv, e := readRSAKey(privatekey)
	if e != nil {
		return
	}
	iv := make([]byte, c.BlockSize())
	_, e = rand.Read(iv)
	if e != nil {
		return
	}
	_, e = win.Write(iv) // pass the iv across first
	if e != nil {
		return
	}
	wraw := cipher.StreamWriter{cipher.NewCFBEncrypter(c, iv), win, nil}
	wenc := flate.NewWriter(wraw, flate.BestCompression)
	e = binary.Write(wenc, binary.LittleEndian, length)
	if e != nil {
		return
	}
	e = binary.Write(wenc, binary.LittleEndian, sequence)
	if e != nil {
		return
	}
	return &hashWriter{wenc, wraw, sha256.New(), priv, length}, nil
}
开发者ID:andradeandrey,项目名称:goadmin,代码行数:30,代码来源:crypt.go


示例18: allocFlateWriter

func allocFlateWriter() interface{} {
	// flate.NewWriter (as of this writing) only returns an error
	// if the second argument is invalid. As we are using a standard
	// compression level here, there is no way this can err
	w, _ := flate.NewWriter(emptyBuffer, flate.DefaultCompression)
	return w
}
开发者ID:lestrrat,项目名称:go-saml,代码行数:7,代码来源:encode.go


示例19: Compress

func Compress(in []byte) []byte {
	buf := new(bytes.Buffer)
	compressor, _ := flate.NewWriter(buf, 9)
	compressor.Write(in)
	compressor.Close()
	return buf.Bytes()
}
开发者ID:skimata,项目名称:go-saml,代码行数:7,代码来源:compress.go


示例20: Save

// Save persists a Siegfried struct to disk (path)
func (s *Siegfried) Save(path string) error {
	ls := persist.NewLoadSaver(nil)
	ls.SaveTime(s.C)
	namematcher.Save(s.nm, ls)
	mimematcher.Save(s.mm, ls)
	containermatcher.Save(s.cm, ls)
	xmlmatcher.Save(s.xm, ls)
	riffmatcher.Save(s.rm, ls)
	bytematcher.Save(s.bm, ls)
	textmatcher.Save(s.tm, ls)
	ls.SaveTinyUInt(len(s.ids))
	for _, i := range s.ids {
		i.Save(ls)
	}
	if ls.Err != nil {
		return ls.Err
	}
	f, err := os.Create(path)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.Write(append(config.Magic(), byte(config.Version()[0]), byte(config.Version()[1])))
	if err != nil {
		return err
	}
	z, err := flate.NewWriter(f, 1)
	if err != nil {
		return err
	}
	_, err = z.Write(ls.Bytes())
	z.Close()
	return err
}
开发者ID:richardlehane,项目名称:siegfried,代码行数:35,代码来源:siegfried.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang flate.Writer类代码示例发布时间:2022-05-24
下一篇:
Golang flate.NewReader函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap