Example #1
0
func init() {
	RegisterEncoder(FormatBrotli, "cgo",
		func(w io.Writer, lvl int) io.WriteCloser {
			c := enc.NewBrotliParams()
			c.SetQuality(lvl)
			return enc.NewBrotliWriter(c, w)
		})
	RegisterDecoder(FormatBrotli, "cgo",
		func(r io.Reader) io.ReadCloser {
			return dec.NewBrotliReaderSize(r, 4096)
		})
}
Example #2
0
func testCompressStream(params *enc.BrotliParams, input []byte, writer io.Writer, T *testing.T) {
	bwriter := enc.NewBrotliWriter(params, writer)
	n, err := bwriter.Write(input)
	if err != nil {
		T.Error(err)
	}

	err = bwriter.Close()
	if err != nil {
		T.Error(err)
	}

	if n != len(input) {
		T.Error("Not all input was consumed")
	}
}
Example #3
0
func compressFile(inFileName string, outFileName string, level int, verbose bool, standardOutput bool) {
	var buffer [1024 * 1024 * 32]byte
	var inFile *os.File
	var err error
	if inFileName == "-" {
		fmt.Printf("Using stdin!\n")
		inFile = os.Stdin
	} else {
		inFile, err = os.Open(inFileName)
		checkError(err)
	}
	defer inFile.Close()
	var outFile *os.File

	if !standardOutput {
		outFile, err = os.Create(outFileName)
		checkError(err)
	} else {
		outFile = os.Stdout
	}

	defer outFile.Close()

	hasher := NewHashWriter()
	archiveWriter := NewArchiveWriter(hasher, outFile)
	teeReader := io.TeeReader(inFile, hasher)

	params := enc.NewBrotliParams()
	params.SetQuality(level)
	params.SetLgwin(24)
	brotliWriter := enc.NewBrotliWriter(params, archiveWriter)
	defer brotliWriter.Close()

	// Perform the actual compression
	io.CopyBuffer(brotliWriter, teeReader, buffer[:])
	defer os.Remove(inFileName)
}
Example #4
0
func (bc *brotliCompressor) Apply(writer io.Writer, quality int32) (io.Writer, error) {
	params := enc.NewBrotliParams()
	params.SetQuality(int(quality))
	return enc.NewBrotliWriter(params, writer), nil
}