Added flush method to encode() method to flush remaining bits

Please review the changes