| 71 | } |
| 72 | |
| 73 | Status Write(const void* data, int64_t nbytes) { |
| 74 | std::lock_guard<std::mutex> guard(lock_); |
| 75 | |
| 76 | auto input = reinterpret_cast<const uint8_t*>(data); |
| 77 | while (nbytes > 0) { |
| 78 | int64_t input_len = nbytes; |
| 79 | int64_t output_len = compressed_->size() - compressed_pos_; |
| 80 | uint8_t* output = compressed_->mutable_data() + compressed_pos_; |
| 81 | ARROW_ASSIGN_OR_RAISE(auto result, |
| 82 | compressor_->Compress(input_len, input, output_len, output)); |
| 83 | compressed_pos_ += result.bytes_written; |
| 84 | |
| 85 | if (result.bytes_read == 0) { |
| 86 | // Not enough output, try to flush it and retry |
| 87 | if (compressed_pos_ > 0) { |
| 88 | RETURN_NOT_OK(FlushCompressed()); |
| 89 | output_len = compressed_->size() - compressed_pos_; |
| 90 | output = compressed_->mutable_data() + compressed_pos_; |
| 91 | ARROW_ASSIGN_OR_RAISE( |
| 92 | result, compressor_->Compress(input_len, input, output_len, output)); |
| 93 | compressed_pos_ += result.bytes_written; |
| 94 | } |
| 95 | } |
| 96 | input += result.bytes_read; |
| 97 | nbytes -= result.bytes_read; |
| 98 | total_pos_ += result.bytes_read; |
| 99 | if (compressed_pos_ == compressed_->size()) { |
| 100 | // Output buffer full, flush it |
| 101 | RETURN_NOT_OK(FlushCompressed()); |
| 102 | } |
| 103 | if (result.bytes_read == 0) { |
| 104 | // Need to enlarge output buffer |
| 105 | RETURN_NOT_OK(compressed_->Resize(compressed_->size() * 2)); |
| 106 | } |
| 107 | } |
| 108 | return Status::OK(); |
| 109 | } |
| 110 | |
| 111 | Status Flush() { |
| 112 | std::lock_guard<std::mutex> guard(lock_); |
nothing calls this directly
no test coverage detected