Sean,

Thanks for the code, this looks like it should be much faster. I may create
a strongly typed byte ArrayList, which would reduce the memory copies from
O(n) to O(log n), if I get enough time.

Erick

----- Original Message -----
From: "Sean Greer (SBI-Chico)" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, May 03, 2002 4:13 PM
Subject: Re: [DOTNET] Downloading a binary file using HttpWebResponse


> Here's my stab at this problem...this is off the top of my head and has
not
> been thoroughly tested, but it does work against my pseudo web service.
:-)
> YMMV
>
> byte[] arrBuffer = new byte[0];
> using(BinaryReader reader = new
> BinaryReader(webResponse.GetResponseStream()))
> {
>         byte[] arrScratch = null;
>         while((arrScratch = reader.ReadBytes(4096)).Length > 0)
>         {
>                 if(arrBuffer.Length == 0)
>                         arrBuffer = arrScratch;
>                 else
>                 {
>                         byte[] arrTemp = new byte[arrBuffer.Length +
> arrScratch.Length];
>                         Array.Copy(arrBuffer, arrTemp, arrBuffer.Length);
>                         Array.Copy(arrScratch, 0, arrTemp,
arrBuffer.Length,
> arrScratch.Length);
>                         arrBuffer = arrTemp;
>                 }
>         }
> }
> // use arrBuffer now...
>
> HTH,
>
> Seang
>
> -----Original Message-----
> From: Erick Thompson [mailto:[EMAIL PROTECTED]]
> Sent: Friday, May 03, 2002 2:24 PM
> To: [EMAIL PROTECTED]
> Subject: [DOTNET] Downloading a binary file using HttpWebResponse
>
>
> I'm downloading binary files from the Stream returned by a HttpWebResponse
> object. The stream returned does not support seeking or look aheads. I
have
> no idea when the length of the stream will be
(HttpWebResponse.ContentLength
> cannot be trusted). What is the fastest way to get the stream into an
array
> of bytes?
>
> Below is the code that I'm using right now. It does work, but it's slow
and
> memory intensive (due to the ArrayList and the cast to a byte array).
There
> must be a better way.
>
> Thanks,
> Erick
>
> private void LoadBinaryContent(HttpWebResponse res) {
>     Stream binStream = res.GetResponseStream();
>     BinaryReader reader = new BinaryReader(binStream);
>     int len = (int)res.ContentLength;
>     if (len == 0) {
>         len = 100000; // make a guess
>     }
>     ArrayList tmp = new ArrayList(len);
>     try {
>         while (true) {
>             tmp.Add(reader.ReadByte());
>         }
>     } catch (EndOfStreamException) {
>         byte b = 0;
>         _binaryContent = (byte[])tmp.ToArray(b.GetType());
>     }
>     reader.Close();
>     binStream.Close();
> }
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.
>
> You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
> subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to