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

Golang zlib.Writer类代码示例

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

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



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

示例1: SetTileGrid

func (d *Data) SetTileGrid(grid DataTileGrid) (err error) {
	var (
		buf        bytes.Buffer
		b64Encoder io.WriteCloser
		zlibWriter *zlib.Writer
		gids       []uint32
		gridTile   DataTileGridTile
	)
	d.Encoding = "base64"
	d.Compression = "zlib"
	d.RawTiles = []DataTile{}
	gids = make([]uint32, grid.Width*grid.Height)
	for y := 0; y < grid.Height; y++ {
		for x := 0; x < grid.Width; x++ {
			gridTile = grid.Tiles[x][y]
			gids[grid.Width*y+x] = encodeGid(
				gridTile.Id,
				gridTile.FlipX,
				gridTile.FlipY,
				gridTile.FlipD)
		}
	}
	b64Encoder = base64.NewEncoder(base64.StdEncoding, &buf)
	zlibWriter = zlib.NewWriter(b64Encoder)
	if err = binary.Write(zlibWriter, binary.LittleEndian, gids); err != nil {
		return
	}
	zlibWriter.Close()
	b64Encoder.Close()
	d.RawContents = buf.String()
	return
}
开发者ID:pikkpoiss,项目名称:tmxgo,代码行数:32,代码来源:tmx.go


示例2: loadFont

func loadFont(f *fontdata) (font *FontMetrics) {
	var err error

	if font, err = ParseFontMetricsFile(f.Metrics, f.Label); err != nil {
		log.Fatalf("loading font metrics: %v", err)
	}
	if f.StemV > 0 && font.StemV <= 0 {
		font.StemV = f.StemV
	}
	if len(f.FontFile) > 0 {
		font.File = []byte(f.FontFile)
		var buf bytes.Buffer
		var writer *zlib.Writer
		if writer, err = zlib.NewWriterLevel(&buf, zlib.BestCompression); err != nil {
			log.Fatal("Setting up zlib compressor: ", err)
		}
		if _, err = writer.Write(font.File); err != nil {
			log.Fatal("Writing to zlib compressor: ", err)
		}
		if err = writer.Close(); err != nil {
			log.Fatal("Closing zlib compressor: ", err)
		}
		font.CompressedFile = buf.Bytes()
	}

	return
}
开发者ID:renquinn,项目名称:warddirectory,代码行数:27,代码来源:font.go


示例3: Generate

func (pp *pendingPayload) Generate(hostname string) (err error) {
	var buffer bytes.Buffer

	// Begin with the nonce
	if _, err = buffer.Write([]byte(pp.nonce)); err != nil {
		return
	}

	var compressor *zlib.Writer
	if compressor, err = zlib.NewWriterLevel(&buffer, 3); err != nil {
		return
	}

	// Append all the events
	for _, event := range pp.events[pp.ack_events:] {
		// Add host field
		event.Event["host"] = hostname
		if err = pp.bufferJdatDataEvent(compressor, event); err != nil {
			return
		}
	}

	compressor.Close()

	pp.payload = buffer.Bytes()
	pp.payload_start = pp.ack_events

	return
}
开发者ID:pascalrobert,项目名称:log-courier,代码行数:29,代码来源:pending_payload.go


示例4: Compress

func (self *ZlibCompressor) Compress(src []byte) ([]byte, error) {
	var err error
	var compressor *zlib.Writer
	cdest := bytes.NewBuffer(make([]byte, 0, len(src)))
	if self.dict == nil {
		compressor, err = zlib.NewWriterLevel(cdest, self.level)
	} else {
		compressor, err = zlib.NewWriterLevelDict(cdest, self.level, self.dict)
	}
	compressor.Write(src)
	err = compressor.Close()
	if err != nil {
		fmt.Println("Compress Close err:%s", err.Error())
	}
	return cdest.Bytes(), err
}
开发者ID:navy1125,项目名称:gostudy,代码行数:16,代码来源:compresstest.go


示例5: createCompressedPacket

// createCompressedPacket generates a compressed protocol packet after
// compressing the specified payload.
func (rw *compressRW) createCompressedPacket(payload []byte) ([]byte, error) {
	var (
		w             *zlib.Writer
		z             bytes.Buffer
		packet        []byte
		err           error
		payloadLength int
	)

	// TODO: add a property for compression level
	if w, err = zlib.NewWriterLevel(&z, zlib.DefaultCompression); err != nil {
		goto E
	}

	if _, err = w.Write(payload); err != nil {
		goto E
	}

	if err = w.Close(); err != nil {
		goto E
	}

	payloadLength = z.Len()

	// allocate buffer for the compressed packet
	// header (7 bytes) + payload
	packet = make([]byte, 7+payloadLength)

	// compressed header
	// - size of compressed payload
	putUint24(packet[0:3], uint32(payloadLength))
	// - packet sequence number
	packet[3] = rw.seqno
	// - size of payload before it was compressed
	putUint24(packet[4:7], uint32(len(payload)))

	// copy the compressed payload
	copy(packet[7:], z.Bytes())

	return packet, nil

E:
	return nil, myError(ErrCompression, err)

}
开发者ID:nirbhayc,项目名称:mysql,代码行数:47,代码来源:compress.go


示例6: Render

