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


Golang lzw.NewWriter函数代码示例

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


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

示例1: make_lzw

func make_lzw(t *Transport, config map[string]interface{}) (uint64, tagfn, tagfn) {
	var wbuf bytes.Buffer
	enc := func(in, out []byte) int {
		if len(in) == 0 {
			return 0
		}
		wbuf.Reset()
		writer := lzw.NewWriter(&wbuf, lzw.LSB, 8 /*litWidth*/)
		if _, err := writer.Write(in); err != nil {
			panic(err)
		}
		writer.Close()
		return copy(out, wbuf.Bytes())
	}
	dec := func(in, out []byte) int {
		if len(in) == 0 {
			return 0
		}
		reader := lzw.NewReader(bytes.NewReader(in), lzw.LSB, 8 /*litWidth*/)
		n, err := readAll(reader, out)
		if err != nil {
			panic(err)
		}
		reader.Close()
		return n
	}
	return tagLzw, enc, dec
}
开发者ID:houcy,项目名称:gofast,代码行数:28,代码来源:tag_lzw.go

示例2: UpdateDeps

// UpdateDeps is used to update the values of the dependencies for a template
func (d *DedupManager) UpdateDeps(t *Template, deps []dep.Dependency) error {
	// Calculate the path to write updates to
	dataPath := path.Join(d.config.Deduplicate.Prefix, t.hexMD5, "data")

	// Package up the dependency data
	td := templateData{
		Data: make(map[string]interface{}),
	}
	for _, dp := range deps {
		// Skip any dependencies that can't be shared
		if !dp.CanShare() {
			continue
		}

		// Pull the current value from the brain
		val, ok := d.brain.Recall(dp)
		if ok {
			td.Data[dp.HashCode()] = val
		}
	}

	// Encode via GOB and LZW compress
	var buf bytes.Buffer
	compress := lzw.NewWriter(&buf, lzw.LSB, 8)
	enc := gob.NewEncoder(compress)
	if err := enc.Encode(&td); err != nil {
		return fmt.Errorf("encode failed: %v", err)
	}
	compress.Close()

	// Compute MD5 of the buffer
	hash := md5.Sum(buf.Bytes())
	d.lastWriteLock.RLock()
	existing, ok := d.lastWrite[t]
	d.lastWriteLock.RUnlock()
	if ok && bytes.Equal(existing, hash[:]) {
		log.Printf("[INFO] (dedup) de-duplicate data '%s' already current",
			dataPath)
		return nil
	}

	// Write the KV update
	kvPair := consulapi.KVPair{
		Key:   dataPath,
		Value: buf.Bytes(),
		Flags: templateDataFlag,
	}
	client, err := d.clients.Consul()
	if err != nil {
		return fmt.Errorf("failed to get consul client: %v", err)
	}
	if _, err := client.KV().Put(&kvPair, nil); err != nil {
		return fmt.Errorf("failed to write '%s': %v", dataPath, err)
	}
	log.Printf("[INFO] (dedup) updated de-duplicate data '%s'", dataPath)
	d.lastWriteLock.Lock()
	d.lastWrite[t] = hash[:]
	d.lastWriteLock.Unlock()
	return nil
}
开发者ID:teemupo,项目名称:consul-template,代码行数:61,代码来源:dedup.go

示例3: compressLZW

func compressLZW(s []byte) []byte {
	var b bytes.Buffer
	w := lzw.NewWriter(&b, lzw.LSB, 8)
	w.Write([]byte("hello, world\n"))
	defer w.Close()
	return b.Bytes()
}
开发者ID:sevki,项目名称:Posts,代码行数:7,代码来源:main.go

示例4: TestNoPalette

func TestNoPalette(t *testing.T) {
	b := &bytes.Buffer{}

	// Manufacture a GIF with no palette, so any pixel at all
	// will be invalid.
	b.WriteString(header[:len(header)-3])
	b.WriteString("\x00\x00\x00") // No global palette.

	// Image descriptor: 2x1, no local palette.
	b.WriteString("\x2c\x00\x00\x00\x00\x02\x00\x01\x00\x00\x02")

	// Encode the pixels: neither is in range, because there is no palette.
	pix := []byte{0, 128}
	enc := &bytes.Buffer{}
	w := lzw.NewWriter(enc, lzw.LSB, 2)
	w.Write(pix)
	w.Close()
	b.WriteByte(byte(len(enc.Bytes())))
	b.Write(enc.Bytes())
	b.WriteByte(0x00) // An empty block signifies the end of the image data.

	b.WriteString(trailer)

	try(t, b.Bytes(), "gif: invalid pixel value")
}
开发者ID:gnanderson,项目名称:go,代码行数:25,代码来源:reader_test.go

