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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:16:54+00:00 2026-05-19T15:16:54+00:00

I just have a function here that caller wants number of bytes and then

  • 0

I just have a function here that caller wants number of bytes and then it returns the bytes but if there are not enough bytes in file it should return an smaller array. is there better approach to do this? I mean not getting 2 array and use BlockCopy?

byte[] GetPartialPackage(string filePath, long offset, int count)
{
    using (var reader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        reader.Seek(offset, SeekOrigin.Begin);
        byte[] tempData = new byte[count];
        int num = reader.Read(tempData, 0, count);
        byte[] tempdata = new byte[num];
        Buffer.BlockCopy(tempData, 0, tempdata, 0, num);
        return tempdata;
    }
}
  • 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-19T15:16:55+00:00Added an answer on May 19, 2026 at 3:16 pm

    Just update the count based on the length of the stream to shorten it if necessary.

    byte[] GetPartialPackage(string filePath, long offset, int count)
    {
        using (var reader = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            reader.Seek(offset, SeekOrigin.Begin);
            int avaliableCount = Math.Min(count, (int)(reader.Length - offset));
            byte[] tempData = new byte[avaliableCount];
            int num = reader.Read(tempData, 0, avaliableCount);
            return tempData;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note that this function does not have a { and } body. Just a
Just a little confused here... I have a function in postgres, and when I'm
I have a function (for ease, I'll just use count()) that I want to
I have a function that prepends a li tag to a parent div just
I have written a function that positions a tooltip just above a textbox. The
I want to have a function defined in a superclass that returns an instance
I have just come across this code: function test(){ //... if ( $profilerule ==
We have a JavaScript function named move which does just windows.location.href = any given
i have custom cell with 2 buttons(the function of these buttons is just to
I have the following JQuery function about a range slider . It's just working

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.