[英]C# FlatBufferBuilder create String from Stream
Suppose you need to read a large string from a stream and you want to put that string into a flatbuffer.假设您需要从流中读取一个大字符串,并且您想将该字符串放入一个 flatbuffer 中。
Currently what I do is read the stream into a string and then use the FlatbufferBuilder.CreateString(string s) function.目前我所做的是将流读入一个字符串,然后使用 FlatbufferBuilder.CreateString(string s) 函数。
This works fine but it does have as a drawback that the string is copied and loaded into memory twice: once by reading it from the stream into the string;这工作正常,但它确实有一个缺点,即字符串被复制并加载到内存中两次:一次是通过将它从流中读取到字符串中; and then a second time the string is copied into the flatbuffer.
然后第二次将字符串复制到 flatbuffer 中。
I was wondering if there is a way to fill the flatbuffer string directly from a stream?我想知道是否有办法直接从流中填充 flatbuffer 字符串?
For a more concrete example: Suppose your flatbuffer schema looks like:对于更具体的示例:假设您的 flatbuffer 架构如下所示:
table Message
{
_Data: string;
}
root_type Message;
We can then create a flatbuffer like this (with myData a string)然后我们可以像这样创建一个 flatbuffer(用 myData 一个字符串)
var fbb = new FlatBufferBuilder(myData.Length);
var dataOffset = fbb.CreateString(myData);
var message = Message.CreateMessage(fbb, dataOffset);
Message.FinishMessageBuffer(fbb, message);
So the question is can we somehow do the same thing, where myData is a System.IO.Stream?所以问题是我们能否以某种方式做同样的事情,其中 myData 是 System.IO.Stream?
Obviously the following works, but I'd like to avoid first reading the Stream into memory.显然以下有效,但我想避免首先将 Stream 读入内存。
using (var reader = new StreamReader(myStream)
{
var myData = reader.ReadToEnd();
var fbb = new FlatBufferBuilder(myData.Length);
var dataOffset = fbb.CreateString(myData);
var message = Message.CreateMessage(fbb, dataOffset);
Message.FinishMessageBuffer(fbb, message);
}
There is currently no way to avoid that copy twice, afaik.. it should be relatively simple to implement a version of CreateString
that takes a stream and reduces it to one copy.目前没有办法避免该副本两次,afaik..实现一个版本的
CreateString
应该相对简单,该版本接受一个流并将其减少到一个副本。 You could have a go at that and open a PR on github with the result.你可以尝试一下,然后在 github 上打开一个 PR 并得到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.