Comments (5)
Your sample seems to have worked on my end.
I would like you to confirm the following
await fs.ReadAsync(buffer, 0, buffer.Length);
Here, is ReadAsync
reading buffer.Length correctly?
This third argument is the "maximum number of bytes". So it is possible that ReadAsync cannot read the specified number of bytes at a time.
I think you need to check the return value of ReadAsync and read repeatedly if there are not enough bytes to read. For example, as follows.
var readBytes = 0;
while (readBytes < length)
{
readBytes += await fs.ReadAsync(buffer, 0, buffer.Length);
}
If the problem persists, please let us know the environment in which you ran each serialization and deserialization. Thanks.
from memorypack.
Hi, thanks for the reply. I tried this and sadly it was reading always the full size, but implemented it anyway. I made a small test project with a sample file, what causes issues. Hope that makes it easier to replicate.
And I am running this on Windows 10 Pro 64
Microsoft Visual Studio Community 2022 (64-bit) - Current
Version 17.9.2
from memorypack.
Thanks a lot for the reproduced code.
Apparently the problem is in BrotliCompressor.CopyToAsync
.
If you pass a very large MemoryStream to CopyToAsync at once, there seems to be a problem with it not being written all the way through.
In your code, it would be as follows.
static async Task SaveLogfile(TradeLog log, MemoryStream ms, CompressionLevel compressionLevel) {
using (var cp = new BrotliCompressor(compressionLevel)) {
MemoryPackSerializer.Serialize(cp, log);
await cp.CopyToAsync(ms);
}
await ms.FlushAsync(); }
}
I will attempt to fix this in #249.
NOTE:
Since the buffer size can be specified in the second argument of CopyToAsync, I think this problem can be avoided by enlarging it.
By the way, I would like to recommend the following points:
If you want to serialize in memory one at a time, don't use Stream. Stream support is primarily for I/O to output to, such as File.
The above code could use IBufferWriter instead.
var buffer = new ArrayBufferWriter<byte>(inputBytes.Length);
compressor.CopyTo(buffer);
from memorypack.
This fix is released in 1.20.1.
Please let us know if there is anything else. Thanks.
from memorypack.
Thanks a lot for the reproduced code. Apparently the problem is in
BrotliCompressor.CopyToAsync
.If you pass a very large MemoryStream to CopyToAsync at once, there seems to be a problem with it not being written all the way through.
In your code, it would be as follows.
static async Task SaveLogfile(TradeLog log, MemoryStream ms, CompressionLevel compressionLevel) { using (var cp = new BrotliCompressor(compressionLevel)) { MemoryPackSerializer.Serialize(cp, log); await cp.CopyToAsync(ms); } await ms.FlushAsync(); } }I will attempt to fix this in #249.
NOTE: Since the buffer size can be specified in the second argument of CopyToAsync, I think this problem can be avoided by enlarging it.
By the way, I would like to recommend the following points: If you want to serialize in memory one at a time, don't use Stream. Stream support is primarily for I/O to output to, such as File.
The above code could use IBufferWriter instead.
var buffer = new ArrayBufferWriter<byte>(inputBytes.Length); compressor.CopyTo(buffer);
Hi thanks for the fix, usually i write it to a file, I just made it to a memorystream so you dont have to wear off your ssd when testing it. I hope this comment doesnt reopen the issue now. I am not really familiar with github.
from memorypack.
Related Issues (20)
- SerializationException: Sequence reached end, cannot provide more buffer HOT 2
- generator not work on other class
- Any hope for supporting netstandard2.0? HOT 1
- Serialize in Roslyn. HOT 5
- How do I use MemoryPack inside a source generator? HOT 2
- Support for signalR HOT 3
- [Unity 6 Beta] "~MemoryPack.Generator.dll' will not be loaded... HOT 1
- Can't Install With githubUrl in Unity HOT 6
- error cs0111, cs0579 HOT 2
- Unity Compile Error After Install MemoryPack.UnityShims
- How to define custom collection with shared property member HOT 1
- Is it possible to serialize a collection with IConvertible as keys?
- Can i get some help understanding the results of this benchmark ?
- Is it possible to utilise MemoryPack to compare serialized data of the same type?
- [Question] Why are unions only supported for class and not struct, and is there a work around?
- POCO structs are treated as unmanaged despite GenerateType.VersionTolerant HOT 2
- Platform implement
- Support WebGL?
- Run In Roslyn
- Unions generate code that is not C#7 compliant HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memorypack.