зеркало из https://github.com/golang/snappy.git
Fix snappytool to use block, not stream, format
The key difference is replacing snappy.NewWriter and snappy.NewReader with snappy.Encode and snappy.Decode. This change restores the behavior of the previous (written in C) snappytool program.
This commit is contained in:
Родитель
e45cd318e0
Коммит
2e65f85255
|
@ -1,40 +1,46 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/golang/snappy"
|
||||
)
|
||||
|
||||
var (
|
||||
enc = flag.Bool("e", false, "encode")
|
||||
dec = flag.Bool("d", false, "decode")
|
||||
decode = flag.Bool("d", false, "decode")
|
||||
encode = flag.Bool("e", false, "encode")
|
||||
)
|
||||
|
||||
func run() int {
|
||||
func run() error {
|
||||
flag.Parse()
|
||||
if *enc == *dec {
|
||||
fmt.Fprintf(os.Stderr, "exactly one of -d or -e must be given")
|
||||
return 1
|
||||
if *decode == *encode {
|
||||
return errors.New("exactly one of -d or -e must be given")
|
||||
}
|
||||
|
||||
// Encode or decode stdin, and write to stdout.
|
||||
var err error
|
||||
if *enc {
|
||||
_, err = io.Copy(snappy.NewWriter(os.Stdout), os.Stdin)
|
||||
} else {
|
||||
_, err = io.Copy(os.Stdout, snappy.NewReader(os.Stdin))
|
||||
}
|
||||
in, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
return 1
|
||||
return err
|
||||
}
|
||||
return 0
|
||||
|
||||
out := []byte(nil)
|
||||
if *decode {
|
||||
out, err = snappy.Decode(nil, in)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
out = snappy.Encode(nil, in)
|
||||
}
|
||||
_, err = os.Stdout.Write(out)
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(run())
|
||||
if err := run(); err != nil {
|
||||
os.Stderr.WriteString(err.Error() + "\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
/*
|
||||
This is a C version of the cmd/snappytool Go program.
|
||||
|
||||
To build the snappytool binary:
|
||||
g++ main.cpp /usr/lib/libsnappy.a -o snappytool
|
||||
or, if you have built the C++ snappy library from source:
|
||||
|
|
17
snappy.go
17
snappy.go
|
@ -2,10 +2,21 @@
|
|||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// Package snappy implements the snappy block-based compression format.
|
||||
// It aims for very high speeds and reasonable compression.
|
||||
// Package snappy implements the Snappy compression format. It aims for very
|
||||
// high speeds and reasonable compression.
|
||||
//
|
||||
// The C++ snappy implementation is at https://github.com/google/snappy
|
||||
// There are actually two Snappy formats: block and stream. They are related,
|
||||
// but different: trying to decompress block-compressed data as a Snappy stream
|
||||
// will fail, and vice versa. The block format is the Decode and Encode
|
||||
// functions and the stream format is the Reader and Writer types.
|
||||
//
|
||||
// The block format, the more common case, is used when the complete size (the
|
||||
// number of bytes) of the original data is known upfront, at the time
|
||||
// compression starts. The stream format, also known as the framing format, is
|
||||
// for when that isn't always true.
|
||||
//
|
||||
// The canonical, C++ implementation is at https://github.com/google/snappy and
|
||||
// it only implements the block format.
|
||||
package snappy // import "github.com/golang/snappy"
|
||||
|
||||
import (
|
||||
|
|
Загрузка…
Ссылка в новой задаче