Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

C#, byte streaming with StreamReader problem

Hi there;

I am trying to do byte streaming with C#. The application must reach the FTP server (which he can) and grab the file (which is not a text file), some encrypted file.

I can reach the server and list the directories with no problem. My problem is regarding streaming the file content. In fact, I have a compile time error in the lines:
int bytesRead = reader.Read(buffer, 0, buffer.Length);
and in the similar line in while.

I am sure it is true since the function takes the exact params but the compiler says:

Error 1 The best overloaded method match for 'System.IO.TextReader.Read(char[], int, int)' has some invalid arguments      
Error 2 Argument '1': cannot convert from 'byte[]' to 'char[]'      

My code is as follows:

Stream responseStream = response.GetResponseStream();
...
StreamReader reader = new StreamReader(responseStream);
MemoryStream memStream = new MemoryStream();
int Size = 1024;
            byte[] buffer = new byte[Size];
            int bytesRead = reader.Read(buffer, 0, buffer.Length);
            while (bytesRead != 0) //while(true)
                {
                    //Try to read the data
                    bytesRead = reader.Read(buffer, 0, buffer.Length);
                      //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);
                       }
                byte[] downloadedData = new byte[0];
                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();
...

Best regards.
Avatar of Omego2K
Omego2K
Flag of United States of America image

why not do this:?
Stream responseStream = response.GetResponseStream();
...
MemoryStream memStream = new MemoryStream();
int Size = 1024;
            byte[] buffer = new byte[Size];
            int bytesRead = responseStream.Read(buffer, 0, buffer.Length);
            while (bytesRead != 0) //while(true)
                {
                    //Try to read the data
                    bytesRead = reader.Read(buffer, 0, buffer.Length);
                      //Write the downloaded data
                        memStream.Write(buffer, 0, bytesRead);
                       }
                byte[] downloadedData = new byte[0];
                //Convert the downloaded stream to a byte array
                downloadedData = memStream.ToArray();

Open in new window

Avatar of jazzIIIlove

ASKER

I couldn't see the difference with my original code in the question. What did you change?
Also, when I put your segment to my code, nothing changes, same error in the same line.
ASKER CERTIFIED SOLUTION
Avatar of Omego2K
Omego2K
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Omego2K:

Sorry, I am a little tired. I missed it.

My issue is ok, i can read txt now, but the file i am going to read is an encrypted file that I want to store it to my localdrive. I can read txt files, but simply, cannot read the encrpypted file. No bytes are coming from that file. What did I miss?
mas_oz2003:
Will the idea you are giving work on my issue?

Best regards.
Yes, it will work because the encrypted file can be treated as binary file. you will recover the contents as byte[] and wirte it as byte[] as per your sample code.
Yet, I haven't tried but I find the problem that for some number of characters it is not working, after some number of characters are passed in the file, it is read. Interesting, isn't it?