示例5: lzwEncode

// lzwEncode returns an LZW encoding (with 2-bit literals) of n zeroes.
func lzwEncode(n int) []byte {
	b := &bytes.Buffer{}
	w := lzw.NewWriter(b, lzw.LSB, 2)
	w.Write(make([]byte, n))
	w.Close()
	return b.Bytes()
}
开发者ID:RajibTheKing,项目名称:gcc,代码行数:8,代码来源:reader_test.go

示例6: writeFrame

func (e *encoder) writeFrame(index int) (err error) {
	e.buildFrameHeader(index)
	_, err = e.w.Write(e.frameHeader[:])
	if err != nil {
		return
	}

	codeSize := log2(e.colorTableSize + 2)
	_, err = e.w.Write([]byte{uint8(codeSize)}) // Start of LZW with minimum code size.
	if err != nil {
		return
	}

	lzww := lzw.NewWriter(&blockWriter{e.w, 0}, lzw.LSB, codeSize)
	_, err = lzww.Write(e.g.Image[index].Pix)
	lzww.Close()
	if err != nil {
		return
	}

	_, err = e.w.Write([]byte{uint8(0x00)}) // End of LZW data.
	if err != nil {
		return
	}

	return nil
}
开发者ID:boppreh,项目名称:gifencoder,代码行数:27,代码来源:gifencoder.go

示例7: RunTestLZW

func RunTestLZW(data []byte) {
	log.Printf("encoding/RunTestLZW: Testing comprssion LZW\n")

	var compressed bytes.Buffer
	w := lzw.NewWriter(&compressed, lzw.MSB, 8)
	defer w.Close()
	now := time.Now()
	w.Write(data)

	cl := compressed.Len()
	log.Printf("encoding/RunTestLZW: Compressed from %d bytes to %d bytes in %d ns\n", len(data), cl, time.Since(now).Nanoseconds())

	recovered := make([]byte, len(data))
	r := lzw.NewReader(&compressed, lzw.MSB, 8)
	defer r.Close()

	total := 0
	n := 100
	var err error = nil
	for err != io.EOF && n != 0 {
		n, err = r.Read(recovered[total:])
		total += n
	}
	log.Printf("encoding/RunTestLZW: Uncompressed from %d bytes to %d bytes in %d ns\n", cl, len(recovered), time.Since(now).Nanoseconds())
}
开发者ID:jmptrader,项目名称:encoding,代码行数:25,代码来源:benchtools.go

示例8: compressRateLzw

/*
compressRateLzw return compression rate of `text` as

	length of compressed text / length of text
*/
func compressRateLzw(text string) (float64, error) {
	var buf bytes.Buffer
	btext := []byte(text)
	btextlen := len(btext)

	if btextlen <= 0 {
		return 0, nil
	}

	w := lzw.NewWriter(&buf, lzw.MSB, 8)

	n, e := w.Write(btext)
	if e != nil {
		fmt.Printf("error: %s, rate: %d, len: %d\n", e, n, btextlen)
		return 1, e
	}

	e = w.Close()
	if e != nil {
		fmt.Printf("error: %s, rate: %d, len: %d\n", e, n, btextlen)
		return 1, e
	}

	return float64(buf.Len()) / float64(btextlen), nil
}
开发者ID:shuLhan,项目名称:wvcgen,代码行数:30,代码来源:compress_rate.go

示例9: TestPixelOutsidePaletteRange

func TestPixelOutsidePaletteRange(t *testing.T) {
	for _, pval := range []byte{0, 1, 2, 3, 255} {
		b := &bytes.Buffer{}

		// Manufacture a GIF with a 2 color palette.
		b.WriteString(header)
		b.WriteString(palette)

		// Image descriptor: 2x1, no local palette.
		b.WriteString("\x2c\x00\x00\x00\x00\x02\x00\x01\x00\x00\x02")

		// Encode the pixels; some pvals trigger the expected error.
		pix := []byte{pval, pval}
		enc := &bytes.Buffer{}
		w := lzw.NewWriter(enc, lzw.LSB, 2)
		w.Write(pix)
		w.Close()
		b.WriteByte(byte(len(enc.Bytes())))
		b.Write(enc.Bytes())
		b.WriteByte(0x00) // An empty block signifies the end of the image data.

		b.WriteString(trailer)

		// No error expected, unless the pixels are beyond the 2 color palette.
		want := ""
		if pval >= 2 {
			want = "gif: invalid pixel value"
		}
		try(t, b.Bytes(), want)
	}
}
开发者ID:gnanderson,项目名称:go,代码行数:31,代码来源:reader_test.go

