如何将 QByteArray 转换为 std::istream 或 std::ifstream?

How to convert QByteArray to std::istream or std::ifstream?

我想在运行时从 QByteArray 创建 istream,而不在 QByteArray 的内存中保存物理文件。

我发现有很多方法可以进行相反的转换,即 istreamQByteArray,但不是这个。

如何实现?

通过 std::istringstreamQByteArray 阅读似乎很容易:

testQByteArray-istream.cc:

#include <iostream>
#include <sstream>
#include <QtCore>

int main()
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  // make a QByteArray
  QByteArray data("Hello Qt World.");
  // convert to std::string
  std::istringstream in(data.toStdString());
  // read from istringstream
  for (;;) {
    std::string buffer;
    if (!std::getline(in, buffer)) break;
    std::cout << "Got: '" << buffer << "'\n";
  }
  // done
  return 0;
}

testQByteArray-istream.pro:

SOURCES = testQByteArray-istream.cc

QT = core

cygwin64 上编译和测试:

$ qmake-qt5 testQByteArray-istream.pro

$ make

$ ./testQByteArray-istream 
Qt Version: 5.9.4
Got: 'Hello Qt World.'

$

完成。停下,等等!

without saving a physical file in memory

我不太确定如何阅读这篇文章。大概意思是

不复制保存在QByteArray

中的数据

我只看到两个解决方案:

  1. 使用 QDataStream 而不是 std::stream。根据文件。 QDataStream::QDataStream(const QByteArray &a)

    Constructs a read-only data stream that operates on byte array a.

    这听起来非常保证不会复制数据。

  2. 自己动手做。从 std::stream 派生一个 class,它可以从 QByteArray 读取而无需复制。

关于选项 2,我找到了 Dietmar Kühl 对 SO: Creating an input stream from constant memory 的回答。将其应用于上面的示例,它看起来像这样:

#include <iostream>
#include <QtCore>

// borrowed from 
struct membuf: std::streambuf {
  membuf(char const* base, size_t size) {
    char* p(const_cast<char*>(base));
    this->setg(p, p, p + size);
  }
};
struct imemstream: virtual membuf, std::istream {
  imemstream(char const *base, size_t size):
    membuf(base, size),
    std::istream(static_cast<std::streambuf*>(this)) {
  }
};

int main()
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  // make a QByteArray
  QByteArray data("Hello Qt World.");  
  imemstream in(data.data(), (size_t)data.size());
  // read from istringstream
  for (;;) {
    std::string buffer;
    if (!std::getline(in, buffer)) break;
    std::cout << "Got: '" << buffer << "'\n";
  }
  // done
  return 0;
}

cygwin64 上再次编译和测试:

$ qmake-qt5 testQByteArray-istream.pro

$ make

$ ./testQByteArray-istream 
Qt Version: 5.9.4
Got: 'Hello Qt World.'

$