func (stream *PDFStream) Render(indent string, out io.Writer) (err error) {
	if CompressStreams && len(stream.Compressed) == 0 {
		// compress the stream contents
		var buf bytes.Buffer
		var writer *zlib.Writer
		if writer, err = zlib.NewWriterLevel(&buf, zlib.BestCompression); err != nil {
			return
		}
		if _, err = writer.Write(stream.Data); err != nil {
			return
		}
		if err = writer.Close(); err != nil {
			return
		}
		stream.Compressed = buf.Bytes()
	}
	if CompressStreams {
		stream.Map["Filter"] = PDFName("FlateDecode")
		stream.Map["Length"] = PDFNumber(len(stream.Compressed))
	} else {
		stream.Map["Length"] = PDFNumber(len(stream.Data))
	}
	if err = stream.Map.Render(indent, out); err != nil {
		return
	}
	fmt.Fprint(out, "\nstream\n")
	if CompressStreams {
		if _, err = out.Write(stream.Compressed); err != nil {
			return
		}
	} else {
		if _, err = out.Write(stream.Data); err != nil {
			return
		}
	}
	fmt.Fprint(out, "endstream")
	return
}
开发者ID:renquinn,项目名称:warddirectory,代码行数:38,代码来源:pdf.go


示例7: Generate

func (pp *pendingPayload) Generate() (err error) {
	var buffer bytes.Buffer

	// Assertion
	if len(pp.events) == 0 {
		return ErrPayloadCorrupt
	}

	// Begin with the nonce
	if _, err = buffer.Write([]byte(pp.nonce)[0:16]); err != nil {
		return
	}

	var compressor *zlib.Writer
	if compressor, err = zlib.NewWriterLevel(&buffer, 3); err != nil {
		return
	}

	// Append all the events
	for _, event := range pp.events[pp.ack_events:] {
		if err = binary.Write(compressor, binary.BigEndian, uint32(len(event.Event))); err != nil {
			return
		}
		if _, err = compressor.Write(event.Event); err != nil {
			return
		}
	}

	compressor.Close()

	pp.payload = buffer.Bytes()
	pp.last_sequence = 0
	pp.sequence_len = len(pp.events) - pp.ack_events

	return
}
开发者ID:atmosx,项目名称:log-courier,代码行数:36,代码来源:pending_payload.go


示例8: handlePeer

func (c *Client) handlePeer(in io.ReadCloser, out io.WriteCloser,
	first bool) (err error) {
	var m method
	write := bufio.NewWriter(out)
	number := rand.Int63n(0x7fff)
	lock, pk := GenerateLock()

	if first {
		send(write, "MyNick", []byte(c.Nick))
		sendf(write, "Lock", func(w *bufio.Writer) {
			fmt.Fprintf(w, "%s Pk=%s", lock, pk)
		})
	}

	/* Step 0 - figure out who we're talking to */
	buf := bufio.NewReader(in)
	if err = readCmd(buf, &m); err != nil {
		return
	}
	if m.name != "MyNick" {
		return errors.New("Expected $MyNick first")
	}
	nick := string(m.data)

	/* Step 1 - make sure we have the only connection to the peer */
	bad := false
	p := c.peer(nick, func(p *peer) {
		if p.state == Uninitialized || p.state == RequestingConnection {
			p.state = Connecting
		} else {
			bad = true
		}
	})
	if bad {
		return errors.New("Invalid state with peer tables")
	}
	if p.write != nil {
		panic("already have a write connection")
	}
	p.in = in
	p.out = out
	defer c.peerGone(p.nick)

	c.log("Connected to: " + p.nick)
	defer c.log("Disconnected from: " + p.nick)

	/* Step 2 - get their lock so we can respond with our nick/key */
	if err = readCmd(buf, &m); err != nil {
		return
	}
	if m.name != "Lock" {
		return errors.New("Expected $Lock second")
	}
	idx := bytes.IndexByte(m.data, ' ')
	if idx == -1 {
		return errors.New("Invalid $Lock")
	}

	/* Step 3 - send our nick/lock/supports/direction metadata */
	if !first {
		send(write, "MyNick", []byte(c.Nick))
		sendf(write, "Lock", func(w *bufio.Writer) {
			fmt.Fprintf(w, "%s Pk=%s", lock, pk)
		})
	}
	send(write, "Supports",
		[]byte("MiniSlots XmlBZList ADCGet ZLIG GetZBlock TTHF"))
	mydirection := "Upload"
	if len(p.dls) > 0 {
		mydirection = "Download"
	}
	sendf(write, "Direction", func(w *bufio.Writer) {
		fmt.Fprintf(w, "%s %d", mydirection, number)
	})
	send(write, "Key", GenerateKey(m.data[0:idx]))

	/* Step 4 - receive what they support (optional) */
	if err = readCmd(buf, &m); err != nil {
		return
	}
	p.supports = make([]string, 0)
	if m.name == "Supports" {
		for _, s := range bytes.Split(m.data, []byte(" ")) {
			p.supports = append(p.supports, string(s))
		}
		if err = readCmd(buf, &m); err != nil {
			return
		}
	}

	/* Step 5 - receive their direction */
	if m.name != "Direction" {
		return errors.New("Expected $Direction")
	}
	/* Don't actually care about the direction */

	/* Step 6 - receive their key */
	if err = readCmd(buf, &m); err != nil {
		return
	}
//.........这里部分代码省略.........
开发者ID:alexcrichton,项目名称:fargo,代码行数:101,代码来源:peer.go



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang template.Rules类代码示例发布时间:2022-05-24
下一篇:
Golang zlib.NewWriterLevelDict函数代码示例发布时间: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