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


Golang Writer.Write方法代码示例

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


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

示例1: 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

示例2: 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

示例3: 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

示例4: 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

示例5: 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


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