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

  • Home
  • SEARCH
  • 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 544633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:40:35+00:00 2026-05-13T10:40:35+00:00

What are possible ways to save string arrays to a stream without using serialization?

  • 0

What are possible ways to save string arrays to a stream without using serialization?

I’m particularly interested in strings since their lengths may vary. I also should be able to restore the array from stream.
And, more importantly, I would like to be able to read only slices of an array without reading full array into memory, because potentially my arrays can be huge.

P.S. I know that there exist databases, that I shouldn’t reinvent the wheel, etc, but I have my reasons to opt for hand made solution.

Thank you.

  • 1 1 Answer
  • 0 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-13T10:40:35+00:00Added an answer on May 13, 2026 at 10:40 am

    Well, saving data to a stream is serialization; the real trick is: what kind. For example, I assume you’re talking about things like XmlSerializer or BinaryFormatter that require you to deserialize the whole thing, but that isn’t always necessary.

    By writing each string with a length-prefix, you should be able to seek past items you don’t want pretty easily. The other option is to write (separately) an index of offsets, but that is sometimes overkill.

    As a basic example, s here is "jkl", without it reading the entire stream or deserializing the unwanted strings; note that it could be optimized by (for example) using a variable-length encoding for the int (length), which would also fix the current assumption that endianness is the same between reader and writer:

    static void Main()
    {
        byte[] raw;
        using (MemoryStream ms = new MemoryStream())
        {
            // serialize all
            List<string> data = new List<string> {
               "abc", "def", "ghi", "jkl", "mno", "pqr" };
            foreach (string s in data)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(s);
                byte[] lenBuffer = BitConverter.GetBytes(buffer.Length);
                ms.Write(lenBuffer, 0, lenBuffer.Length);
                ms.Write(buffer, 0, buffer.Length);
            }
            raw = ms.ToArray();
        }
        using (MemoryStream ms = new MemoryStream(raw))
        {
            int offset = 3, len;
            byte[] buffer = new byte[128];
            while (offset-- > 0)
            {
                Read(ms, ref buffer, 4);
                len = BitConverter.ToInt32(buffer, 0);
                ms.Seek(len, SeekOrigin.Current); // assume seekable, but
                                                  // easy to read past if not
            }
            Read(ms, ref buffer, 4);
            len = BitConverter.ToInt32(buffer, 0);
            Read(ms, ref buffer, len);
            string s = Encoding.UTF8.GetString(buffer, 0, len);
        }
    }
    static void Read(Stream stream, ref byte[] buffer, int count)
    {
        if (buffer.Length < count) buffer = new byte[count];
        int offset = 0;
        while (count > 0)
        {
            int bytes = stream.Read(buffer, offset, count);
            if (bytes <= 0) throw new EndOfStreamException();
            offset += bytes;
            count -= bytes;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 287k
  • Answers 287k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The ViewModel can expose this property from the Model to… May 13, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Lazy loading and eager loading are concepts that must be… May 13, 2026 at 5:03 pm
  • Editorial Team
    Editorial Team added an answer Add a new install project to your solution. Add targets… May 13, 2026 at 5:03 pm

Related Questions

I remember the old effective approach of studying a new framework. It was always
What is the typical way to handle product pictures in a web-page selling products?
While analyzing some approaches to making our web app easier to deploy on our
One of the feature requests I've got for the program I'm working on is
The setup: I use Linq to Sql for my DAL and I extend the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.