| 42 | } |
| 43 | |
| 44 | Status ZlibOutputBuffer::Init() { |
| 45 | // Output buffer size should be greater than 1 because deflation needs atleast |
| 46 | // one byte for book keeping etc. |
| 47 | if (output_buffer_capacity_ <= 1) { |
| 48 | return errors::InvalidArgument( |
| 49 | "output_buffer_bytes should be greater than " |
| 50 | "1"); |
| 51 | } |
| 52 | memset(z_stream_.get(), 0, sizeof(z_stream)); |
| 53 | z_stream_->zalloc = Z_NULL; |
| 54 | z_stream_->zfree = Z_NULL; |
| 55 | z_stream_->opaque = Z_NULL; |
| 56 | int status = |
| 57 | deflateInit2(z_stream_.get(), zlib_options_.compression_level, |
| 58 | zlib_options_.compression_method, zlib_options_.window_bits, |
| 59 | zlib_options_.mem_level, zlib_options_.compression_strategy); |
| 60 | if (status != Z_OK) { |
| 61 | z_stream_.reset(nullptr); |
| 62 | return errors::InvalidArgument("deflateInit failed with status", status); |
| 63 | } |
| 64 | z_stream_->next_in = z_stream_input_.get(); |
| 65 | z_stream_->next_out = z_stream_output_.get(); |
| 66 | z_stream_->avail_in = 0; |
| 67 | z_stream_->avail_out = output_buffer_capacity_; |
| 68 | return Status::OK(); |
| 69 | } |
| 70 | |
| 71 | int32 ZlibOutputBuffer::AvailableInputSpace() const { |
| 72 | return input_buffer_capacity_ - z_stream_->avail_in; |