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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:14:18+00:00 2026-05-27T11:14:18+00:00

I’m trying to see if there is a way I can do away with

  • 0

I’m trying to see if there is a way I can do away with Reading in my Stream to a MemoryStream before deserialising the Object that has been stored via ProtoBuf.Net.

I can’t use the Protobuf.Net With LengthPrefix features because I am coming back and adjusting the records in the File as new records are written so that the file can be traversed backward or forwards. The Serialized Class requires no changes because I keep the 3 length separate to actual class.

This is the code I’m currently using to read an entry in:

Private Function ReadEntry(ByVal br As BinaryReader) As PacketErrorLogEntry
    Dim activeRecord As PacketErrorLogEntry

    Dim OffsetPrevious As UInt32 = br.ReadUInt32()
    Dim RecordLength As UInt32 = br.ReadUInt32
    Dim OffsetNext As UInt32 = br.ReadUInt32
    Using ms As New MemoryStream
        Dim readLength As UInt32
        Dim bytesRead As UInt32
        Dim writeBuffer As Byte() = CType(Array.CreateInstance(GetType(Byte), _ 
                                          4096), Byte())
        Dim bytesToRead As UInt32 = CType(writeBuffer.Length, UInt32)
        If bytesToRead > RecordLength Then
            bytesToRead = RecordLength
        End If
        bytesRead = 0

        While readLength < RecordLength
            bytesRead = CType(br.BaseStream.Read(writeBuffer, 0, _ 
                              CType(bytesToRead, Integer)), UInt32)
            ms.Write(writeBuffer, 0, CType(bytesRead, Integer))
            readLength += bytesRead
        End While

        ms.Flush()
        ms.Position = 0
        activeRecord = Serializer.Deserialize(Of PacketErrorLogEntry)(ms)
        activeRecord.PreviousRecordLocation = OffsetPrevious
        activeRecord.NextRecordLocation = OffsetNext
    End Using
    'activeRecord = Serializer.Deserialize(Of PacketErrorLogEntry)(br.BaseStream, RecordLength)
    'activeRecord.PreviousRecordLocation = OffsetPrevious
    'activeRecord.NextRecordLocation = OffsetNext


    Return activeRecord
End Function

What I was hoping I might be able to achieve is that by passing the Length to Read to the Deserialize function then I’d be able to do away with the whole MemoryStream block and just get my object back.

I use the BinaryReader/Writer for the Length/Offsets so that I can come back later and just overwrite those positions with updated values.

  • 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-27T11:14:18+00:00Added an answer on May 27, 2026 at 11:14 am

    If you are using v2, then this is available on the TypeModel API (which is actually the primary API; the Serializer.Deserialize<T> API just calls into RuntimeTypeModel.Default.Deserialize). There are overloads that accept the number of bytes to consume. Once such method (on a TypeModel instance, most likely RuntimeTypeModel.Default) is:

    /// <summary>
    /// Applies a protocol-buffer stream to an existing instance (which may be null).
    /// </summary>
    /// <param name="type">The type (including inheritance) to consider.</param>
    /// <param name="value">The existing instance to be modified (can be null).</param>
    /// <param name="source">The binary stream to apply to the instance (cannot be null).</param>
    /// <param name="length">The number of bytes to consume.</param>
    /// <returns>The updated instance; this may be different to the instance argument if
    /// either the original instance was null, or the stream defines a known sub-type of the
    /// original instance.</returns>
    public object Deserialize(Stream source, object value, Type type, int length);
    

    I should also note that the “with length prefix” API also allow different prefix-styles, with fixed-length (either big-endian or little-endian) int32 available. But your current approach should work fine too.

    If you are using v1, you may have to manufacture a length-limited stream…. or just borrow the one that v1 uses internally (note that v2 doesn’t use this approach – it tracks the remaining active bytes on a single stream plus buffer).

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I know there's a lot of other questions out there that deal with this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.