Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Managed types and data streams

This topic is 5282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a bunch of binary data in a stream that is used to populate a collection of N managed types. For example, let''s say I have the following class:
class ManagedType
   public float floatData[] = new float[3];
   public int intData[] = new int[2];
   public int someInt;
   public char someChar;
In the stream will be (in order) three floats, two ints, one int, and one char, repeated N times. The way the file is structured, I only know beforehand how many bytes the entire collection is, rather than N. Naturally this leads to some problems when working with managed types. I need to return an array of ManagedType, but since I don''t know how many elements there are beforehand, I''m stuck using an ArrayList and converting it to an array. I don''t like that solution, and on top of that, there''s no way to verify the existence of complete data in the file. Given the following construct:
while(bReader.PeekChar() != -1)
   ManagedType managedType = new ManagedType();

   // Assume we''re using a BinaryReader around the stream
   managedType.floatData[0] = bReader.ReadSingle();
   managedType.floatData[1] = bReader.ReadSingle();
   managedType.someChar = bReader.ReadChar();

This assumes that we have exactly the amount of data we need. However, if there is more or less, an exception will be thrown midway through the loop since there we be a sudden lack of data. My preferred solution would be to do something similar to what I would have done in C/C++ - take the length of the stream and divide by the size of the type to obtain the number of elements. Truncation makes sure I don''t read in partial structures. However, even if I use the StructLayout attribute on ManagedType to enable calls to Marshal.SizeOf, I don''t know it would handle the arrays. My guess is that it would marshal them to pointers, which would obviously lack their managed size information. And I don''t feel like writing a custom marshaller So, will I have to bite the bullet, use ArrayList, and handle stream reader exceptions? Or is there another approach I''m overlooking? I''d like to stay as safe and managed as possible.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!