Dumping a [Stream] to a file.

  • Thread starter Thread starter Phil Jones
  • Start date Start date
P

Phil Jones

What's the best way to dump a sundry [Stream] into a file?

At present I'm doing the following:

******************
Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)

Dim source As Stream = getStreamMethod()
Dim reader As New StreamReader(source)

REM -- Copy the stream to the target.
Dim writer As New StreamWriter(target)
Do
writer.WriteLine(reader.ReadLine)
Loop Until reader.Peek = -1
writer.Flush()
******************

The problem is this:
1) It breaks if the stream has different encodings (for example, with a GIF
image I'm working with)
2) This doesn't seem like a very efficient way to do something that must
surely be quite a common task.

Thanks anyone....
 
Phil Jones said:
What's the best way to dump a sundry [Stream] into a file?

At present I'm doing the following:

******************
Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)

Dim source As Stream = getStreamMethod()
Dim reader As New StreamReader(source)

The problem is this:
1) It breaks if the stream has different encodings (for example, with a GIF
image I'm working with)
2) This doesn't seem like a very efficient way to do something that must
surely be quite a common task.

The first problem is that you're using a StreamReader to start with. If
you've got binary data, you shouldn't be even *thinking* of converting
it into text. Stick with just streams. The easiest way of doing it is
something like (in C# - you should get the drift):

using (Stream output = new FileStream("c:\\MyFile.bin",
FileMode.CreateNew))
{
using (Stream input = GetStreamMethod())
{
byte[] buffer = new byte [32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read==-1)
break;
output.Write (buffer, 0, read);
}
}
}
 
or use a BinaryWriter
Jon Skeet said:
Phil Jones said:
What's the best way to dump a sundry [Stream] into a file?

At present I'm doing the following:

******************
Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)

Dim source As Stream = getStreamMethod()
Dim reader As New StreamReader(source)

The problem is this:
1) It breaks if the stream has different encodings (for example, with a GIF
image I'm working with)
2) This doesn't seem like a very efficient way to do something that must
surely be quite a common task.

The first problem is that you're using a StreamReader to start with. If
you've got binary data, you shouldn't be even *thinking* of converting
it into text. Stick with just streams. The easiest way of doing it is
something like (in C# - you should get the drift):

using (Stream output = new FileStream("c:\\MyFile.bin",
FileMode.CreateNew))
{
using (Stream input = GetStreamMethod())
{
byte[] buffer = new byte [32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read==-1)
break;
output.Write (buffer, 0, read);
}
}
}
 
Cool. That makes sense. Thanks for the pointer Jon.
---
Phil


Jon Skeet said:
Phil Jones said:
What's the best way to dump a sundry [Stream] into a file?

At present I'm doing the following:

******************
Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)

Dim source As Stream = getStreamMethod()
Dim reader As New StreamReader(source)

The problem is this:
1) It breaks if the stream has different encodings (for example, with a GIF
image I'm working with)
2) This doesn't seem like a very efficient way to do something that must
surely be quite a common task.

The first problem is that you're using a StreamReader to start with. If
you've got binary data, you shouldn't be even *thinking* of converting
it into text. Stick with just streams. The easiest way of doing it is
something like (in C# - you should get the drift):

using (Stream output = new FileStream("c:\\MyFile.bin",
FileMode.CreateNew))
{
using (Stream input = GetStreamMethod())
{
byte[] buffer = new byte [32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read==-1)
break;
output.Write (buffer, 0, read);
}
}
}
 
Hello Phil,

Thanks for posting in the group.

Jon is right. StreamReader is designed for character input in a particular
encoding, whereas the Stream class is designed for byte input and output.
Use StreamReader for reading lines of information from a standard text
file. So if you are dumping a binary file, please use Stream class instead.

