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 7601131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:05:39+00:00 2026-05-30T23:05:39+00:00

I have a binary file specification that describes a packetized data structure. Each data

  • 0

I have a binary file specification that describes a packetized data structure. Each data packet has a two-byte sync pattern, so scanning for the beginning of a packet is possible, using a BinaryReader and FileStream combination:

while(!reader.EndOfFile)
{
    // Check for sync pattern.
    if (reader.ReadUInt16() != 0xEB25)
    {
        // Move to next byte.
        reader.BaseStream.Seek(-1, SeekOrigin.Current);
        continue;
    }

    // If we got here, a sync pattern was found.
}

This process works perfectly fine in the forward direction, but similar code scanning in the reverse direction is at least two orders of magnitude slower:

while(!reader.BeginningOfFile)
{
    // Check for sync pattern.
    if (reader.ReadUInt16() != 0xEB25)
    {
        // Move to previous byte.
        reader.BaseStream.Seek(-3, SeekOrigin.Current);
        continue;
    }

    // If we got here, a sync pattern was found.
}

I’ve tried a few workarounds, like moving back by an arbitrary amount (currently 1 megabyte) and scanning forward, but it’s becoming clear that what I really need is a BinaryReader or FileStream that is modified to have adequate performance characteristics when reading in both forward and reverse directions.

I already have a FastFileStream which improves forward read performance by subclassing an ordinary FileStream and caching the Position and Length properties (it also provides the BeginningOfFile and EndOfFile properties). That’s what drives the reader variable in the code above.

Is there something similar I could do to improve reverse reading performance, perhaps by incorporating a MemoryStream as a buffer?

  • 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-30T23:05:41+00:00Added an answer on May 30, 2026 at 11:05 pm

    L.B mentioned in a comment to use a Memory Mapped file, you may be impressed with the performance.

    Please try something like this:

    var memoryMapName = Path.GetFileName(fileToRead);
    
    using (var mapStream = new FileStream(fileToRead, FileMode.Open))
    {
        using (var myMap = MemoryMappedFile.CreateFromFile(
                                mapStream, 
                                memoryMapName, mapStream.Length,
                                MemoryMappedFileAccess.Read, null, 
                                HandleInheritability.None, false))
        {                    
            long leftToRead = mapStream.Length;
            long mapSize = Math.Min(1024 * 1024, mapStream.Length);
            long bytesRead = 0;
            long mapOffset = Math.Max(mapStream.Length - mapSize, 0);
    
            while (leftToRead > 1)
            {
                using (var FileMap = myMap.CreateViewAccessor(mapOffset, 
                                     mapSize, MemoryMappedFileAccess.Read))
                {
                    long readAt = mapSize - 2;
                    while (readAt > -1)
                    {
                        var int16Read = FileMap.ReadUInt16(readAt);
                        //0xEB25  <--check int16Read here                            
                        bytesRead += 1;
                        readAt -= 1;
                    }
                }
    
                leftToRead = mapStream.Length- bytesRead;
                mapOffset = Math.Max(mapOffset - mapSize, 0);
                mapSize = Math.Min(mapSize, leftToRead);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have binary data in a file that I can read into a byte
I have a binary file, inside of which has multiple frames. Each frame starts
I have file with binary data. This is specification: The SRTM data files have
I have a large binary file that represents the alpha channel for each pixel
I have binary data in a file (a list of 32-bit integer values) that
Suppose we have a binary file, that contains 32 bit numbers. Each 32bit number
I have a binary file in CSV format that contains multiple records. Each record
I have a binary file that I have to parse and I'm using Python.
If I have a large binary file (say it has 100,000,000 floats), is there
I have a binary file of doubles that I need to load using C++.

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.