The code below tries to fill the message byte array with some simple text until the buffer is filled.
byte[] message = new byte[1024];
using (MemoryStream memoryStream = new MemoryStream(message, true))
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
{
while (???)
streamWriter.WriteLine("Hello World!");
}
}
What should be in the while(???) statement?
byte[] message = new byte[1024];
using (MemoryStream memoryStream = new MemoryStream(message, true))
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
{
var lineToAdd = "Hello World!";
while (memoryStream.Length - memoryStream.Position > lineToAdd.Length)
{
streamWriter.WriteLine(lineToAdd);
streamWriter.Flush();
}
}
}
OR
using (MemoryStream memoryStream = new MemoryStream(message, true))
using (StreamWriter streamWriter = new StreamWriter(memoryStream, Encoding.ASCII))
{
streamWriter.AutoFlush = true;
var lineToAdd = "Hello World!";
while (memoryStream.Length - memoryStream.Position > lineToAdd.Length)
streamWriter.WriteLine(lineToAdd);
}
Related
How do I convert from a gzip compressed file to Zstandard? Here is my attempt:
using Zstandard.Net;
public static void ConvertToZstd(string path)
{
byte[] compressed = null;
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
{
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
compressionStream.Write(gzipStream, 0, gzipStream.Length);
compressionStream.Close();
compressed = memoryStream.ToArray();
}
}
public static void ConvertToZstd(string path, string outputPath)
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var gzipStream = new GZipStream(stream, CompressionMode.Decompress, false))
using (var memoryStream = new MemoryStream())
using (var compressionStream = new ZstandardStream(memoryStream, CompressionMode.Compress))
{
compressionStream.CompressionLevel = 11; // optional!!
//compressionStream.CompressionDictionary = dictionary; // optional!!
gzipStream.CopyTo(compressionStream);
compressionStream.Close();
File.WriteAllBytes(outputPath, memoryStream.ToArray());
}
}
Hi I am trying to make working this piece of code, after the copy of the word file template into a memory stream, read it and replace some text, I would convert the stream writer to byte array which will be used to download the result. Thanks in advance
public byte[] GetWordFile()
{
try
{
string sourceFile = Path.Combine("C:/[...]/somefile.docx");
using (MemoryStream inStream = new MemoryStream())
{
using (Stream fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read))
{
fs.CopyTo(inStream);
}
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(inStream, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
docText = docText.Replace("numpol", "HAHAHHAHA");
using (MemoryStream outStream = new MemoryStream())
using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
sw.Flush();
sw.BaseStream.CopyTo(outStream);
outStream.Position = 0;
return outStream.ToArray();
}
}
}
}
catch (Exception ex)
{
///...
}
}
can someone tell me why I'm loosing information doing this process ? Some utf8 chars appears not decoded :
"Biography":"\u003clink type=... or Steve Blunt \u0026 Marty Kelley
but others do : "Name":"朱敬
// Creating a 64bit string containing gzip data
string bar;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress))
using (StreamWriter writer = new StreamWriter(gzip, System.Text.Encoding.UTF8))
{
writer.Write(s);
}
ms.Flush();
bar = Convert.ToBase64String(ms.ToArray());
}
// Reading it
string foo;
byte[] itemData = Convert.FromBase64String(bar);
using (MemoryStream src = new MemoryStream(itemData))
using (GZipStream gzs = new GZipStream(src, CompressionMode.Decompress))
using (MemoryStream dest = new MemoryStream(itemData.Length*2))
{
gzs.CopyTo(dest);
foo = Encoding.UTF8.GetString(dest.ToArray());
}
Console.WriteLine(foo);
It could be because you are writing the string using StreamWriter but reading it using CopyTo() and Encoding.GetString().
What happens if you try this?
// Reading it
string foo;
byte[] itemData = Convert.FromBase64String(bar);
using (MemoryStream src = new MemoryStream(itemData))
using (GZipStream gzs = new GZipStream(src, CompressionMode.Decompress))
using (StreamReader reader = new StreamReader(gzs, Encoding.UTF8))
{
foo = reader.ReadLine();
}
Although I think you should be using BinaryReader and BinaryWriter:
string s = "Biography:\u003clink type...";
string bar;
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(ms, CompressionMode.Compress))
using (var writer = new BinaryWriter(gzip, Encoding.UTF8))
{
writer.Write(s);
}
ms.Flush();
bar = Convert.ToBase64String(ms.ToArray());
}
// Reading it
string foo;
byte[] itemData = Convert.FromBase64String(bar);
using (MemoryStream src = new MemoryStream(itemData))
using (GZipStream gzs = new GZipStream(src, CompressionMode.Decompress))
using (var reader = new BinaryReader(gzs, Encoding.UTF8))
{
foo = reader.ReadString();
}
Console.WriteLine(foo);
The issue was simply that the characters were already encoded in the source string.
Ps : Credit goes to rik for this answer :)
Edit : I also had the StreamReader issue matthew-watson was suggesting.
I am trying to decompress a byte array and get it into a string using a binary reader. When the following code executes, the inStream position changes from 0 to the length of the array, but str is always an empty string.
BinaryReader br = null;
string str = String.Empty;
using (MemoryStream inStream = new MemoryStream(pByteArray))
{
GZipStream zipStream = new GZipStream(inStream, CompressionMode.Decompress);
BinaryReader br = new BinaryReader(zipStream);
str = br.ReadString();
inStream.Close();
br.Close();
}
You haven't shown how is the data being compressed, but here's a full example of compressing and decompressing a buffer:
using System;
using System.IO;
using System.IO.Compression;
using System.Text;
class Program
{
static void Main()
{
var test = "foo bar baz";
var compressed = Compress(Encoding.UTF8.GetBytes(test));
var decompressed = Decompress(compressed);
Console.WriteLine(Encoding.UTF8.GetString(decompressed));
}
static byte[] Compress(byte[] data)
{
using (var compressedStream = new MemoryStream())
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Compress))
{
zipStream.Write(data, 0, data.Length);
zipStream.Close();
return compressedStream.ToArray();
}
}
static byte[] Decompress(byte[] data)
{
using (var compressedStream = new MemoryStream(data))
using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
using (var resultStream = new MemoryStream())
{
zipStream.CopyTo(resultStream);
return resultStream.ToArray();
}
}
}
I am getting the message "Stream was not readable" on the statement:
using (StreamReader sr = new StreamReader(ms))
I have tried the tips posted here without success. Thanks for the help.
This is my code:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Conflict));
//Serialize Conflicts array to memorystream as XML
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(ms))
{
foreach (Conflict ct in Conflicts)
xmlSerializer.Serialize(sw, ct);
sw.Flush(); //Site tip
ms.Position = 0; //Site tip
}
//Retrieve memory stream to string
using (StreamReader sr = new StreamReader(ms))
{
string conflictXml = String.Format(CultureInfo.InvariantCulture, "{0}</NewDataSet>",
When this block of code completes, it will also dispose the attached MemoryStream
using (StreamWriter sw = new StreamWriter(ms))
{
foreach (Conflict ct in Conflicts)
xmlSerializer.Serialize(sw, ct);
sw.Flush(); //Site tip
ms.Position = 0; //Site tip
}
Remove the using statement, and dispose the stream manually after you are done with it
StreamWriter sw = new StreamWriter(ms);
foreach (Conflict ct in Conflicts)
xmlSerializer.Serialize(sw, ct);
sw.Flush(); //Site tip
ms.Position = 0; //Site tip
// other code that uses MemoryStream here...
sw.Dispose();
Try this instead (assuming Conflicts is of type List<Conflict>):
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Conflict>));
StringWriter sw = new StringWriter();
xmlSerializer.Serialize(sw, Conflicts);
string conflictXml = sw.GetStringBuilder().ToString();