本文整理汇总了Golang中compress/lzw.NewWriter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewWriter函数的具体用法?Golang NewWriter怎么用?Golang NewWriter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewWriter函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
示例16: newStream
func newStream(filter name) *stream {
st := new(stream)
st.filter = filter
switch filter {
case streamLZWDecode:
st.writer = lzw.NewWriter(&st.Buffer, lzw.MSB, 8)
case streamFlateDecode:
st.writer = zlib.NewWriter(&st.Buffer)
default:
// TODO: warn about bad filter names?
st.writer = &st.Buffer
}
return st
}
开发者ID:nolenroyalty,项目名称:bangarang,代码行数:14,代码来源:stream.go
示例17: lzw_lsb
func (e *Engine) lzw_lsb() error {
var buf bytes.Buffer
var litWidth int
var err error
litWidth, err = e.stack.PopInt()
if err == nil {
w := lzw.NewWriter(&buf, lzw.LSB, litWidth)
_, err = w.Write(e.stack.Pop())
w.Close()
}
if err == nil {
e.stack.Push(buf.Bytes())
}
return err
}
开发者ID:ancientlore,项目名称:hashsrv,代码行数:15,代码来源:compress.go
示例18: main
func main() {
ugo.MaxProcs()
ufs.NewDirWalker(false, nil, func(fullPath string) bool {
blobs = append(blobs, ufs.ReadBinaryFile(fullPath, true))
return true
}).Walk(dirPath)
testComp("flate1", func(w io.Writer) (wc io.WriteCloser) {
var err error
if wc, err = flate.NewWriter(w, 1); err != nil {
panic(err)
}
return
}, flate.NewReader)
testComp("flate9", func(w io.Writer) (wc io.WriteCloser) {
var err error
if wc, err = flate.NewWriter(w, 9); err != nil {
panic(err)
}
return
}, flate.NewReader)
testComp("lzw\t", func(w io.Writer) io.WriteCloser {
return lzw.NewWriter(w, lzw.MSB, 8)
}, func(r io.Reader) io.ReadCloser {
return lzw.NewReader(r, lzw.MSB, 8)
})
testComp("zlib", func(w io.Writer) io.WriteCloser {
return zlib.NewWriter(w)
}, func(r io.Reader) (rc io.ReadCloser) {
var err error
if rc, err = zlib.NewReader(r); err != nil {
panic(err)
}
return
})
testComp("gzip", func(w io.Writer) io.WriteCloser {
return gzip.NewWriter(w)
}, func(r io.Reader) (rc io.ReadCloser) {
var err error
if rc, err = gzip.NewReader(r); err != nil {
panic(err)
}
return
})
printStats("PACK:", packStats)
printStats("UNPACK:", unpackStats)
}
开发者ID:metaleap,项目名称:go-misctools,代码行数:47,代码来源:main.go
示例19: Fuzz
func Fuzz(data []byte) int {
r := lzw.NewReader(bytes.NewReader(data), lzw.MSB, 8)
uncomp := make([]byte, 64<<10)
n, err := r.Read(uncomp)
if err != nil && err != io.EOF {
return 0
}
if n == len(uncomp) {
return 0 // too large
}
uncomp = uncomp[:n]
for width := 2; width <= 8; width++ {
uncomp0 := append([]byte{}, uncomp...)
for i, v := range uncomp0 {
uncomp0[i] = v & (1<<uint(width) - 1)
}
for _, order := range []lzw.Order{lzw.MSB, lzw.LSB} {
buf := new(bytes.Buffer)
w := lzw.NewWriter(buf, order, width)
n, err := w.Write(uncomp0)
if err != nil {
fmt.Printf("order=%v width=%v\n", order, width)
panic(err)
}
if n != len(uncomp0) {
fmt.Printf("order=%v width=%v\n", order, width)
panic("short write")
}
if err := w.Close(); err != nil {
fmt.Printf("order=%v width=%v\n", order, width)
panic(err)
}
r1 := lzw.NewReader(buf, order, width)
uncomp1, err := ioutil.ReadAll(r1)
if err != nil {
fmt.Printf("order=%v width=%v\n", order, width)
panic(err)
}
if !bytes.Equal(uncomp0, uncomp1) {
fmt.Printf("order=%v width=%v\n", order, width)
panic("data differs")
}
}
}
return 1
}
开发者ID:sjn1978,项目名称:go-fuzz,代码行数:46,代码来源:lzw.go
示例20: encodeImageBlock
func encodeImageBlock(w io.Writer, img *image.Paletted) error {
// start image
litWidth := int(paletteBits(img.Palette))
if litWidth < 2 {
litWidth = 2
}
bounds := img.Bounds()
if err := writeData(w,
byte(0x2C),
uint16(bounds.Min.X), uint16(bounds.Min.Y), uint16(bounds.Dx()), uint16(bounds.Dy()),
byte(0),
byte(litWidth),
); err != nil {
return err
}
// start compression
blocks := &blockWriter{w: w}
compress := lzw.NewWriter(blocks, lzw.LSB, litWidth)
// write each scan line (might not be contiguous)
startX := img.Rect.Min.X
stopX := img.Rect.Max.X
stopY := img.Rect.Max.Y
for y := img.Rect.Min.Y; y < stopY; y++ {
start := img.PixOffset(startX, y)
stop := img.PixOffset(stopX, y)
if _, err := compress.Write(img.Pix[start:stop]); err != nil {
return err
}
}
if err := compress.Close(); err != nil {
return err
}
return blocks.Close()
}
开发者ID:pkdevboxy,项目名称:pixelcycle-sample-app,代码行数:44,代码来源:encode.go
注:本文中的compress/lzw.NewWriter函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论