My stream keeps throwing Read/Write Timeout exceptions

12,105

Mischief managed! I was able to overcome this problem by using a BufferedStream and adding an overloaded method to my MD5 method that accepted 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)
Share:
12,105

Related videos on Youtube

Kevin
Author by

Kevin

For years I developed ILE RPGIV software for the IBM AS/400 iSeries midrange computer. I made the leap to .Net years ago and love it. I've worked in a variety of industries from manufacturing, financial, and defense. I'm constantly amazed that even to this day, I learn something new every single day.

Updated on October 15, 2022

Comments

  • Kevin
    Kevin over 1 year

    I am parsing a PowerPoint presentation using Open Office SDK 2.0. At one point in the program I'm passing a stream to a method that will return an image's MD5. However, there seems to be a problem in the stream, before it even 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 let it drop into Hasher.CalculateStreamHash, I check the imageStream properties. Immediately, I see that the ReadTimeout and WriteTimeout both have similar errors:

    imageStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException
    imageStream.WriteTimeout' threw an exception of type 'System.InvalidOperationException
    

    Here's a picture of the properties that I"m seeing during debug, in case it helps: enter image description here

    This code is running over a PowerPoint presentation. I'm wondering if the fact that it's zipped (a PowerPoint presentation is basically just a zipped up file) is the reason I'm seeing those timeout errors?

    UPDATE: I tried taking the stream, getting the image and converting it to a byte array and sending that to the MD5 method as a memory stream, but I still get those same errors in the Read/Write Timeout properties of the stream. Here's the code 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)