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
//!+close
// Close flushes the compressed data and closes the stream.
// It does not close the underlying io.Writer.
func (w *writer) Close() error {
	if w.stream == nil {
		panic("closed")
	}
	defer func() {
		C.BZ2_bzCompressEnd(w.stream)
		w.stream = nil
	}()
	for {
		inlen, outlen := C.uint(0), C.uint(cap(w.outbuf))
		r := C.bz2compress(w.stream, C.BZ_FINISH, nil, &inlen,
			(*C.char)(unsafe.Pointer(&w.outbuf)), &outlen)
		if _, err := w.w.Write(w.outbuf[:outlen]); err != nil {
			return err
		}
		if r == C.BZ_STREAM_END {
			return nil
		}
	}
}