My thread keeps throwing read / write exceptions
I am analyzing a PowerPoint presentation using the Open Office SDK 2.0. At some point in the program, I am passing a stream to a method that will return an MD5 image. However, there seems to be a threading issue before it gets to my MD5 method.
Here's my code:
// Get image information here.
var blipRelId = blip.Embed;
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId);
var imageFileName = imagePart.Uri.OriginalString;
var imageStream = imagePart.GetStream();
var imageMd5 = Hasher.CalculateStreamHash(imageStream);
In debug, before I put it in Hasher.CalculateStreamHash
, I check the properties of the imageStream. Immediately, I see that ReadTimeout and WriteTimeout have the same errors:
imageStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException
imageStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException
Here is a picture of the properties I see while debugging, in case it helps:
This code is working on a PowerPoint presentation. I am wondering if the fact that it is zipped (the PowerPoint presentation is basically just an encrypted file) is the reason why I see these timeout errors?
UPDATE: I tried to take a stream, get an image and convert it to a byte array and send it to the MD5 method as a memory stream, but I still get the same errors in the read / write timeout properties of the stream, Here the code is as it is now :
// Get image information here.
var blipRelId = blip.Embed;
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId);
var imageFileName = imagePart.Uri.OriginalString;
var imageStream = imagePart.GetStream();
// Convert image to memory stream
var img = Image.FromStream(imageStream);
var imageMemoryStream = new MemoryStream(this.imageToByteArray(img));
var imageMd5 = Hasher.CalculateStreamHash(imageMemoryStream);
For clarity, here's the signature for the CalculateStreamHash method:
public static string CalculateStreamHash([NotNull] Stream stream)
source to share
Damn it! I was able to overcome this problem by using a BufferedStream and adding an overloaded method to my MD5 method that took a BufferedStream as a parameter:
// Get image information here.
var blipRelId = blip.Embed;
var imagePart = (ImagePart)slidePart.GetPartById(blipRelId);
var imageFileName = imagePart.Uri.OriginalString;
// Convert image to buffered stream
var imageBufferedStream = new BufferedStream(imagePart.GetStream());
var imageMd5 = Hasher.CalculateStreamHash(imageBufferedStream);
... and:
public static string CalculateStreamHash([NotNull] BufferedStream bufferedStream)
source to share