Problem with reading binary file.


  • Hello everyone,

    I have created a SL4 application which should read the binary file (jpeg image) and display the content of it. I am using the code below. When the read method executed, i get an exception below:

    "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

    System.Text.UnicodeEncoding Unicode = new System.Text.UnicodeEncoding();
                            Byte[] BytesMessage = Unicode.GetBytes(e.Data);
    FileStream FileStream = new FileStream("C:\\FileUpload\\" + this.UserImage.FileName, FileMode.Create);
    FileStream.Write(BytesMessage, 0, this.UserImage.Length);

    The method creates a file with right name and extension in the folder but size of the file is 0 byte. Any help will be very appreciated!

    Thursday, January 19, 2012 6:45 AM

All replies

  • Hi

    don't use static Path. use FileDialog Control to Select file.

    check below sample

    Thursday, January 19, 2012 10:49 AM
  • Thanks a lot for quick reply and comment. In fact I get the binary file in question through Socket. I open socket, get binary stream, close the socket. Do you think the problem is security related?

    Thursday, January 19, 2012 12:12 PM
  • I don't see a Read method in the code above.  You might want to post it. 

    Thursday, January 26, 2012 8:52 PM
  • Make sure the byte array's lengh is larger than UserImage's lengh. Otherwise you'll encounter this issue.

    Friday, January 27, 2012 1:48 AM
  • Trevor, Lanxing, thanks a lot for further details.

    I retrieve binary stream from the server in other class like below:


                                receivestr = UTF8Encoding.Unicode.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                                taillength = ex2.BytesUnknown.Length;
                                ex2.BytesUnknown.CopyTo(e.Buffer, 0);
                            catch (DecoderFallbackException ex3)
                                throw new Exception("Message decode failed.", ex3);

    It's a jpeg picture. When I encode the jpeg file via the following website
    I get a base 64 string that represents the file like so: "/9j/4AAQSkZJRgABAQAAAQABAAD/2w..."

    So my object is to extract the completely similar 64 base string from a sequence of bytes. I do it like below: 
    byte[] BytesMessageUTF = UTF8Encoding.Unicode.GetBytes(e.Data);
    string strImagestr = Convert.ToBase64String(BytesMessageUTF);
    So if I set a breakpoint in VS 2010 I'll see that I could extract in many ways similar 64 base string.
    The only difference is a couple of header and footer characters. And that's the problem needs to be addressed. So the problem is not in the ViewModel class but in the SocketAPI class. The first ff characters throw DecoderFallbackException exception. So I think I need to scan the whole stream and filter it. Any help will be greatly appreciated.
    Friday, March 02, 2012 4:30 AM
  • I tried to made the changes below to address the problem:

    receivestr = UTF8Encoding.Unicode.GetString(e.Buffer, 3, e.BytesTransferred - 3);

    I've also built a custom 64 base string encoder. (It was simply a waste of time though, since it returns exactly the same 64base string)

    So now, a couple of symbols (/f/) are still different from the original one. The python server sends data as ASCII. So I am pretty sure that the problem in this line of the code in the ViewModel class:

    byte[] BytesMessageUTF = UTF8Encoding.Unicode.GetBytes(e.Data);

    Looks like this built in class substitutes some characters in wrong way. But I've validated 64base string, it's valid. I am pretty confused on what makes the problem.

    Wednesday, March 07, 2012 11:35 AM