| // SPDX-License-Identifier: Apache-2.0 | |
| // SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership | |
| // SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements | |
| #pragma once | |
| #include <array> | |
| #include <cstddef> | |
| #include <stdexcept> | |
| #include <vector> | |
| #include <string> | |
| #include "Stream.h" | |
| #include "minifi-cpp/utils/Id.h" | |
| namespace org::apache::nifi::minifi::io { | |
| enum class LengthPrefixSize { | |
| _16BIT, | |
| _32BIT | |
| }; | |
| class InputStream : public virtual Stream { | |
| public: | |
| [[nodiscard]] virtual size_t size() const = 0; | |
| /** | |
| * Reads a byte array from the stream. Use isError (Stream.h) to check for errors. | |
| * @param out_buffer reference in which will set the result | |
| * @param len length to read | |
| * @return resulting read size or STREAM_ERROR on error or static_cast<size_t>(-2) on EAGAIN | |
| **/ | |
| virtual size_t read(std::span<std::byte> out_buffer) = 0; | |
| /** | |
| * Read length prefixed string from stream. Use isError (Stream.h) to check for errors. | |
| * @param str reference string | |
| * @param length_prefix_size The bit width of the length prefix, 16bit or 32bit | |
| * @param max_length The max length of the string, to avoid excessive allocations | |
| * @return resulting read size or STREAM_ERROR on error or static_cast<size_t>(-2) on EAGAIN | |
| **/ | |
| size_t read(std::string &str, LengthPrefixSize length_prefix_size, size_t max_length); | |
| /** | |
| * Read a bool from stream. Use isError (Stream.h) to check for errors. | |
| * @param value reference to the output | |
| * @return resulting read size or STREAM_ERROR on error or static_cast<size_t>(-2) on EAGAIN | |
| **/ | |
| size_t read(bool& value); | |
| /** | |
| * Read a uuid from stream. Use isError (Stream.h) to check for errors. | |
| * @param value reference to the output | |
| * @return resulting read size or STREAM_ERROR on error or static_cast<size_t>(-2) on EAGAIN | |
| **/ | |
| size_t read(utils::Identifier& value); | |
| /** | |
| * Reads sizeof(Integral) bytes from the stream. Use isError (Stream.h) to check for errors. | |
| * @param value reference in which will set the result | |
| * @return resulting read size or STREAM_ERROR on error or static_cast<size_t>(-2) on EAGAIN | |
| **/ | |
| template<typename Integral, typename = std::enable_if_t<std::is_unsigned<Integral>::value && !std::is_same<Integral, bool>::value>> | |
| size_t read(Integral& value) { | |
| std::array<std::byte, sizeof(Integral)> buf; | |
| if (read(buf) != sizeof(Integral)) { | |
| return io::STREAM_ERROR; | |
| } | |
| value = 0; | |
| for (std::size_t byteIdx = 0; byteIdx < sizeof(Integral); ++byteIdx) { | |
| value += static_cast<Integral>(buf[byteIdx]) << (8 * (sizeof(Integral) - 1) - 8 * byteIdx); | |
| } | |
| return sizeof(Integral); | |
| } | |
| std::optional<std::byte> readByte(); | |
| }; | |
| } // namespace org::apache::nifi::minifi::io |