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

The Archive Base Latest Questions

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

In response to my answer to a file-reading question , a commenter stated that

  • 0

In response to my answer to a file-reading question, a commenter stated that FileInputStream.read(byte[]) is “not guaranteed to fill the buffer.”

File file = /* ... */  
long len = file.length();
byte[] buffer = new byte[(int)len];
FileInputStream in = new FileInputStream(file);
in.read(buffer);

(The code assumes that the file length does not exceed 2GB)

Apart from an IOException, what could cause the read method to not retrieve the entire file contents?

EDIT:

The idea of the code (and the goal of the OP of the question I answered) is to read the entire file into a chunk of memory in one swoop, that’s why buffer_size = file_size.

  • 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-22T15:15:18+00:00Added an answer on May 22, 2026 at 3:15 pm

    Apart from an IOException, what could cause the read method to not retrieve the entire file contents?

    In my own API implementation, and on my home rolled file-system I simply choose to fill half the buffer…… just kidding.

    My point is that even if I wasn’t kidding, technically speaking it wouldn’t be a bug. It is a matter of method contract. This is the contract (documentation) in this case is:

    Reads up to b.length bytes of data from this input stream into an array of bytes.

    i.e., it gives no guarantees for filling the buffer.

    Depending on the API implementation, and perhaps on the file-system the read method may choose not to fill the buffer. It’s basically a question of what the contract of the method says.


    Bottom line: It probably works, but is not guaranteed to work.

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

Sidebar

Related Questions

In @mmalc's response to this question he states that In general you should not
This question and my answer below are mainly in response to an area of
I'm sending a file using the response << file.newInputStream (similar to this answer here
Note: Before reading this question and its answer, please check your input element has
I think the answer to this question is no but I do not have
This is a question that never has a proper answer, i have search the
Based on the response to this question: Why does C++ have header files and
A web application has no make file unlike C++ or anything like that. However,
I'm still struggling to Stream a file to the HTTP response in Pylons .
I was trying to answer another SO question when I hit upon some very

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.