Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 924091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:17:36+00:00 2026-05-15T19:17:36+00:00

I am faced with the following problem. I need to (de)serialize (binary) a stream

  • 0

I am faced with the following problem. I need to (de)serialize (binary) a stream of objects to a single file on disk. Serialization part is not an issue, just open a stream in append mode and use .Net’s BinaryFormatter Serialize method and you are done. The problem with this approach is that I can’t just give this stream to BinaryFormatter’s deserialize function, what it contains is not a single instance of the object I’ve serialized.

Does a common solution to this problem exists? All objects serialized to a given stream are of the same type, so at least we don’t need to figure out what is to be deserialized, that’s a given, but it doesn’t seem to suggest a way out of this to me.

Clarification based on replies: The number of objects sent in is expected to be large and it is therefore infeasible to hold them all in a wrapper collection (as flushing to disk would require to load them all into memory -> add the new ones -> flush to disk).

  • Normally when you serialize a single object you get a file that contains:

[Object]

  • What I am creating is a file that contains:

[Object][Object][Object][Object]…[Object]

And I need to deserialize the individual Object instances.

Thanks in advance!

Answer: Since the answer is alluded to in this thread (with sufficient clarity), but never explicitly stated, I thought I’ll state it here:

while (fileStream.Position < fileStream.Length)
    messages.Add((Message)formatter.Deserialize(fileStream));

The BinaryFormatter will deserialize one object at a time as desired 🙂 You might want to cache the fileStream.Length property, since the length appears to be re-computed every time you call the property, slowing things down. I’ve got no clue why that didn’t work the first time I tried it before posting this question, but it does work flawlessly now.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:17:36+00:00Added an answer on May 15, 2026 at 7:17 pm

    Try putting your objects into a serializable collection (I believe List is serializable), then (de)serializing that object

    EDIT in response to clarification:
    I just realized that this question has the same answer as this question. Rather than try and reinvent an answer, I’d just take a look at Mark Gravell’s answer, or to this one

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am faced with the following programming problem. I need to generate n (a,
I'm refactoring a library we currently use, and I'm faced with the following problem.
I faced following issue with my work: having two projects: MFC .dll and MFC
I faced the following issue while I submitting my form using jQuery FORM and
I'm faced with the following and I'm not sure what's best practice. Consider the
I am faced with the following issue and at this point I feel like
I've faced the following problem. I'm developing a form for the site and this
While working on ASP.NET MVC 2 web site I faced to the following problem.
We are faced with the following problem that involves keeping the following in sync:
I am stuck and need outside help from JSF experts with the following problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.