Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: BinaryRead - the whole chunk in one go


Message #1 by Wim Hollebrandse <Wim.Hollebrandse@c...> on Thu, 12 Jul 2001 13:34:46 +0100
Hi,



I am trying to get a download class to work. Serving the whole Byte stream

to the client, using BinaryWrite.



Thing is, I get an error "unable to read beyond the end of the stream.";

this probably is because I read in line by line, and use the ReadString

method, but I'm sure there must be a way to get the whole binary chunk in

one go, and then outputting it.



Any ideas?



Here's my class definition:



---

<script language="VB" runat="server">

	Class DownloadBinary

		Public Sub Download(strFile As String,ByRef objResponse As

Object)

			Dim binOut As StringBuilder = New StringBuilder

			Dim fs As FileStream = New FileStream(strFile,

FileMode.Open)

			Dim fileBin As BinaryReader = New BinaryReader(fs) 

			

			Try

    			fileBin.BaseStream.Seek(0,SeekOrigin.Begin)

    			Do While fileBin.BaseStream.Position <

fileBin.BaseStream.Length

    				binOut.Append(fileBin.ReadString())

    			Loop

			Finally

				fs.Close()

			End Try

			objResponse.BinaryWrite(binOut.ToString())



		End Sub

	End Class

</script>

---



Thanks,

Wim

Message #2 by "Thomas Tomiczek" <t.tomiczek@t...> on Thu, 12 Jul 2001 15:21:01 +0200
Wim,



"binary data" and "ReadString" are pretty bad a match, you know. Storing

the data in a StringBuilder WILL kill your data with a high percentage.



Also, try NOT to read ALL the data - read chunks or, lets say 64k, put

them into the output.



WITHOUT converting to strings.



You have a VB past, right? There was no byte array there.



Thomas



-----Original Message-----

From: Wim Hollebrandse [mailto:Wim.Hollebrandse@c...]

Sent: Donnerstag, 12. Juli 2001 14:35

To: ASP+

Subject: [aspx] BinaryRead - the whole chunk in one go



Hi,



I am trying to get a download class to work. Serving the whole Byte

stream

to the client, using BinaryWrite.



Thing is, I get an error "unable to read beyond the end of the stream.";

this probably is because I read in line by line, and use the ReadString

method, but I'm sure there must be a way to get the whole binary chunk

in

one go, and then outputting it.



Any ideas?



Here's my class definition:



---

<script language=3D"VB" runat=3D"server">

	Class DownloadBinary

		Public Sub Download(strFile As String,ByRef objResponse

As

Object)

			Dim binOut As StringBuilder =3D New StringBuilder

			Dim fs As FileStream =3D New FileStream(strFile,

FileMode.Open)

			Dim fileBin As BinaryReader =3D New

BinaryReader(fs)

		=09

			Try

    			fileBin.BaseStream.Seek(0,SeekOrigin.Begin)

    			Do While fileBin.BaseStream.Position <

fileBin.BaseStream.Length

    				binOut.Append(fileBin.ReadString())

    			Loop

			Finally

				fs.Close()

			End Try

			objResponse.BinaryWrite(binOut.ToString())



		End Sub

	End Class

</script>

---



Thanks,

Wim
Message #3 by Wim Hollebrandse <Wim.Hollebrandse@c...> on Thu, 12 Jul 2001 14:10:31 +0100
OK guys, I just browsed through the Class Libraries and corrected it. 

Now

I'm using the ReadBytes method which seems to work.



---

<script language=3D"VB" runat=3D"server">

	Class DownloadBinary

		Public Sub Download(strFile As String,ByRef objResponse As

Object)

			Dim fs As FileStream =3D New FileStream(strFile,

FileMode.Open)

			Dim fileBin As BinaryReader =3D New BinaryReader(fs)

			Dim buffer() As Byte

		=09

			buffer =3D

fileBin.ReadBytes(fileBin.BaseStream.Length)

			fs.Close()		=09

			objResponse.BinaryWrite(buffer)

		End Sub

	End Class

</script>

---



Of course for the file to be downloaded correctly, I need to set the

Response.ContentType (probably as a parameter in the Download routine) 

and

put the correct MimeType in.



Wim



-----Original Message-----

From: Wim Hollebrandse [mailto:Wim.Hollebrandse@c...]

Sent: 12 July 2001 13:35

To: ASP+

Subject: [aspx] BinaryRead - the whole chunk in one go





Hi,



I am trying to get a download class to work. Serving the whole Byte 

stream

to the client, using BinaryWrite.



Thing is, I get an error "unable to read beyond the end of the 

stream.";

this probably is because I read in line by line, and use the ReadString

method, but I'm sure there must be a way to get the whole binary chunk 

in

one go, and then outputting it.



Any ideas?



Here's my class definition:



---

<script language=3D"VB" runat=3D"server">

	Class DownloadBinary

		Public Sub Download(strFile As String,ByRef objResponse As

Object)

			Dim binOut As StringBuilder =3D New StringBuilder

			Dim fs As FileStream =3D New FileStream(strFile,

FileMode.Open)

			Dim fileBin As BinaryReader =3D New BinaryReader(fs)

		=09

			Try

    			fileBin.BaseStream.Seek(0,SeekOrigin.Begin)

    			Do While fileBin.BaseStream.Position <

fileBin.BaseStream.Length

    				binOut.Append(fileBin.ReadString())

    			Loop

			Finally

				fs.Close()

			End Try

			objResponse.BinaryWrite(binOut.ToString())



		End Sub

	End Class

</script>

---



Thanks,

Wim





Message #4 by Wim Hollebrandse <Wim.Hollebrandse@c...> on Thu, 12 Jul 2001 15:28:00 +0100
Hi Thomas,



I don't just have a VB past, no. Modula, Pascal, Delphi, Visual FoxPro, 

bit

of C...



Anyway, there is a byte array in VB. And you can define it like this: 

Dim

buffer As Byte()



Thing is, a Byte in VB uses 2 bytes to store it...



Anyway, I now use the ReadBytes method. Without the StringBuilder 

stuff.



Thanks,

Wim







-----Original Message-----

From: Thomas Tomiczek [mailto:t.tomiczek@t...]

Sent: 12 July 2001 14:21

To: ASP+

Subject: [aspx] RE: BinaryRead - the whole chunk in one go





Wim,



"binary data" and "ReadString" are pretty bad a match, you know. 

Storing

the data in a StringBuilder WILL kill your data with a high percentage.



Also, try NOT to read ALL the data - read chunks or, lets say 64k, put

them into the output.



WITHOUT converting to strings.



You have a VB past, right? There was no byte array there.



Thomas






  Return to Index