如何解决basic_streambuf<char> 到 uint8_t*
我们有一个存在于数据存储 (S3) 中的文件,其中包含 byte[] 形式的数据(使用 Java 语言上传)。
现在当我下载文件时,我得到的数据是 std::basic_streambuf 的形式(理想情况下它也应该有字节)。现在我想将此数据发送到另一个以 uint8_t* 作为输入的 API。
这样做的方法是什么?这样做有意义吗?
我试过了:
// Assume streambuf is:
std::streambuf *buf;
std::stringstream ss;
ss << buf;
// Solution1
const std::string output1 = ss.str();
cout<<output1;
// This prints the whole data with some weird characters (i think weird characters are valid because data is in byte form). Upon converting output1 to uint8_t*,the final data contains only 20 characters/bytes.
// Solution2
uint8_t* finalString;
ss >> finalString;
cout<<finalString;
// This prints only initial 20 characters/bytes and the remaining part is skipped.
因此,对于解决方案 1 和解决方案 2,无法实现获得完整数据的 uint8_t* 的最终目标。建议的方法是什么?
解决方法
您必须从缓冲区中读取数据(因为缓冲区本身可以在数据可用时将其流式传输)。一种可能的实现是这样的:
vector<uint8_t> bytes;
do {
bytes.push_back(buf->sgetc());
} while(buf->snextc() != EOF);
// your data is in bytes.data() of type uint8_t*
当然,如果您从一开始就知道字节数,而不必读取缓冲区来查找,只需预先预先分配向量即可。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。