本文整理汇总了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
}
示例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
}
示例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)
}
示例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
}
示例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
}