コード例 #1
0
ファイル: decode_test.go プロジェクト: kothar/brotli-go
func TestEOFBehavior(T *testing.T) {
	input := []byte{1, 2, 3, 4}
	output := make([]byte, len(input)*12)

	params := enc.NewBrotliParams()
	params.SetQuality(4)

	output, err := enc.CompressBuffer(params, input, output)
	if err != nil {
		T.Fatal(err)
	}

	// Decompress as a stream
	reader := NewBrotliReader(bytes.NewReader(output))
	receiver := make([]byte, len(input))

	readBytes, err := reader.Read(receiver)
	if err != nil {
		T.Fatal(err)
	}

	if readBytes != len(input) {
		T.Fatalf("Expected to read %d bytes, read %d", len(input), readBytes)
	}
}
コード例 #2
0
ファイル: brotli_test.go プロジェクト: Nelson69/brotli-go
func testCompressBuffer(params *enc.BrotliParams, input []byte, T *testing.T) []byte {
	// Test buffer compression
	bro, err := enc.CompressBuffer(params, input, nil)
	if err != nil {
		T.Error(err)
	}
	T.Logf("  Compressed from %d to %d bytes, %.1f%%", len(input), len(bro), (float32(len(bro))/float32(len(input)))*100)

	return bro
}
コード例 #3
0
ファイル: gbr.go プロジェクト: Nelson69/brotli-go
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)
	}
}
コード例 #4
0
ファイル: brotli_test.go プロジェクト: Nelson69/brotli-go
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")
	}
}
コード例 #5
0
ファイル: decode_test.go プロジェクト: kothar/brotli-go
func TestStreamDecompression(T *testing.T) {

	input1 := bytes.Repeat([]byte("The quick brown fox jumps over the lazy dog. "), 100000)

	output1 := make([]byte, len(input1)*2)
	params := enc.NewBrotliParams()
	params.SetQuality(4)

	_, err := enc.CompressBuffer(params, input1, output1)
	if err != nil {
		T.Fatal(err)
	}

	// Decompress as a stream
	reader := NewBrotliReader(bytes.NewReader(output1))
	decoded := make([]byte, len(input1))

	read, err := io.ReadFull(reader, decoded)
	if err != nil {
		T.Fatal(err)
	}
	if read != len(input1) {
		T.Errorf("Length of decoded stream (%d) doesn't match input (%d)", read, len(input1))
	}

	T.Logf("Input:  %s", input1[:50])
	T.Logf("Output: %s", decoded[:50])
	if !bytes.Equal(decoded, input1) {
		T.Error("Decoded output does not match original input")
	}

	// Decompress using a shorter buffer
	reader = NewBrotliReader(bytes.NewReader(output1))
	decoded = make([]byte, 500)

	read, err = reader.Read(decoded)
	if err != nil {
		T.Fatal(err)
	}
	if read != len(decoded) {
		T.Errorf("Length of decoded stream (%d) shorter than requested (%d)", read, len(decoded))
	}

	T.Logf("Input:  %s", input1[:50])
	T.Logf("Output: %s", decoded[:50])
	if !bytes.Equal(decoded, input1[:len(decoded)]) {
		T.Error("Decoded output does not match original input")
	}

	// Read next buffer
	read, err = reader.Read(decoded)
	if err != nil {
		T.Fatal(err)
	}
	if read != len(decoded) {
		T.Errorf("Length of decoded stream (%d) shorter than requested (%d)", read, len(decoded))
	}

	T.Logf("Input:  %s", input1[len(decoded):len(decoded)+50])
	T.Logf("Output: %s", decoded[:50])
	if !bytes.Equal(decoded, input1[len(decoded):2*len(decoded)]) {
		T.Error("Decoded output does not match original input")
	}
}