Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[core] Add natvie stream and stream buff (#36511)

Pawel Raasz175d687f5f0a
diff --git a/src/common/util/CMakeLists.txt b/src/common/util/CMakeLists.txt
index 2891b52f285..c73bdf489bd 100644
--- a/src/common/util/CMakeLists.txt
+++ b/src/common/util/CMakeLists.txt
@@ -9,6 +9,7 @@ set(UTIL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include/)
set(WIN32_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/os.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/parallel_io.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/win_file_util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/win_io.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/win_memory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/win/win_mmap_object.cpp
@@ -17,6 +18,7 @@ set(WIN32_SOURCES
)
set(UNIX_SOURCES
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/os/lin/lin_file_util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/lin/lin_io.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/lin/lin_memory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/os/lin/lin_mmap_object.cpp
@@ -47,6 +49,8 @@ target_sources(${TARGET_NAME}
${UTIL_INCLUDE_DIR}/openvino/util/log.hpp
${UTIL_INCLUDE_DIR}/openvino/util/memory.hpp
${UTIL_INCLUDE_DIR}/openvino/util/mmap_object.hpp
+ ${UTIL_INCLUDE_DIR}/openvino/util/native_stream.hpp
+ ${UTIL_INCLUDE_DIR}/openvino/util/native_streambuf.hpp
${UTIL_INCLUDE_DIR}/openvino/util/os.hpp
${UTIL_INCLUDE_DIR}/openvino/util/ov_version.hpp
${UTIL_INCLUDE_DIR}/openvino/util/parallel_io.hpp
@@ -65,6 +69,8 @@ target_sources(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/src/file_path.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/file_util.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/log.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/native_stream.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/native_streambuf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/parallel_read_streambuf.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/xml_parse_utils.cpp
# Windows specific sources
diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hpp
index 05a6a8e990f..eda6ad8a0e1 100644
--- a/src/common/util/include/openvino/util/file_util.hpp
+++ b/src/common/util/include/openvino/util/file_util.hpp
@@ -21,9 +21,13 @@ namespace ov {
#ifdef _WIN32
// Windows uses HANDLE (void*) for file handles
using FileHandle = void*;
+// Value matches the Windows SDK: ((HANDLE)(ULONG_PTR)-1).
+// In C++20 can be constexpr if replaced with std::bit_cast<void*>(-1).
+inline const FileHandle invalid_handle = reinterpret_cast<void*>(static_cast<uintptr_t>(-1));
#else
// Linux/Unix uses int for file descriptors
using FileHandle = int;
+inline constexpr FileHandle invalid_handle = -1;
#endif
} // namespace ov
@@ -293,4 +297,49 @@ const char* trim_file_name(const char* const fname);
inline uint64_t get_id_for_file(const std::filesystem::path& path, size_t offset, size_t size) {
return util::u64_hash_combine(std::filesystem::hash_value(path), {offset, size});
}
+
+/**
+ * @brief Flags controlling how a file is opened via @ref open_file.
+ */
+enum class FileMode : unsigned {
+ READ = 1u << 0, //!< Open for reading.
+ // WRITE = 1u << 1, //!< (reserved) Open for writing.
+ DIRECT = 1u << 2, //!< Bypass the OS page cache.
+};
+
+/// @brief Combine two @ref FileMode values.
+constexpr FileMode operator|(FileMode a, FileMode b) noexcept {
+ return static_cast<FileMode>(static_cast<unsigned>(a) | static_cast<unsigned>(b));
+}
+
+/**
+ * @brief Returns true when **all** bits of @p flags are set in @p mode.
+ *
+ * @param mode The @ref FileMode value to test.
+ * @param flags The flag or combination of flags that must all be present in @p mode.
+ * @return True if every bit in @p flags is set in @p mode; False otherwise.
+ */
+constexpr bool mode_set(FileMode mode, FileMode flags) noexcept {
+ const auto mask = static_cast<unsigned>(flags);
+ return (static_cast<unsigned>(mode) & mask) == mask;
+}
+
+/**
+ * @brief Open a file with the specified access mode.
+ *
+ * @param path Path to the file.
+ * @param mode Access flags. Defaults to @c FileMode::READ.
+ * @return A valid @ref FileHandle on success, or @c ov::invalid_handle on failure.
+ */
+FileHandle open_file(const std::filesystem::path& path, FileMode mode = FileMode::READ);
+
+/**
+ * @brief Close a file handle previously opened by @ref open_file.
+ *
+ * Safe to call with @c ov::invalid_handle (no-op).
+ *
+ * @param handle The handle to close.
+ */
+void close_file(FileHandle handle);
+
} // namespace ov::util
diff --git a/src/common/util/include/openvino/util/memory.hpp b/src/common/util/include/openvino/util/memory.hpp
index 82f23392cc2..7bf3cdbdedd 100644
--- a/src/common/util/include/openvino/util/memory.hpp
+++ b/src/common/util/include/openvino/util/memory.hpp
@@ -12,6 +12,9 @@
namespace ov::util {
+/** @brief Minimum guaranteed page alignment on all supported platforms (x86, ARM, RISC-V). */
+inline constexpr size_t min_page_alignment = 4096;
+
/**
* @brief Rounds @p size up to the nearest multiple of @p alignment.
*
diff --git a/src/common/util/include/openvino/util/native_stream.hpp b/src/common/util/include/openvino/util/native_stream.hpp
new file mode 100644
index 00000000000..41b065060a9
--- /dev/null
+++ b/src/common/util/include/openvino/util/native_stream.hpp
@@ -0,0 +1,83 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * @brief An ifstream-like input stream backed by NativeStreamBuf (io_read_into platform backend).
+ * @file native_stream.hpp
+ */
+
+#pragma once
+
+#include <filesystem>
+#include <istream>
+
+#include "openvino/util/native_streambuf.hpp"
+
+namespace ov::util {
+
+/**
+ * @brief A read-only std::istream backed by NativeStreamBuf.
+ *
+ * Uses native OS file handles and the platform io_read_into backend instead of C-runtime stdio,
+ * making it a drop-in replacement for std::ifstream for binary sequential and random-access reads.
+ *
+ * Three construction modes:
+ * - **default constructor**: stream not associated with any file; behaves like a default-constructed
+ * std::ifstream — reads immediately return EOF.
+ * - **path constructor** (owning): opens a read-only native handle from a path; closes it on destruction.
+ * - **handle constructor** (non-owning): borrows a caller-provided handle for a sub-region
+ * [@p offset, @p offset + @p size); the handle is never closed by this class.
+ *
+ * Supports move construction and move assignment (via swap). Copy is deleted.
+ *
+ * @code
+ * ov::util::NativeIfstream empty; // default — not associated with any file
+ * ov::util::NativeIfstream file(path); // owning — whole file from offset 0
+ * ov::util::NativeIfstream view(handle, off, size); // non-owning — sub-region [off, off+size)
+ * file.seekg(blob_offset);
+ * file.read(dst, n);
+ * @endcode
+ */
+class NativeIfstream : public std::istream {
+public:
+ /** @brief Constructs a stream not associated with any file. Any read immediately returns EOF. */
+ NativeIfstream() noexcept;
+
+ /**
+ * @brief Opens a read-only native handle from @p path and takes ownership of it.
+ *
+ * The stream is positioned at the beginning of the file. Use @c seekg() to reposition.
+ *
+ * @param path Path to the file to open.
+ */
+ explicit NativeIfstream(const std::filesystem::path& path);
+
+ /**
+ * @brief Builds a stream over a caller-owned native handle. The handle is never closed by this class.
+ *
+ * The caller must keep @p handle valid for the entire lifetime of the stream.
+ *
+ * @param handle Read-only native handle. Ownership remains with the caller.
+ * @param offset Absolute file offset that maps to logical stream position 0.
+ * @param size Number of readable bytes from @p offset.
+ */
+ explicit NativeIfstream(FileHandle handle, std::streamoff offset, std::streamoff size);
+
+ ~NativeIfstream();
+
+ NativeIfstream(const NativeIfstream&) = delete;
+ NativeIfstream& operator=(const NativeIfstream&) = delete;
+
+ NativeIfstream(NativeIfstream&& other) noexcept;
+ NativeIfstream& operator=(NativeIfstream&& other) noexcept;
+
+ void swap(NativeIfstream& other) noexcept;
+
+private:
+ FileHandle m_handle; //!< Native file handle; may be owned or borrowed.
+ bool m_owns_handle; //!< True when the destructor is responsible for closing m_handle.
+ NativeStreamBuf m_buf; //!< Streambuf value member; Must be after m_handle and m_owns_handle.
+};
+
+} // namespace ov::util
diff --git a/src/common/util/include/openvino/util/native_streambuf.hpp b/src/common/util/include/openvino/util/native_streambuf.hpp
new file mode 100644
index 00000000000..d34cfad2013
--- /dev/null
+++ b/src/common/util/include/openvino/util/native_streambuf.hpp
@@ -0,0 +1,98 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+/**
+ * @brief A header file for a handle-based fast-read streambuf.
+ * @file native_streambuf.hpp
+ */
+
+#pragma once
+
+#include <cstddef>
+#include <streambuf>
+
+#include "openvino/util/file_util.hpp"
+
+namespace ov::util {
+
+/** @brief Default size of the internal window used to amortize small sequential reads. */
+inline constexpr size_t default_native_window = 8UL * 1024 * 1024;
+
+/** @brief Reads at least this large bypass the window and are read straight into the caller buffer. */
+inline constexpr size_t default_native_threshold = 2UL * 1024 * 1024;
+
+/**
+ * @brief A std::streambuf that reads from an already-open file handle using the platform-agnostic I/O backend.
+ *
+ * The buffer is decoupled from any file path: it only holds a (non-owning) file handle and serves reads through
+ * @ref io_read_into. Bulk reads (>= threshold) are passed straight to the backend into the caller's destination
+ * buffer, saturating fast storage without an intermediate copy. Smaller, char-by-char reads are amortized through
+ * a single internal window buffer.
+ *
+ * The handle is @b not owned by this object; the caller is responsible for keeping it open for the whole lifetime
+ * of the streambuf and for closing it afterwards.
+ *
+ * Usage:
+ * @code
+ * FileHandle handle = ...; // opened and sized by the caller
+ * std::streamoff size = ...; // readable bytes from blob_offset to the end of the region
+ * NativeStreamBuf buf(handle, blob_offset, size);
+ * std::istream stream(&buf);
+ * ib >> ...;
+ * @endcode
+ */
+class NativeStreamBuf : public std::streambuf {
+public:
+ /** @brief Constructs an empty streambuf not associated with any file. Any read immediately returns EOF. */
+ NativeStreamBuf() noexcept;
+
+ /**
+ * @param handle Open, readable file handle. Not owned; must outlive this object.
+ * Pass @c ov::invalid_handle only via the default constructor.
+ * @param offset Absolute file offset mapped to logical stream position 0. Must be >= 0.
+ * @param size Number of readable bytes starting at @p offset. Must be >= 0;
+ * pass 0 to create an empty (EOF-only) region. Caller is responsible for
+ * ensuring the range [@p offset, @p offset + @p size) lies
+ * within the file; out-of-range reads fail at the io_read_into level.
+ * @param window Size of the internal amortization window in bytes.
+ * @param threshold Minimum read size that bypasses the window and reads straight into the destination.
+ */
+ explicit NativeStreamBuf(FileHandle handle,
+ std::streamoff offset,
+ std::streamoff size,
+ size_t window = default_native_window,
+ size_t threshold = default_native_threshold);
+
+ ~NativeStreamBuf() override;
+
+ NativeStreamBuf(const NativeStreamBuf&) = delete;
+ NativeStreamBuf& operator=(const NativeStreamBuf&) = delete;
+
+ NativeStreamBuf(NativeStreamBuf&& other) noexcept;
+ NativeStreamBuf& operator=(NativeStreamBuf&& other) noexcept;
+ void swap(NativeStreamBuf& other) noexcept;
+
+protected:
+ std::streamsize xsgetn(char_type* dst, std::streamsize n) override;
+ int_type underflow() override;
+ pos_type seekoff(off_type off, std::ios_base::seekdir way, std::ios_base::openmode which) override;
+ pos_type seekpos(pos_type pos, std::ios_base::openmode which) override;
+ std::streamsize showmanyc() override;
+
+private:
+ bool allocate_window();
+ bool read_into(char* dst, size_t size, std::streamoff abs);
+ bool fill_window();
+
+ FileHandle m_handle; //!< Non-owning file handle; must remain open for the lifetime of this object.
+ std::streamoff m_cursor = 0; //!< Absolute offset of the next byte to be fetched.
+ std::streamoff m_start = 0; //!< Absolute offset of the region start; maps to logical stream position 0.
+ std::streamoff m_end = 0; //!< Absolute offset one past the last readable byte of the region.
+
+ size_t m_window_capacity = 0; //!< allocated capacity of m_window in bytes
+ size_t m_bypass_size = default_native_threshold; //!< minimum read size that bypasses the window
+ char* m_window = nullptr; //!< window buffer backing the get-area; null until first use
+};
+
+} // namespace ov::util
diff --git a/src/common/util/include/openvino/util/parallel_io.hpp b/src/common/util/include/openvino/util/parallel_io.hpp
index 8c6cd6fcc22..1911cf3b8d2 100644
--- a/src/common/util/include/openvino/util/parallel_io.hpp
+++ b/src/common/util/include/openvino/util/parallel_io.hpp
@@ -12,14 +12,11 @@
#include <cstddef>
#include <filesystem>
+#include "openvino/util/file_util.hpp"
#include "openvino/util/mmap_object.hpp"
namespace ov::util {
-#ifndef _WIN32
-inline constexpr FileHandle INVALID_HANDLE_VALUE = -1;
-#endif
-
inline constexpr size_t default_parallel_io_threshold = 4UL * 1024 * 1024; ///< 4 MB default threshold for parallel I/O
inline constexpr size_t default_parallel_io_min_chunk = 2UL * 1024 * 1024; ///< 2 MB minimum chunk size per thread
/** @brief Default upper bound for ParallelReadStreamBuf::prefetch() requests.
@@ -87,4 +84,4 @@ FileHandle open_file_for_read(const std::filesystem::path& path);
* @return true if all bytes were read successfully, false on I/O error.
*/
bool positional_read(FileHandle handle, char* dst, size_t size, size_t file_offset);
-} // namespace ov::util
\ No newline at end of file
+} // namespace ov::util
diff --git a/src/common/util/src/native_stream.cpp b/src/common/util/src/native_stream.cpp
new file mode 100644
index 00000000000..6ad7a2d9d4c
--- /dev/null
+++ b/src/common/util/src/native_stream.cpp
@@ -0,0 +1,69 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/util/native_stream.hpp"
+
+#include <utility>
+
+#include "openvino/util/file_util.hpp"
+#include "openvino/util/parallel_io.hpp"
+
+namespace ov::util {
+
+// Default constructor: stream not associated with any file; reads immediately return EOF.
+NativeIfstream::NativeIfstream() noexcept
+ : std::istream(nullptr),
+ m_handle(ov::invalid_handle),
+ m_owns_handle(false),
+ m_buf() {
+ this->init(&m_buf);
+}
+
+NativeIfstream::NativeIfstream(const std::filesystem::path& path)
+ : std::istream(nullptr),
+ m_handle(open_file(path, FileMode::READ | FileMode::DIRECT)),
+ m_owns_handle(m_handle != ov::invalid_handle),
+ m_buf(m_handle, 0, m_owns_handle ? static_cast<std::streamoff>(ov::util::file_size(path)) : 0) {
+ this->init(&m_buf);
+ if (!m_owns_handle)
+ setstate(std::ios::failbit);
+}
+
+NativeIfstream::NativeIfstream(FileHandle handle, std::streamoff offset, std::streamoff size)
+ : std::istream(nullptr),
+ m_handle(handle),
+ m_owns_handle(false),
+ m_buf(handle, offset, size) {
+ this->init(&m_buf);
+}
+
+NativeIfstream::NativeIfstream(NativeIfstream&& other) noexcept
+ : std::istream(std::move(other)),
+ m_handle(std::exchange(other.m_handle, ov::invalid_handle)),
+ m_owns_handle(std::exchange(other.m_owns_handle, false)),
+ m_buf(std::move(other.m_buf)) {
+ this->set_rdbuf(&m_buf);
+}
+
+NativeIfstream& NativeIfstream::operator=(NativeIfstream&& other) noexcept {
+ if (this != &other) {
+ swap(other); // old *this state (handle, buf) migrates to other; closed by other's destructor
+ }
+ return *this;
+}
+
+void NativeIfstream::swap(NativeIfstream& other) noexcept {
+ std::istream::swap(other);
+ std::swap(m_handle, other.m_handle);
+ std::swap(m_owns_handle, other.m_owns_handle);
+ m_buf.swap(other.m_buf);
+}
+
+NativeIfstream::~NativeIfstream() {
+ if (m_owns_handle) {
+ close_file_handle(m_handle);
+ }
+}
+
+} // namespace ov::util
diff --git a/src/common/util/src/native_streambuf.cpp b/src/common/util/src/native_streambuf.cpp
new file mode 100644
index 00000000000..881f73125f9
--- /dev/null
+++ b/src/common/util/src/native_streambuf.cpp
@@ -0,0 +1,213 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/util/native_streambuf.hpp"
+
+#include <algorithm>
+#include <cassert>
+#include <cstring>
+#include <utility>
+
+#include "openvino/util/memory.hpp"
+#include "openvino/util/parallel_io.hpp"
+
+namespace ov::util {
+
+namespace {
+// Window size must be a multiple of the system page size.
+constexpr size_t window_alignment = min_page_alignment;
+// Any caller-supplied window larger than max_window is silently clamped (e.g. a 16 GB value becomes 32 MiB).
+constexpr size_t max_window = 32UL * 1024 * 1024;
+} // namespace
+
+NativeStreamBuf::NativeStreamBuf(NativeStreamBuf&& other) noexcept
+ : std::streambuf(other), // copies get/put area pointers — they point into other.m_window, which we steal below
+ m_handle(std::exchange(other.m_handle, ov::invalid_handle)),
+ m_cursor(other.m_cursor),
+ m_start(other.m_start),
+ m_end(other.m_end),
+ m_window_capacity(std::exchange(other.m_window_capacity, 0)),
+ m_bypass_size(other.m_bypass_size),
+ m_window(std::exchange(other.m_window, nullptr)) {
+ other.setg(nullptr, nullptr, nullptr); // get-area still points into the stolen window — reset the source
+}
+
+NativeStreamBuf& NativeStreamBuf::operator=(NativeStreamBuf&& other) noexcept {
+ if (this != &other) {
+ swap(other); // old *this content migrates to other; freed by other's destructor
+ }
+ return *this;
+}
+
+void NativeStreamBuf::swap(NativeStreamBuf& other) noexcept {
+ std::streambuf::swap(other); // swaps get/put area pointers
+ std::swap(m_handle, other.m_handle);
+ std::swap(m_cursor, other.m_cursor);
+ std::swap(m_start, other.m_start);
+ std::swap(m_end, other.m_end);
+ std::swap(m_window_capacity, other.m_window_capacity);
+ std::swap(m_bypass_size, other.m_bypass_size);
+ std::swap(m_window, other.m_window);
+}
+
+NativeStreamBuf::NativeStreamBuf() noexcept : NativeStreamBuf(ov::invalid_handle, 0, 0) {}
+
+NativeStreamBuf::NativeStreamBuf(FileHandle handle,
+ std::streamoff offset,
+ std::streamoff size,
+ size_t window,
+ size_t threshold)
+ : m_handle(handle),
+ m_cursor(offset),
+ m_start(offset),
+ m_end(offset + size),
+ m_window_capacity(align_size_up(std::clamp(window, window_alignment, max_window), window_alignment)),
+ m_bypass_size(threshold) {
+ assert(offset >= 0);
+ assert(size >= 0);
+}
+
+NativeStreamBuf::~NativeStreamBuf() {
+ aligned_free(m_window);
+}
+
+bool NativeStreamBuf::allocate_window() {
+ if (m_window == nullptr) {
+ m_window = static_cast<char*>(aligned_alloc(m_window_capacity, window_alignment));
+ }
+ return m_window != nullptr;
+}
+
+bool NativeStreamBuf::read_into(char* /* dst */, size_t /* size */, std::streamoff /* abs */) {
+ // TODO(CVS-186707): io_read_into backend not yet implemented
+ return false;
+}
+
+// Refill the get-area (window) with up to m_window_capacity bytes starting at m_cursor.
+bool NativeStreamBuf::fill_window() {
+ // CVS-189123
+ if (m_cursor >= m_end || !allocate_window()) {
+ return false;
+ }
+ const size_t to_read =
+ static_cast<size_t>(std::min(static_cast<std::streamoff>(m_window_capacity), m_end - m_cursor));
+ if (!read_into(m_window, to_read, m_cursor)) {
+ return false;
+ }
+ setg(m_window, m_window, m_window + to_read);
+ m_cursor += static_cast<std::streamoff>(to_read);
+ return true;
+}
+
+// xsgetn: main bulk-read path. Large requests bypass the window; small requests are amortized through it.
+std::streamsize NativeStreamBuf::xsgetn(char_type* dst, std::streamsize n) {
+ // CVS-189123
+ if (n <= 0) {
+ return 0;
+ }
+
+ std::streamsize total = 0;
+ while (n > 0) {
+ // Drain whatever is already buffered in the get-area.
+ if (gptr() != nullptr && gptr() < egptr()) {
+ const std::streamsize avail = static_cast<std::streamsize>(egptr() - gptr());
+ const std::streamsize from_buf = std::min(n, avail);
+ std::memcpy(dst, gptr(), static_cast<size_t>(from_buf));
+ gbump(static_cast<int>(from_buf)); // from_buf <= m_window_capacity <= INT_MAX
+ dst += from_buf;
+ n -= from_buf;
+ total += from_buf;
+ continue;
+ }
+
+ if (m_cursor >= m_end) {
+ break;
+ }
+
+ const std::streamoff remaining = m_end - m_cursor;
+ const std::streamsize want = static_cast<std::streamsize>(std::min(static_cast<std::streamoff>(n), remaining));
+
+ if (static_cast<size_t>(want) >= m_bypass_size) {
+ // Bypass the window: read straight into the caller's buffer.
+ if (!read_into(dst, static_cast<size_t>(want), m_cursor)) {
+ break;
+ }
+ m_cursor += want;
+ dst += want;
+ n -= want;
+ total += want;
+ } else if (!fill_window()) {
+ // Refill the window and loop back to drain it; stop on read failure.
+ break;
+ }
+ }
+
+ return total;
+}
+
+// underflow: char-by-char / peek path (operator>>, std::getline).
+NativeStreamBuf::int_type NativeStreamBuf::underflow() {
+ // CVS-189123
+ if (gptr() != nullptr && gptr() < egptr()) {
+ return traits_type::to_int_type(*gptr());
+ }
+ if (!fill_window()) {
+ return traits_type::eof();
+ }
+ return traits_type::to_int_type(*gptr());
+}
+
+NativeStreamBuf::pos_type NativeStreamBuf::seekoff(off_type off,
+ std::ios_base::seekdir way,
+ std::ios_base::openmode /* which */) {
+ // CVS-189123
+ // Internal offsets are absolute; public stream positions are logical (0 == m_start).
+ std::streamoff new_pos = 0;
+ if (way == std::ios_base::beg) {
+ new_pos = m_start + off;
+ } else if (way == std::ios_base::cur) {
+ // Account for bytes still buffered in the get-area ahead of the logical cursor.
+ const std::streamoff ahead = (gptr() != nullptr) ? static_cast<std::streamoff>(egptr() - gptr()) : 0;
+ new_pos = m_cursor - ahead + off;
+ if (off == 0) {
+ // Pure tell: report position without disturbing the get-area.
+ if (new_pos < m_start || new_pos > m_end) {
+ return pos_type(off_type(-1));
+ }
+ return pos_type(new_pos - m_start);
+ }
+ } else {
+ new_pos = m_end + off;
+ }
+
+ if (new_pos < m_start || new_pos > m_end) {
+ return pos_type(off_type(-1));
+ }
+
+ setg(nullptr, nullptr, nullptr); // drop the get-area; the next read refills from new_pos
+ m_cursor = new_pos;
+ return pos_type(new_pos - m_start);
+}
+
+NativeStreamBuf::pos_type NativeStreamBuf::seekpos(pos_type pos, std::ios_base::openmode /* which */) {
+ // CVS-189123
+ return seekoff(off_type(pos), std::ios_base::beg, std::ios_base::in);
+}
+
+std::streamsize NativeStreamBuf::showmanyc() {
+ // CVS-189123
+ std::streamsize buffered = 0;
+ if (gptr() != nullptr && egptr() > gptr()) {
+ buffered = static_cast<std::streamsize>(egptr() - gptr());
+ }
+ std::streamoff remaining_off = m_end - m_cursor;
+ if (remaining_off < 0) {
+ remaining_off = 0;
+ }
+ const std::streamsize total = buffered + static_cast<std::streamsize>(remaining_off);
+ // Per [streambuf.virt.get]: -1 signals that underflow() would return eof().
+ return total > 0 ? total : static_cast<std::streamsize>(-1);
+}
+
+} // namespace ov::util
diff --git a/src/common/util/src/os/lin/lin_file_util.cpp b/src/common/util/src/os/lin/lin_file_util.cpp
new file mode 100644
index 00000000000..e8a93271f47
--- /dev/null
+++ b/src/common/util/src/os/lin/lin_file_util.cpp
@@ -0,0 +1,43 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <fcntl.h>
+#include <unistd.h>
+#ifdef __APPLE__
+# include <sys/fcntl.h>
+#endif
+
+#include "openvino/util/file_util.hpp"
+
+namespace ov::util {
+
+FileHandle open_file(const std::filesystem::path& path, FileMode mode) {
+ // CVS-189123
+ int flags = O_CLOEXEC;
+ if (mode_set(mode, FileMode::READ)) {
+ flags |= O_RDONLY;
+ }
+#if defined(O_DIRECT)
+ if (mode_set(mode, FileMode::DIRECT)) {
+ flags |= O_DIRECT;
+ }
+#endif
+ FileHandle fd = ::open(path.c_str(), flags);
+#if defined(__APPLE__) && defined(F_NOCACHE)
+ // macOS has no O_DIRECT; use F_NOCACHE to disable the unified buffer cache.
+ if (fd != invalid_handle && mode_set(mode, FileMode::DIRECT)) {
+ ::fcntl(fd, F_NOCACHE, 1);
+ }
+#endif
+ return fd;
+}
+
+void close_file(FileHandle handle) {
+ // CVS-189123
+ if (handle != invalid_handle) {
+ ::close(handle);
+ }
+}
+
+} // namespace ov::util
diff --git a/src/common/util/src/os/win/win_file_util.cpp b/src/common/util/src/os/win/win_file_util.cpp
new file mode 100644
index 00000000000..2f8aa6adbed
--- /dev/null
+++ b/src/common/util/src/os/win/win_file_util.cpp
@@ -0,0 +1,44 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/util/file_util.hpp"
+
+// clang-format off
+#ifndef NOMINMAX
+# define NOMINMAX
+#endif
+#include <windows.h>
+// clang-format on
+
+namespace ov::util {
+
+FileHandle open_file(const std::filesystem::path& path, FileMode mode) {
+ // CVS-189123
+ DWORD desired_access = 0;
+ DWORD flags_and_attrs = FILE_ATTRIBUTE_NORMAL;
+
+ if (mode_set(mode, FileMode::READ)) {
+ desired_access |= GENERIC_READ;
+ }
+ if (mode_set(mode, FileMode::DIRECT)) {
+ flags_and_attrs |= FILE_FLAG_NO_BUFFERING;
+ }
+
+ return CreateFileW(path.native().c_str(),
+ desired_access,
+ FILE_SHARE_READ | FILE_SHARE_WRITE,
+ nullptr,
+ OPEN_EXISTING,
+ flags_and_attrs,
+ nullptr);
+}
+
+void close_file(FileHandle handle) {
+ // CVS-189123
+ if (handle != INVALID_HANDLE_VALUE) {
+ CloseHandle(handle);
+ }
+}
+
+} // namespace ov::util
diff --git a/src/common/util/src/parallel_read_streambuf.cpp b/src/common/util/src/parallel_read_streambuf.cpp
index 3d3bfbafdad..51021933105 100644
--- a/src/common/util/src/parallel_read_streambuf.cpp
+++ b/src/common/util/src/parallel_read_streambuf.cpp
@@ -312,7 +312,7 @@ bool ParallelReadStreamBuf::parallel_read(char* dst, size_t size, size_t file_of
const size_t thread_file_offset = file_offset + cur_offset;
FileHandle t_handle = open_file_for_read(m_path);
- if (t_handle == INVALID_HANDLE_VALUE) {
+ if (t_handle == ov::invalid_handle) {
success = false;
return;
}