问题标签 [memorystream]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
839 浏览

c# - How do I store arbitrary binary data in a binary serialized class?

Using C#/.NET for my application, I've got a series of classes in my main data model that represent "binary" (as opposed to text) content. I've got a inheritance setup like this:

alt text

Basically, the abstract class BinaryContent contains a MemoryStream that stores arbitrary binary data. That data is read from a file on disk. Each type of binary data I plan to store will be a derived type, such as ImageContent and FontContent. Those derived types will interpret the binary data in BinaryContent.Content. ImageContent for example, would create a BitmapImage (stored in an ImageSource) from the MemoryStream. FontContent would of course create a font from the BinaryContent.Content. I chose this way of doing things because I wanted to be able to basically store a copy of the content file (ie an image) and not have to rely on the file being in a particular location on disk time after time.

I'm also storing instances of these classes in a "project file" using binary serialization. I've done this to basically "package" everything together. I'm having trouble when I attempt to deserialize the MemoryStream, it seems. The problem happens when I create the image from the MemoryStream. When the following method runs after deserialization, a FileFormatexception occurs.

The FileFormatException message is: "The image cannot be decoded. The image header might be corrupted." Inner Exception: "Exception from HRESULT: 0x88982F61"

My best guess right now is that something is happening to corrupt the data in BinaryContent.Content when during serialization or deserialization.

This leads me to ask 2 questions.

  1. Does anyone have any suggestions to fix this problem?
  2. Does anyone have other suggested ways to store arbitrary binary data that is going to be (de)serialized?

Please feel free to ask for clarification on anything about my question.

Thanks.

0 投票
2 回答
8365 浏览

c# - ByteArray 数组到 MemoryStream

我有一个返回 ByteArray 数组的方法:

我需要把它写成MemoryStream

到目前为止,这是有效的,因为 byteArray 数组只有一个元素。有人可以指出并提供解决方案:如果 byteArray 的数组有多个元素会发生什么?

我想使用当前代码我仍然会采用 byteArray 的第一个元素并丢弃其余的元素,但我需要MemoryStream并且它不能采用多维数组。

0 投票
4 回答
1703 浏览

c# - C# 通过异步套接字连接发送具有序列化对象的对象大小

我想序列化一个对象并通过网络发送它。我已经在我的类和 BinaryFormatter 上使用 ISerializeable 属性设置它以将对象转换为字节。我可以发送对象并在接收端反序列化它。但是,为了确保在尝试重建对象之前拥有整个对象,我想将大小与流一起发送。我想将前几个字节设置为大小,检查接收到的数据何时至少是这个固定大小,然后我可以读取它并获取对象的完整大小。然后,只需等到我收到的数据是对象的大小+固定大小的字节。如何在流中偏移我的数据,以便我可以发送一个 int 将对象的大小存储为前几个字节,并将我的对象存储为剩余字节?

0 投票
2 回答
1906 浏览

c# - 将本机缓冲区转换为 MemoryStream

在我的 C++\CLI 我有这段代码:

MemoryStream(out_buf),内存流是再次分配内存还是仅仅获得所有权out_buf

如果MemoryStream确实再次分配内存,有没有办法将本机缓冲区转换为MemoryStream

0 投票
4 回答
3161 浏览

.net - MemoryStream 必须明确处理?

由于 MemoryStream 是非托管资源,它是否总是必须被释放?

鉴于:

MemoryStream 对象上的引用因此丢失。这种情况是否需要 try/finally 块(或 using 语句)?

0 投票
3 回答
349 浏览

c# - Best way to represent text in memory

I have 2 classes that inherit from a common base class.

Each of these specialized classes load some data from a data base, process it and then save that information in text files.

The first class represents the information as a XML document. The second class will store its information as a text file with delimiters separating fields.

What I want to do is to write a single Save method in the base class that can be used by both classes. As all classes will write to text files I was thinking in use a common representation to store their data in memory - for instance the first class will transform the XmlDocument to that common representation.

What is the best way to store this in memory, string, Stream?

Thanks

0 投票
2 回答
5062 浏览

c# - Python 中的 MemoryStream 模拟

Python 中是否存在一些 C# 的类似物MemoryStream(这可以让我将二进制数据从某个源直接写入内存)?我将如何使用它?

0 投票
5 回答
2637 浏览

c# - 当函数可以返回 MemoryStream 时,为什么我不能声明 MemoryStream 可为空(MemoryStream?)?

我有一个返回的函数MemoryStream?。如果发生错误,则为 null。然后我发现我不能声明一个变量MemoryStream?

类型“System.IO.MemoryStream”必须是不可为空的值类型,才能将其用作泛型类型或方法“System.Nullable”中的参数“T”

0 投票
1 回答
4750 浏览

c# - C# BinaryReader.Read() 开始变得垃圾

我想弄清楚我在这里做错了什么。我正在尝试使用二进制读取器来轻松地将流中的初始四个字节转换为 Int32 值,该值告诉我剩余数据的预期时间。

输出应该是输入,但我明白了(注意第一个字符从字符串变为字符串)

(想象这是一个非常长的字符串

有人可以向我解释我做错了什么吗?理想情况下,第二次读取将继续,直到总读取字节数等于前四个字节的值。这段代码只是为了显示我遇到的问题的简化。流的位置似乎是正确的 (4),但它几乎似乎从 2 开始读取。

0 投票
3 回答
1306 浏览

c++ - 如何从内存中读取,就像使用 wistream 从文件中读取一样?

在我之前的问题中,我询问了如何从文件中读取内存。因为我的整个文件都在内存中,所以我想类似地阅读它。

我找到了问题的答案,但实际上我需要将行读为wstring. 使用文件我可以这样做:

即使文件是 ASCII 格式。

现在我只是用这个答案中的一个函数来改变我的string行。但是,我认为如果有一种方法可以像处理这块内存一样,那么将这些行设置为s 将是一个更快的解决方案。我需要这个速度很快。wstringwistreamwstring

所以有人知道如何把这块内存当作wistream?