Stream issue

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
C

Craig Buchanan

I am trying to save a MemoryStream (the attachment's Data property) to disk
using a FileStream. It seems that the MemoryStream's Read method is the
issue. The MemoryStream's size is 6680 bytes and its Position is 0.

Here's the code:

....
For Each attachment As Attachment In Attachments

Using FS As New FileStream(app_data & fileName, FileMode.Create)

Dim buffer(32768) As Byte
Dim size As Integer

'size is always 0, so the .Write method is never called.
While size = attachment .Data.Read(buffer, 0, buffer.Length)
FS.Write(buffer, 0, size)
End While
End Using

Next
....

What am I missing?

Thanks,

Craig Buchanan
 
I am trying to save a MemoryStream (the attachment's Data property) to disk
using a FileStream. It seems that the MemoryStream's Read method is the
issue. The MemoryStream's size is 6680 bytes and its Position is 0.

Here's the code:

...
For Each attachment As Attachment In Attachments

Using FS As New FileStream(app_data & fileName, FileMode.Create)

Dim buffer(32768) As Byte
Dim size As Integer

'size is always 0, so the .Write method is never called.
While size = attachment .Data.Read(buffer, 0, buffer.Length)
FS.Write(buffer, 0, size)
End While
End Using

Next
...

What am I missing?

The Read method returns 0 if it reaches the end of stream before
reading any data. Have you tried explicitly setting the position of
the stream (unless that happens elsewhere in code you did not post) ?

Just for the heck of it, what happens if you place this line in your
code before the while loop?

attachment.Data.Seek(0, SeekOrigin.Begin);

This is from the docs:

"The Read method will return zero only if the end of the stream is
reached. In all other cases, Read always reads at least one byte from
the stream before returning. By definition, if no data is available
from the stream upon a call to Read, the Read method returns zero (the
end of the stream is reached automatically). An implementation is free
to return fewer bytes than requested even if the end of the stream has
not been reached."

Chris
 
chris-

thanks for the reply.

i change the code a bit:

Dim size As Integer = attachment .Data.Read(buffer, 0, buffer.Length)
While size > 0
FS.Write(buffer, 0, size)
size = attachment .Data.Read(buffer, 0, buffer.Length)
End While

this worked correctly. perhaps i needed to enclose the test in ().

thoughts?
 
Why not just use the MemoryStream's WriteTo method to write the contents
to the FileStream?

For Each attachment As Attachment In Attachments
Using FS As New FileStream(app_data & fileName, FileMode.Create)
attachment.Data.WriteTo(FS)
End Using
Next
 
Nice! Thanks for the reply.

Kelly Ethridge said:
Why not just use the MemoryStream's WriteTo method to write the contents
to the FileStream?

For Each attachment As Attachment In Attachments
Using FS As New FileStream(app_data & fileName, FileMode.Create)
attachment.Data.WriteTo(FS)
End Using
Next
 
Back
Top