示例10: lzwCompress

func lzwCompress(input io.Reader, out io.Writer) error {
	writer := lzw.NewWriter(out, lzw.MSB, 8)
	_, err := io.Copy(writer, input)
	if err != nil {
		return err
	}
	writer.Close()
	return nil
}
开发者ID:go-sxb-go,项目名称:version2-exercises-results,代码行数:9,代码来源:compress.go

示例11: Compress

func (self *LzwCompressor) Compress(src []byte) ([]byte, error) {
	cdest := bytes.NewBuffer(make([]byte, 0, len(src)))
	compressor := lzw.NewWriter(cdest, self.order, self.litWidth)
	compressor.Write(src)
	err := compressor.Close()
	if err != nil {
		fmt.Println("Compress Close err:%s", err.Error())
	}
	return cdest.Bytes(), err
}
开发者ID:navy1125,项目名称:gostudy,代码行数:10,代码来源:compresstest.go

示例12: Compress

// Compress places the canary byte in a buffer and uses the same buffer to fill
// in the compressed information of the given input. The configuration supports
// two type of compression: LZW and Gzip. When using Gzip compression format,
// if GzipCompressionLevel is not specified, the 'gzip.DefaultCompression' will
// be assumed.
func Compress(data []byte, config *CompressionConfig) ([]byte, error) {
	var buf bytes.Buffer
	var writer io.WriteCloser
	var err error

	if config == nil {
		return nil, fmt.Errorf("config is nil")
	}

	// Write the canary into the buffer and create writer to compress the
	// input data based on the configured type
	switch config.Type {
	case CompressionTypeLzw:
		buf.Write([]byte{CompressionCanaryLzw})

		writer = lzw.NewWriter(&buf, lzw.LSB, 8)
	case CompressionTypeGzip:
		buf.Write([]byte{CompressionCanaryGzip})

		switch {
		case config.GzipCompressionLevel == gzip.BestCompression,
			config.GzipCompressionLevel == gzip.BestSpeed,
			config.GzipCompressionLevel == gzip.DefaultCompression:
			// These are valid compression levels
		default:
			// If compression level is set to NoCompression or to
			// any invalid value, fallback to Defaultcompression
			config.GzipCompressionLevel = gzip.DefaultCompression
		}
		writer, err = gzip.NewWriterLevel(&buf, config.GzipCompressionLevel)
	default:
		return nil, fmt.Errorf("unsupported compression type")
	}
	if err != nil {
		return nil, fmt.Errorf("failed to create a compression writer; err: %v", err)
	}

	if writer == nil {
		return nil, fmt.Errorf("failed to create a compression writer")
	}

	// Compress the input and place it in the same buffer containing the
	// canary byte.
	if _, err = writer.Write(data); err != nil {
		return nil, fmt.Errorf("failed to compress input data; err: %v", err)
	}

	// Close the io.WriteCloser
	if err = writer.Close(); err != nil {
		return nil, err
	}

	// Return the compressed bytes with canary byte at the start
	return buf.Bytes(), nil
}
开发者ID:hashicorp,项目名称:consul-replicate,代码行数:60,代码来源:compress.go

示例13: lzwEncode

// lzwEncode returns an LZW encoding (with 2-bit literals) of in.
func lzwEncode(in []byte) []byte {
	b := &bytes.Buffer{}
	w := lzw.NewWriter(b, lzw.LSB, 2)
	if _, err := w.Write(in); err != nil {
		panic(err)
	}
	if err := w.Close(); err != nil {
		panic(err)
	}
	return b.Bytes()
}
开发者ID:Greentor,项目名称:go,代码行数:12,代码来源:reader_test.go

示例14: LzwMustCompress

func LzwMustCompress(inb []byte) (outb []byte) {
	buf := &bytes.Buffer{}
	w := lzw.NewWriter(buf, lzw.LSB, 8)
	_, err := w.Write(inb)
	if err != nil {
		w.Close()
		panic(err)
	}
	err = w.Close()
	if err != nil {
		panic(err)
	}
	return buf.Bytes()
}
开发者ID:keysonZZZ,项目名称:kmg,代码行数:14,代码来源:lzw_test.go

示例15: Serialize

// Sérialise le réseau à l'interrieur d'un fichier
func (self *Reto) Serialize(filename string) {
	file, err := os.Create(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()
	stream := lzw.NewWriter(file, lzw.LSB, 8)
	defer stream.Close()
	encoder := json.NewEncoder(stream)
	encoder.Encode(self)
	if err != nil {
		log.Fatal(err)
	}
}
开发者ID:ZackEyston,项目名称:tesseract,代码行数:15,代码来源:reto.go


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