當前位置: 首頁>>代碼示例>>Golang>>正文


Golang zlib.Writer類代碼示例

本文整理匯總了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;未經允許,請勿轉載。