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. How to read bytes from Stream into Objects?

How to read bytes from Stream into Objects?

Scheduled Pinned Locked Moved C#
mobiledata-structuresjsontutorialquestion
8 Posts 3 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 Offline
    S Offline
    Software2007
    wrote on last edited by
    #1

    I am sending data from mobile app to REST in binary format like this:

    "firstname=Mike&lastname=Smith&..binary represenation for the image here.."

    class Employee{
    string firstname;
    string lastname;
    Stream photo;
    }

    public string PostMethod(Stream streamObj)
    {
    using (FileStream filetoUpload = new FileStream(filePath + filename, FileMode.Create))
    {
    byte[] byteArray = new byte[10000];
    int bytesRead = 0;

               do
               {
                   bytesRead = streamObj.Read(byteArray, 0, byteArray.Length);
                   if (bytesRead > 0)
                   {
                       filetoUpload.Write(byteArray, 0, bytesRead);
                   }
               }
    
               while (bytesRead > 0);
           }
    
           return "Uploaded " + filePath + filename;
       }
    

    I do know how to get the contents of the Stream into a byte array. What do I need to do to be able to read the bytes from the StreamObj into the class? Some pseudo code would be helpful. employee.firstname = ... employee.lastname=... employee.photo=....

    L 2 Replies Last reply
    0
    • S Software2007

      I am sending data from mobile app to REST in binary format like this:

      "firstname=Mike&lastname=Smith&..binary represenation for the image here.."

      class Employee{
      string firstname;
      string lastname;
      Stream photo;
      }

      public string PostMethod(Stream streamObj)
      {
      using (FileStream filetoUpload = new FileStream(filePath + filename, FileMode.Create))
      {
      byte[] byteArray = new byte[10000];
      int bytesRead = 0;

                 do
                 {
                     bytesRead = streamObj.Read(byteArray, 0, byteArray.Length);
                     if (bytesRead > 0)
                     {
                         filetoUpload.Write(byteArray, 0, bytesRead);
                     }
                 }
      
                 while (bytesRead > 0);
             }
      
             return "Uploaded " + filePath + filename;
         }
      

      I do know how to get the contents of the Stream into a byte array. What do I need to do to be able to read the bytes from the StreamObj into the class? Some pseudo code would be helpful. employee.firstname = ... employee.lastname=... employee.photo=....

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

      First you need to split your array into sections as delineated by the & character. You can then convert the first two to properly encoded character strings using the System.Text.Encoding.GetChars()[^] method, and then split them based on the = character.

      speaking as ...

      OriginalGriffO 1 Reply Last reply
      0
      • L Lost User

        First you need to split your array into sections as delineated by the & character. You can then convert the first two to properly encoded character strings using the System.Text.Encoding.GetChars()[^] method, and then split them based on the = character.

        speaking as ...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Might not work - I think he needs to look a little more closely at his data. The third section is raw binary by my interpretation, so it probably shouldn't be in a string at all or he runs the risk of unicode conversion problems.

        Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          Might not work - I think he needs to look a little more closely at his data. The third section is raw binary by my interpretation, so it probably shouldn't be in a string at all or he runs the risk of unicode conversion problems.

          Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water

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

          My suggestions were largely guesswork, but I wasn't proposing to convert section 3 to a string, just the first two.

          speaking as ...

          1 Reply Last reply
          0
          • S Software2007

            I am sending data from mobile app to REST in binary format like this:

            "firstname=Mike&lastname=Smith&..binary represenation for the image here.."

            class Employee{
            string firstname;
            string lastname;
            Stream photo;
            }

            public string PostMethod(Stream streamObj)
            {
            using (FileStream filetoUpload = new FileStream(filePath + filename, FileMode.Create))
            {
            byte[] byteArray = new byte[10000];
            int bytesRead = 0;

                       do
                       {
                           bytesRead = streamObj.Read(byteArray, 0, byteArray.Length);
                           if (bytesRead > 0)
                           {
                               filetoUpload.Write(byteArray, 0, bytesRead);
                           }
                       }
            
                       while (bytesRead > 0);
                   }
            
                   return "Uploaded " + filePath + filename;
               }
            

            I do know how to get the contents of the Stream into a byte array. What do I need to do to be able to read the bytes from the StreamObj into the class? Some pseudo code would be helpful. employee.firstname = ... employee.lastname=... employee.photo=....

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

            Software2007 wrote:

            I do know how to get the contents of the Stream into a byte array.

            Google for Serialize/deserialize - takes only a few lines of code. You'll be wanting the BinaryFormatter.

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            S 1 Reply Last reply
            0
            • L Lost User

              Software2007 wrote:

              I do know how to get the contents of the Stream into a byte array.

              Google for Serialize/deserialize - takes only a few lines of code. You'll be wanting the BinaryFormatter.

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

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

              OriginalGriff is correct that the image section is raw binary. So, what to do again?

              L 1 Reply Last reply
              0
              • S Software2007

                OriginalGriff is correct that the image section is raw binary. So, what to do again?

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

                Serialize your employee, using the binaryformatter :) Serializing means that you'd encode it in a portable format. You can use different formatters, but the Xml-version doesn't sound too appropriate under these circumstances, hence the binaryformatter.

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                S 1 Reply Last reply
                0
                • L Lost User

                  Serialize your employee, using the binaryformatter :) Serializing means that you'd encode it in a portable format. You can use different formatters, but the Xml-version doesn't sound too appropriate under these circumstances, hence the binaryformatter.

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

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

                  Can you elaborate a bit. So, I have a stream

                  "firstname=Mike&lastname=Smith&..binary represenation for the image here.."

                  in binary format.

                  [Serializable]
                  public class Employee {
                  Serialize()..
                  Deserialize()..
                  }

                  This sounds like I need a file for the serialization, but I thought this was memory data type of thing. Anyways, what after this, how do I read it in to the class members what I don't get? How do I go from Stream type which is sent to me to Employee? Note, stream comes from iphone app.

                  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