func testDecompressBuffer(input, bro []byte, T *testing.T) { // Buffer decompression unbro, err := dec.DecompressBuffer(bro, nil) if err != nil { T.Error(err) } check("Buffer decompress", input, unbro, T) }
func main() { // Configure flags flag.StringVar(&compress, "c", "", "compress a file") flag.StringVar(&decompress, "d", "", "decompress a file") flag.StringVar(&output, "o", "", "output file") flag.IntVar(&quality, "q", 9, "compression quality (1-11)") flag.Parse() // Read input var input string if compress != "" { input = compress } else if decompress != "" { input = decompress } else { log.Fatal("You must specify either compress or decompress") } inputData, err := ioutil.ReadFile(input) if err != nil { log.Fatal(err) } // Perform compression or decompression var outputData []byte if compress != "" { params := enc.NewBrotliParams() params.SetQuality(quality) outputData, err = enc.CompressBuffer(params, inputData, nil) } else if decompress != "" { outputData, err = dec.DecompressBuffer(inputData, nil) } if err != nil { log.Fatal(err) } // Write output if output == "" { if compress != "" { output = input + ".bro" } else if decompress != "" { output = input + ".unbro" } } err = ioutil.WriteFile(output, outputData, 0666) if err != nil { log.Fatal(err) } }
func testCompress(s []byte, T *testing.T) { T.Logf("Compressing: %s\n", s) params := enc.NewBrotliParams() buffer1 := make([]byte, len(s)*2) encoded, cerr := enc.CompressBuffer(params, s, buffer1) if cerr != nil { T.Error(cerr) } buffer2 := make([]byte, len(s)) decoded, derr := dec.DecompressBuffer(encoded, buffer2) if derr != nil { T.Error(derr) } if !bytes.Equal(s, decoded) { T.Logf("Decompressed: %s\n", decoded) T.Error("Decoded output does not match original input") } }