Does that answer your question?

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Phil Jones" <[email protected]>
!Subject: Dumping a [Stream] to a file.
!Date: Wed, 24 Sep 2003 20:59:30 +1200
!Lines: 30
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework
!NNTP-Posting-Host: 210-246-0-7.paradise.net.nz 210.246.0.7
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:54575
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!What's the best way to dump a sundry [Stream] into a file?
!
!At present I'm doing the following:
!
!******************
! Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)
!
! Dim source As Stream = getStreamMethod()
! Dim reader As New StreamReader(source)
!
! REM -- Copy the stream to the target.
! Dim writer As New StreamWriter(target)
! Do
! writer.WriteLine(reader.ReadLine)
! Loop Until reader.Peek = -1
! writer.Flush()
!******************
!
!The problem is this:
!1) It breaks if the stream has different encodings (for example, with a GIF
!image I'm working with)
!2) This doesn't seem like a very efficient way to do something that must
!surely be quite a common task.
!
!Thanks anyone....
!---
!Phil
!
!
!
!
 
Sean Chapman said:
or use a BinaryWriter

I don't see what benefit a BinaryWriter would bring here. The point (as
I see it) of a BinaryWriter is to make it easy to write primitives and
strings to a stream. When you're only writing blocks of data as the OP
wants here, I don't see how it helps.
 
Hi Phil,

You are welcome. :)

Also thanks Jon and Sean for actively participating the community.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!From: "Phil Jones" <[email protected]>
!References: <[email protected]>
<[email protected]>
!Subject: Re: Dumping a [Stream] to a file.
!Date: Thu, 25 Sep 2003 16:42:03 +1200
!Lines: 76
!X-Priority: 3
!X-MSMail-Priority: Normal
!X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!Message-ID: <[email protected]>
!Newsgroups: microsoft.public.dotnet.framework
!NNTP-Posting-Host: 210-246-0-68.paradise.net.nz 210.246.0.68
!Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:54675
!X-Tomcat-NG: microsoft.public.dotnet.framework
!
!Yes... I'm sussed here - thank you all.
!
!
!!> Hello Phil,
!>
!> Thanks for posting in the group.
!>
!> Jon is right. StreamReader is designed for character input in a
particular
!> encoding, whereas the Stream class is designed for byte input and output.
!> Use StreamReader for reading lines of information from a standard text
!> file. So if you are dumping a binary file, please use Stream class
!instead.
!>
!> Does that answer your question?
!>
!> Best regards,
!> Yanhong Huang
!> Microsoft Online Partner Support
!>
!> Get Secure! - www.microsoft.com/security
!> This posting is provided "AS IS" with no warranties, and confers no
!rights.
!>
!> --------------------
!> !From: "Phil Jones" <[email protected]>
!> !Subject: Dumping a [Stream] to a file.
!> !Date: Wed, 24 Sep 2003 20:59:30 +1200
!> !Lines: 30
!> !X-Priority: 3
!> !X-MSMail-Priority: Normal
!> !X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
!> !X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
!> !Message-ID: <[email protected]>
!> !Newsgroups: microsoft.public.dotnet.framework
!> !NNTP-Posting-Host: 210-246-0-7.paradise.net.nz 210.246.0.7
!> !Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
!> !Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework:54575
!> !X-Tomcat-NG: microsoft.public.dotnet.framework
!> !
!> !What's the best way to dump a sundry [Stream] into a file?
!> !
!> !At present I'm doing the following:
!> !
!> !******************
!> ! Dim target As New FileStream("C:\MyFile.bin", FileMode.CreateNew)
!> !
!> ! Dim source As Stream = getStreamMethod()
!> ! Dim reader As New StreamReader(source)
!> !
!> ! REM -- Copy the stream to the target.
!> ! Dim writer As New StreamWriter(target)
!> ! Do
!> ! writer.WriteLine(reader.ReadLine)
!> ! Loop Until reader.Peek = -1
!> ! writer.Flush()
!> !******************
!> !
!> !The problem is this:
!> !1) It breaks if the stream has different encodings (for example, with a
!GIF
!> !image I'm working with)
!> !2) This doesn't seem like a very efficient way to do something that must
!> !surely be quite a common task.
!> !
!> !Thanks anyone....
!> !---
!> !Phil
!> !
!> !
!> !
!> !
!>
!
!
!
 
Back
Top