Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Problem with Writing FileStream

Problem with Writing FileStream

Scheduled Pinned Locked Moved C#
csshelpquestion
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Software2007

    I have a stream object coming from mobile client in binary fromat as follows

    name=tony&image=....raw binary representation of an image.....

    I would like to extract the image out of the stream. I can extract the image and show it fine as long as string size preceding the binary image data is 16 chars or less! ("name=tony&image=") is 16 chars long

    bool PostData(Stream stream)
    {
    using (FileStream filetoUpload = new FileStream(filePath + "ImageFromBytes.jpg", FileMode.Create))
    {
    byte[] byteArray = new byte[10000];
    int bytesRead = 0;
    bytesRead = stream.Read(byteArray, 0, byteArray.Length);

    //write image data, ignore the preceding string
    if (bytesRead > 0)
    filetoUpload.Write(byteArray, 16, bytesRead-16);
    ....
    }
    }

    If I do("name=randy&image=") = 17 chars long, it doesn't work, the image shows the right size, but can't be opened.

    name=randy&image=....raw binary representation of an image.....

    filetoUpload.Write(byteArray, 17, bytesRead-17);

    Any ideas what in the world is going on?

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    I answered this question yesterday and explained that you need to parse the input stream. You cannot assume that every name will be only 4 characters long. You already have the marker characters that separate your objects (&) and keyword value pairs (=), so you should be scanning your input stream for those characters to split the data into its constituent parts.

    speaking as ...

    1 Reply Last reply
    0
    • S Software2007

      I have a stream object coming from mobile client in binary fromat as follows

      name=tony&image=....raw binary representation of an image.....

      I would like to extract the image out of the stream. I can extract the image and show it fine as long as string size preceding the binary image data is 16 chars or less! ("name=tony&image=") is 16 chars long

      bool PostData(Stream stream)
      {
      using (FileStream filetoUpload = new FileStream(filePath + "ImageFromBytes.jpg", FileMode.Create))
      {
      byte[] byteArray = new byte[10000];
      int bytesRead = 0;
      bytesRead = stream.Read(byteArray, 0, byteArray.Length);

      //write image data, ignore the preceding string
      if (bytesRead > 0)
      filetoUpload.Write(byteArray, 16, bytesRead-16);
      ....
      }
      }

      If I do("name=randy&image=") = 17 chars long, it doesn't work, the image shows the right size, but can't be opened.

      name=randy&image=....raw binary representation of an image.....

      filetoUpload.Write(byteArray, 17, bytesRead-17);

      Any ideas what in the world is going on?

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #3

      Software2007 wrote:

      Any ideas what in the world is going on?

      Sure - you have a randy image. As you've discovered, you can't rely on fixed lengths in your string here - instead, you need to find the index of the second =. You can do this with string.IndexOf to find it. The good news is that you know where the first = is based on your key here, so all you need to do is use IndexOf starting on the value 5 (the position after name= in your string).

      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

      L 1 Reply Last reply
      0
      • S Software2007

        I have a stream object coming from mobile client in binary fromat as follows

        name=tony&image=....raw binary representation of an image.....

        I would like to extract the image out of the stream. I can extract the image and show it fine as long as string size preceding the binary image data is 16 chars or less! ("name=tony&image=") is 16 chars long

        bool PostData(Stream stream)
        {
        using (FileStream filetoUpload = new FileStream(filePath + "ImageFromBytes.jpg", FileMode.Create))
        {
        byte[] byteArray = new byte[10000];
        int bytesRead = 0;
        bytesRead = stream.Read(byteArray, 0, byteArray.Length);

        //write image data, ignore the preceding string
        if (bytesRead > 0)
        filetoUpload.Write(byteArray, 16, bytesRead-16);
        ....
        }
        }

        If I do("name=randy&image=") = 17 chars long, it doesn't work, the image shows the right size, but can't be opened.

        name=randy&image=....raw binary representation of an image.....

        filetoUpload.Write(byteArray, 17, bytesRead-17);

        Any ideas what in the world is going on?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #4

        Software2007 wrote:

        the image shows the right size

        where? how?

        Software2007 wrote:

        but can't be opened

        when? by what program? has your app exited already? did you wait a full minute and try again? Please start taking the answers you get more seriously, and provide more detailed accurate information, so we all can help you more effectively. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        S 1 Reply Last reply
        0
        • P Pete OHanlon

          Software2007 wrote:

          Any ideas what in the world is going on?

          Sure - you have a randy image. As you've discovered, you can't rely on fixed lengths in your string here - instead, you need to find the index of the second =. You can do this with string.IndexOf to find it. The good news is that you know where the first = is based on your key here, so all you need to do is use IndexOf starting on the value 5 (the position after name= in your string).

          *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          Pete O'Hanlon wrote:

          use IndexOf starting on the value 5

          Not really a good suggestion based on the original question[^]. I get the impression he is trying to solve the problem by modifying the input data rather than getting to grips with the concept of parsing it.

          speaking as ...

          P 1 Reply Last reply
          0
          • L Lost User

            Pete O'Hanlon wrote:

            use IndexOf starting on the value 5

            Not really a good suggestion based on the original question[^]. I get the impression he is trying to solve the problem by modifying the input data rather than getting to grips with the concept of parsing it.

            speaking as ...

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #6

            Ah. I wasn't aware that he'd had an original question. Ho hum.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            L 1 Reply Last reply
            0
            • P Pete OHanlon

              Ah. I wasn't aware that he'd had an original question. Ho hum.

              *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #7

              Another one that's probably going to run and run ...

              speaking as ...

              P 1 Reply Last reply
              0
              • L Lost User

                Another one that's probably going to run and run ...

                speaking as ...

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #8

                I suspect you're right.

                *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                1 Reply Last reply
                0
                • L Luc Pattyn

                  Software2007 wrote:

                  the image shows the right size

                  where? how?

                  Software2007 wrote:

                  but can't be opened

                  when? by what program? has your app exited already? did you wait a full minute and try again? Please start taking the answers you get more seriously, and provide more detailed accurate information, so we all can help you more effectively. :)

                  Luc Pattyn [My Articles] Nil Volentibus Arduum

                  S Offline
                  S Offline
                  Software2007
                  wrote on last edited by
                  #9

                  Luc: The image doesn't open up on disk, meaning I can't view the image. I have split the stream based on '&'. The bytes array apparently is full of them when it comes to image raw binary data. So, I decided to parse the string to the left of "image=". This is not where the problem is, the problem is after parsing, for the remaining image bytes, why can't I open the image correctly if the preceding string happens to be more than 16 chars long? The code above is only psuedo code, in reality this will be much longer string.

                  T L 2 Replies Last reply
                  0
                  • S Software2007

                    Luc: The image doesn't open up on disk, meaning I can't view the image. I have split the stream based on '&'. The bytes array apparently is full of them when it comes to image raw binary data. So, I decided to parse the string to the left of "image=". This is not where the problem is, the problem is after parsing, for the remaining image bytes, why can't I open the image correctly if the preceding string happens to be more than 16 chars long? The code above is only psuedo code, in reality this will be much longer string.

                    T Offline
                    T Offline
                    Trak4Net
                    wrote on last edited by
                    #10

                    Is it the exact same binary data for both preceding lengths? If not, maybe the stream exceeds the size of your byteArray buffer? The byte array should be initialized at the length of the stream without your preceeding string, not sure if you are doing that in live code but from your example it is a static value. Giving more information on your stream length and maybe even a sample of how you are generating the image would be helpful?

                    S 1 Reply Last reply
                    0
                    • S Software2007

                      Luc: The image doesn't open up on disk, meaning I can't view the image. I have split the stream based on '&'. The bytes array apparently is full of them when it comes to image raw binary data. So, I decided to parse the string to the left of "image=". This is not where the problem is, the problem is after parsing, for the remaining image bytes, why can't I open the image correctly if the preceding string happens to be more than 16 chars long? The code above is only psuedo code, in reality this will be much longer string.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #11

                      If that's your best answer to my six questions, it stops here for me. Lacking the info, I'm not going to waste my time guessing what is going on. :(

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      S 1 Reply Last reply
                      0
                      • T Trak4Net

                        Is it the exact same binary data for both preceding lengths? If not, maybe the stream exceeds the size of your byteArray buffer? The byte array should be initialized at the length of the stream without your preceeding string, not sure if you are doing that in live code but from your example it is a static value. Giving more information on your stream length and maybe even a sample of how you are generating the image would be helpful?

                        S Offline
                        S Offline
                        Software2007
                        wrote on last edited by
                        #12

                        Yes, it same exact image being generated, its sitting in my project folder. I tried dealing with static numbers to debug it easier. From iphone:

                        NSData *strData = ["name=tony&image=" datausingencoding:NSFTU8StringEncoding]
                        NSData *imageData = NSDATA(myImage)....raw binary representation of an image.....
                        [body appendData:strData];
                        [body appendData:imageData];
                        [request setHTTP body]

                        The WCF code in my original post works for any string <= 16 chars. If add an extra charcter and make it 17 chars "

                        "name=tony1&image="

                        , it stops working (of course I change the offset when I do that by one. Something is special aboutt he 16 char length here, may be it has to do with Encoding or something.

                        1 Reply Last reply
                        0
                        • L Luc Pattyn

                          If that's your best answer to my six questions, it stops here for me. Lacking the info, I'm not going to waste my time guessing what is going on. :(

                          Luc Pattyn [My Articles] Nil Volentibus Arduum

                          S Offline
                          S Offline
                          Software2007
                          wrote on last edited by
                          #13

                          This line was the problem, byte[] byteArray = new byte[10000]; I chose a randomly bigger number 20,000 and it works. But, it still doesn't make lot of sense to me. The bytesread for the image data alone is 5430. So, with extra 17 bytes string, I would think the 10000 would be still big enough? Is there a way to know how much to allocate instead of just guessing a large number? Stream.length seems to be problematic for me. Remember, this is a network stream that I don't know the size of containing image data.

                          T 1 Reply Last reply
                          0
                          • S Software2007

                            This line was the problem, byte[] byteArray = new byte[10000]; I chose a randomly bigger number 20,000 and it works. But, it still doesn't make lot of sense to me. The bytesread for the image data alone is 5430. So, with extra 17 bytes string, I would think the 10000 would be still big enough? Is there a way to know how much to allocate instead of just guessing a large number? Stream.length seems to be problematic for me. Remember, this is a network stream that I don't know the size of containing image data.

                            T Offline
                            T Offline
                            Trak4Net
                            wrote on last edited by
                            #14

                            That is what I was talking about static values. If this is network stream you either need an end of message marker to make sure you read the whole message or read until nothing is left. You should be able to check what is available on the network stream and if multiple reads are required to get the entire message you may need to do some byte array manipulation (new byte() with current and new and use array.copy...) hope that is helpful.

                            S 1 Reply Last reply
                            0
                            • T Trak4Net

                              That is what I was talking about static values. If this is network stream you either need an end of message marker to make sure you read the whole message or read until nothing is left. You should be able to check what is available on the network stream and if multiple reads are required to get the entire message you may need to do some byte array manipulation (new byte() with current and new and use array.copy...) hope that is helpful.

                              S Offline
                              S Offline
                              Software2007
                              wrote on last edited by
                              #15

                              Thanks. I will look into it.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups