/*------------------------
* Conversion between stream and byte []
*-----------------------*/
/// <Summary>
/// Convert stream to byte []
/// </Summary>
Public byte [] streamtobytes (Stream)
{
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, seekorigin. Begin );
Return bytes;
}
/// <Summary>
/// Convert byte [] to stream
/// </Summary>
Public stream bytestostream (byte [] bytes)
{
Stream stream = new memorystream (bytes );
Return stream;
}
/*------------------------
* Conversions between streams and files
*-----------------------*/
/// <Summary>
/// Write stream to a file
/// </Summary>
Public void streamtofile (Stream stream, string filename)
{
// Convert stream to byte []
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, seekorigin. Begin );
// Write byte [] to a file
Filestream FS = new filestream (filename, filemode. Create );
Binarywriter BW = new binarywriter (FS );
Bw. Write (bytes );
Bw. Close ();
FS. Close ();
}
///
// read stream from a file
///
Public stream filetostream (string filename)
{< br> // open the file
filestream = new filestream (filename, filemode. open, fileaccess. read, fileshare. read);
// read the object's byte []
byte [] bytes = new byte [filestream. length];
filestream. read (bytes, 0, bytes. length);
filestream. close ();
// converts byte [] to stream
stream = new memorystream (bytes);
return stream;
}