memorystream

Asynchronous memory streaming approach: which of the following?

做~自己de王妃 提交于 2019-12-05 10:56:54
I am working on solution which uses asynchronous memory streaming and I am thinking about right approach for implementing such. Which one is more convenient? The first, simple: //First approach: linear async private async static Task WriteToStreamFirstVariant() { MemoryStream memoryStream = new MemoryStream(); byte[] data = new byte[256]; try { await memoryStream.WriteAsync(data, 0, data.Length); } catch(Exception exception) { //Handling exception } finally { memoryStream.Dispose(); } } Or the second with nested tasks and closures? //Second approach: nested tasks async private async static

How to copy one file to many locations simultaneously

别等时光非礼了梦想. 提交于 2019-12-05 09:53:21
I want to find a way to copy one file to multiple locations simultaneously (with C#). means that i don't want the original file to be read only one time, and to "paste" the file to another locations (on local network). as far as my tests showed me, the File.Copy() will always read the source again. and as far as i understand, even while using memory, that memory piece gets locked. so basically, i want to mimic the "copy-paste" to the form of one "copy", and multiple "paste", without re-reading from the Hard Drive again. Why ? because eventually, i need to copy one folder (more than 1GB) to

Writing to MemoryStream with StreamWriter returns empty

房东的猫 提交于 2019-12-05 09:14:01
问题 I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working. public static Stream Foo() { var memStream = new MemoryStream(); var streamWriter = new StreamWriter(memStream); for (int i = 0; i < 6; i++) streamWriter.WriteLine("TEST"); memStream.Seek(0, SeekOrigin.Begin); return memStream; } I am doing a simple test on this method to try and get it to pass, but no matter what, my collection count is 0. [Test] public void TestStreamRowCount() { var stream

different thread accessing MemoryStream

左心房为你撑大大i 提交于 2019-12-05 08:22:36
There's a bit of code which writes data to a MemoryStream object directly into it's data buffer by calling GetBuffer(). It also uses and updates the Position and SetLength() properties appropriately. This code works properly 99.9999% of the time. Literally. Only every so many 100,000's of iterations it will barf. The specific problem is that the Position property of MemoryStream suddenly returns zero instead of the appropriate value. However, code was added that checks for the 0 and throws an exception which includes log of the MemoryStream properties like Position and Length in a separate

How to remove data from a MemoryStream

若如初见. 提交于 2019-12-05 07:21:50
I cannot get this to work. I have a MemoryStream object. This class has a Position property that tells you how many bytes you have read. What I want to do is to delete all the bytes between 0 and Position-1 I tried this: MemoryStream ms = ... ms.SetLength(ms.Length - ms.Position); but at some point my data gets corrupted. So I ended up doing this MemoryStream ms = ... byte[] rest = new byte[ms.Length - ms.Position]; ms.Read(rest, 0, (int)(ms.Length - ms.Position)); ms.Dispose(); ms = new MemoryStream(); ms.Write(rest, 0, rest.Length); which works but is not really efficient. Any ideas how I

How to Merge two memory streams containing PDF file's data into one

核能气质少年 提交于 2019-12-05 05:56:13
I am trying to read two PDF files into two memory streams and then return a stream that will have both stream's data. But I don't seem to understand what's wrong with my code. Sample Code: string file1Path = "Sampl1.pdf"; string file2Path = "Sample2.pdf"; MemoryStream stream1 = new MemoryStream(File.ReadAllBytes(file1Path)); MemoryStream stream2 = new MemoryStream(File.ReadAllBytes(file2Path)); stream1.Position = 0; stream1.Copyto(stream2); return stream2; /*supposed to be containing data of both stream1 and stream2 but contains data of stream1 only*/ ArslanIqbal It appears in case of PDF

want to re-use MemoryStream

跟風遠走 提交于 2019-12-05 05:19:36
My code uses MemoryStream to serialize/deserialize objects to/from the network. I would like to re-use a single MemoryStream in my class, rather than create a new one each time I need to send something over the wire. Does anyone know how to do this? Code snippet: // Serialize object to buffer public byte[] Serialize(object value) { if (value == null) return null; MemoryStream _memoryStream = new MemoryStream(); _memoryStream.Seek(0, 0); _bf.Serialize(_memoryStream, value); return _memoryStream.GetBuffer(); } // Deserialize buffer to object public object Deserialize(byte[] someBytes) { if

C++ FILE without writing to disk

[亡魂溺海] 提交于 2019-12-05 04:35:58
I'm using a library that has quite a few functions that write to a FILE but none that seem to conveniently dump the same data to an object in memory. Is there any way to create a FILE object (or override it) that stores the data in memory instead of writing to disk -- I'd like to avoid the performance hit of opening/writing/reading from files over and over again. UPDATE: per Rob's suggestion, trying stringstream: ss.put(c); std::string myval = ss.str(); printf("Value: %s\n after writing: %i length %lu\n",myval.c_str(),c, myval.length()); But, now trying to get the data (binary) out of the

MemoryStream.WriteTo(Stream destinationStream) versus Stream.CopyTo(Stream destinationStream)

末鹿安然 提交于 2019-12-05 03:25:16
Which one is better : MemoryStream.WriteTo(Stream destinationStream) or Stream.CopyTo(Stream destinationStream) ?? I am talking about the comparison of these two methods without Buffer as I am doing like this : Stream str = File.Open("SomeFile.file"); MemoryStream mstr = new MemoryStream(File.ReadAllBytes("SomeFile.file")); using(var Ms = File.Create("NewFile.file", 8 * 1024)) { str.CopyTo(Ms) or mstr.WriteTo(Ms);// Which one will be better?? } Update Here is what I want to Do : Open File [ Say "X" Type File] Parse the Contents From here I get a Bunch of new Streams [ 3 ~ 4 Files ] Parse One

How do I “fork” a Stream in .NET?

喜欢而已 提交于 2019-12-05 02:59:53
As discussed before , when a BinaryReader or BinaryWriter gets closed, its underlying Stream get closed as well (aargh). Consider this situation: a routine R is passed a MemoryStream, say M ; I would like to write some stuff to M and then pass it to another routine for more processing (not necessarily writing). For convenience, I'd like to wrap M in a BinaryWriter to do my writing. After writing, I'm done with the BinaryWriter but not with M . void R(MemoryStream M) { using (B = new BinaryWriter(M)) { // write some stuff using B } S(M); // now pass M to another routine for further processing }