G
Guest
Greetings all...
I was playing around with compressing streams and came across a behavior
that I do not understand. I create a stream (input) from the contents of a
textbox. That stream is compressed into another stream (output). I then
copy the stream (output) to another stream (input2). The compressed stream
(input2) is then decompressed into a final stream (output2).
My question is this. I create input2 from output.GetBuffer() method. If
I close the compress DeflateStream prior to the copy, everything works. If I
don't close it, the decompression won't work. If I comment out the Close(),
I can see no difference using watch. Can someone help me understand why I
have to use the close?
Here is the code...
private void button1_Click(object sender, EventArgs e)
{
MemoryStream input = new
MemoryStream(UnicodeEncoding.Unicode.GetBytes(textBox1.Text));
MemoryStream output = new MemoryStream();
Stream compress = new DeflateStream(output,
CompressionMode.Compress);
int theByte = input.ReadByte();
while (theByte != -1)
{
compress.WriteByte((byte)theByte);
theByte = input.ReadByte();
}
//Why does this have to be here?
compress.Close();
MemoryStream output2 = new MemoryStream();
MemoryStream input2 = new MemoryStream(output.GetBuffer());
Stream decompress = new DeflateStream(input2,
CompressionMode.Decompress);
theByte = decompress.ReadByte();
while (theByte != -1)
{
output2.WriteByte((byte)theByte);
theByte = decompress.ReadByte();
}
textBox2.Text =
UnicodeEncoding.Unicode.GetString(output2.GetBuffer());
}
Thanks in advance for any help.
I was playing around with compressing streams and came across a behavior
that I do not understand. I create a stream (input) from the contents of a
textbox. That stream is compressed into another stream (output). I then
copy the stream (output) to another stream (input2). The compressed stream
(input2) is then decompressed into a final stream (output2).
My question is this. I create input2 from output.GetBuffer() method. If
I close the compress DeflateStream prior to the copy, everything works. If I
don't close it, the decompression won't work. If I comment out the Close(),
I can see no difference using watch. Can someone help me understand why I
have to use the close?
Here is the code...
private void button1_Click(object sender, EventArgs e)
{
MemoryStream input = new
MemoryStream(UnicodeEncoding.Unicode.GetBytes(textBox1.Text));
MemoryStream output = new MemoryStream();
Stream compress = new DeflateStream(output,
CompressionMode.Compress);
int theByte = input.ReadByte();
while (theByte != -1)
{
compress.WriteByte((byte)theByte);
theByte = input.ReadByte();
}
//Why does this have to be here?
compress.Close();
MemoryStream output2 = new MemoryStream();
MemoryStream input2 = new MemoryStream(output.GetBuffer());
Stream decompress = new DeflateStream(input2,
CompressionMode.Decompress);
theByte = decompress.ReadByte();
while (theByte != -1)
{
output2.WriteByte((byte)theByte);
theByte = decompress.ReadByte();
}
textBox2.Text =
UnicodeEncoding.Unicode.GetString(output2.GetBuffer());
}
Thanks in advance for any help.