Why do I need two calls to stream CopyTo?
I have the following method and for some reason the first call to Copy to seems to do nothing? Anyone know why? In input to the method is compressed and base64 can supply that method to if need.
private byte[] GetFileChunk(string base64)
{
using (
MemoryStream compressedData = new MemoryStream(Convert.FromBase64String(base64), false),
uncompressedData = new MemoryStream())
{
using (GZipStream compressionStream = new GZipStream(compressedData, CompressionMode.Decompress))
{
// first copy does nothing ?? second works
compressionStream.CopyTo(uncompressedData);
compressionStream.CopyTo(uncompressedData);
开发者_运维百科 }
return uncompressedData.ToArray();
}
}
If the first call to Read() returns 0 then Stream.CopyTo() isn't going to work either. While this points to a problem with GZipStream, it is very unlikely that it has a bug like this. Far more likely is that something went wrong when you created the compressed data. Like compressing 0 bytes first, followed by compressing the real data.
Just a guess, but is it because the new GZipStream
constructor leaves the index at the end of the array, and the first CopyTo
resets it to the start, so that when you call the second CopyTo
its now at the start and copies the data properly?
How sure are you that first copy does nothing and the second works
, that would be a bug in the GZipStream
class. Your code should work fine without calling CopyTo twice.
Hi thank you for everybody’s input. It turns out the error was caused by a mistake in encoding method. The method was
/// <summary>
/// Compress file data and then base64s the compressed data for safe transportation in XML.
/// </summary>
/// <returns>Base64 string of file chunk</returns>
private string GetFileChunk()
{
// MemoryStream for compression output
using (MemoryStream compressed = new MemoryStream())
{
using (GZipStream zip = new GZipStream(compressed, CompressionMode.Compress))
{
// read chunk from file
byte[] plaintext = new byte[this.readSize];
int read = this.file.Read(plaintext, 0, plaintext.Length);
// write chunk to compreesion
zip.Write(plaintext, 0, read);
plaintext = null;
// Base64 compressed data
return Convert.ToBase64String(compressed.ToArray());
}
}
}
The return line should be below the using allowing the compression stream to close and flush, this caused the inconsistent behaviour when decompressing the stream.
/// <summary>
/// Compress file data and then base64s the compressed data for safe transportation in XML.
/// </summary>
/// <returns>Base64 string of file chunk</returns>
private string GetFileChunk()
{
// MemoryStream for compression output
using (MemoryStream compressed = new MemoryStream())
{
using (GZipStream zip = new GZipStream(compressed, CompressionMode.Compress))
{
// read chunk from file
byte[] plaintext = new byte[this.readSize];
int read = this.file.Read(plaintext, 0, plaintext.Length);
// write chunk to compreesion
zip.Write(plaintext, 0, read);
plaintext = null;
}
// Base64 compressed data
return Convert.ToBase64String(compressed.ToArray());
}
}
Thank you for everybody's help.
精彩评论