473,320 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stream issue

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)
0
FS.Write(buffer, 0, size)
End While
End Using

Next
....

What am I missing?

Thanks,

Craig Buchanan
Jun 1 '07 #1
4 1458
On Jun 1, 9:55 am, "Craig Buchanan" <some...@somewhere.comwrote:
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)
0
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
Jun 1 '07 #2
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?

"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@p47g2000hsd.googlegr oups.com...
On Jun 1, 9:55 am, "Craig Buchanan" <some...@somewhere.comwrote:
>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)
> 0
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


Jun 2 '07 #3
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
Craig Buchanan wrote:
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)
0
FS.Write(buffer, 0, size)
End While
End Using

Next
...

What am I missing?

Thanks,

Craig Buchanan

Jun 2 '07 #4
Nice! Thanks for the reply.

"Kelly Ethridge" <ke***@kellyethridge.comwrote in message
news:O8**************@TK2MSFTNGP06.phx.gbl...
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
Craig Buchanan wrote:
>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) 0
FS.Write(buffer, 0, size)
End While
End Using

Next
...

What am I missing?

Thanks,

Craig Buchanan

Jun 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: ´óÑã | last post by:
I want to create a multi-stream file using C#,but System.IO.File.Create method do not support this.
1
by: andrewcw | last post by:
OK I am half way there - I can manipulate the stream without the byte issue like this - but is this the way to push the new values back into the stream & write out the stream without resorting to...
5
by: ad | last post by:
I used use SharpZipLib to compress files in disk. But now I want to compress stream into another stream in memory(the stream not associated with disk file) My pseudo is: Stream...
5
by: Daniel Corbett | last post by:
I am trying to save a file dynamically created in a webpage. I get the following headers, but cannot figure out how to save the attachment. I am basically trying to replicate what internet...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
4
by: Lopamudra | last post by:
Hi, I am displaying thumbnails of images in the way Google does in its Image Search. i.e along with the details on the image height, width, size in KB and file type. I am using a http handler...
7
by: mathieu | last post by:
Hi there, I am trying to rewrite this very slow function (due to AFAIK the extra copy): void DoAction(std::istream &is, std::ostream &os) { uint16_t c; while( is.read((char*)&c,2) )
3
by: dont_spam_me | last post by:
I'm running an application on an embedded device running Linux (2.6.21 kernel) . I've found that when the CPU load gets really high on the device, output to a file stream sometimes fails. When...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.