Ejemplo n.º 1
0
func (w *writer) Close() error {
	defer C.BZ2_bzCompressEnd(&w.bzStream)

	// Finish the stream.
	w.bzStream.next_in = nil
	w.bzStream.avail_in = 0

	done := false
	for !done {
		w.bzStream.next_out = (*C.char)(unsafe.Pointer(&w.outBuffer[0]))
		w.bzStream.avail_out = C.uint(len(w.outBuffer))

		ret := C.BZ2_bzCompress(&w.bzStream, C.BZ_FINISH)

		switch ret {
		case C.BZ_FINISH_OK:
		case C.BZ_STREAM_END:
			done = true
		default:
			return translateError(ret)
		}

		if err := w.writeAvailOut(); err != nil {
			return err
		}
	}

	return nil
}
Ejemplo n.º 2
0
func (w *writer) Write(b []byte) (n int, err error) {
	if len(b) == 0 {
		return 0, nil
	}

	w.bzStream.next_in = (*C.char)(unsafe.Pointer(&b[0]))
	w.bzStream.avail_in = C.uint(len(b))

	for w.bzStream.avail_in > 0 {
		// Compress the available data.
		w.bzStream.next_out = (*C.char)(unsafe.Pointer(&w.outBuffer[0]))
		w.bzStream.avail_out = C.uint(len(w.outBuffer))

		ret := C.BZ2_bzCompress(&w.bzStream, C.BZ_RUN)
		if ret != C.BZ_RUN_OK {
			return len(b) - int(w.bzStream.avail_in), translateError(ret)
		}

		// Write the contents of the output buffer to the underlying writer.
		if err = w.writeAvailOut(); err != nil {
			return len(b) - int(w.bzStream.avail_in), err
		}
	}

	return len(b), nil
}