adamdebreceni commented on a change in pull request #1170:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1170#discussion_r705159596



##########
File path: libminifi/src/utils/LineByLineInputOutputStreamCallback.cpp
##########
@@ -0,0 +1,129 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "utils/LineByLineInputOutputStreamCallback.h"
+
+#include "utils/gsl.h"
+
+namespace org::apache::nifi::minifi::utils {
+
+LineByLineInputOutputStreamCallback::LineByLineInputOutputStreamCallback(uint64_t
 buffer_size, std::shared_ptr<core::logging::Logger> logger, CallbackType 
callback)
+  : buffer_(buffer_size),
+    logger_(std::move(logger)),
+    callback_(std::move(callback)) {
+}
+
+int64_t LineByLineInputOutputStreamCallback::process(const 
std::shared_ptr<io::BaseStream>& input, const std::shared_ptr<io::BaseStream>& 
output) {
+  gsl_Expects(input);
+  gsl_Expects(output);
+
+  input_size_ = input->size();
+
+  if (int64_t status = readLine(*input); status <= 0) {
+    return status;
+  }
+
+  bool is_first_line = true;
+  do {
+    if (int64_t status = readLine(*input); status < 0) {
+      return status;
+    }
+    std::string output_line = callback_(*current_line_, is_first_line, 
isLastLine());
+    output->write(reinterpret_cast<const uint8_t *>(output_line.data()), 
output_line.size());
+    is_first_line = false;
+  } while (!isLastLine());
+
+  return gsl::narrow<int64_t>(total_bytes_read_);
+}
+
+int64_t LineByLineInputOutputStreamCallback::readLine(io::InputStream& stream) 
{
+  std::string input_line;
+  const size_t current_read = readLine(stream, input_line);
+  if (io::isError(current_read)) {
+    return -1;
+  }
+  if (current_read == 0) {
+    current_line_ = next_line_;
+    next_line_ = std::nullopt;
+    return 0;
+  }
+  current_line_ = next_line_;
+  next_line_ = input_line;
+  total_bytes_read_ += current_read;
+  return gsl::narrow<int64_t>(current_read);
+}
+
+size_t LineByLineInputOutputStreamCallback::readLine(io::InputStream& stream, 
std::string& line) {
+  if (current_pos_ == end_pos_) {
+    const auto current_bytes_read = fillBuffer(stream);
+    if (io::isError(current_bytes_read) || current_bytes_read == 0) {
+      return current_bytes_read;
+    }
+  }
+
+  int64_t pos = current_pos_;
+  while (pos < end_pos_ && buffer_[pos] != '\n') {
+    ++pos;
+  }
+  if (pos < end_pos_ && buffer_[pos] == '\n') {
+    ++pos;
+    line = std::string{buffer_.begin() + current_pos_, buffer_.begin() + pos};
+    current_pos_ = pos;
+    return line.size();
+  }
+
+  std::string start_of_line{buffer_.begin() + current_pos_, buffer_.begin() + 
end_pos_};
+
+  const auto current_bytes_read = fillBuffer(stream);
+  if (io::isError(current_bytes_read)) {
+    return io::STREAM_ERROR;
+  } else if (current_bytes_read == 0) {
+    line = start_of_line;
+    return line.size();
+  }
+
+  pos = current_pos_;
+  while (pos < end_pos_ && buffer_[pos] != '\n') {
+    ++pos;
+  }
+  if (pos < end_pos_ && buffer_[pos] == '\n') {
+    ++pos;
+    line = start_of_line + std::string{buffer_.begin() + current_pos_, 
buffer_.begin() + pos};
+    current_pos_ = pos;
+    return line.size();
+  }
+
+  logger_->log_error("Found a line longer than the max buffer size (%zu 
bytes)", buffer_.size());

Review comment:
       it seems we might process longer than `buffer size` lines, as we might 
append the end of the previous buffer fill




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to