Can we clone a Stream?
No, streams usually refer to local resources of some kind (a socket, a file handle, etc) and so they can't be cloned or serialized. Furthermore many streams are forward-only and do not support seeking so you might not even be able to re-read from a stream.
What you can do from a readable stream though is copy it into a MemoryStream which can be moved around as a byte array.
See the following post for a code snippet showing how to do this: How do I copy the contents of one stream to another?
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With