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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:21:21+00:00 2026-06-11T01:21:21+00:00

This is probably a often asked question but I couldn’t find a right answer

  • 0

This is probably a often asked question but I couldn’t find a right answer yet.

Well, I’ve got the following code:

java.net.URL url = new java.net.URL(built);
java.net.HttpURLConnection con = (HttpURLConnection)url.openConnection();

if (con.getResponseCode() != 200) {
    // error handle here!;
    continue;
}

// begin to download the file
int file_size = con.getContentLength();

FileOutputStream stream = new FileOutputStream(m_WorkingDir + "/" + getFilenameWithPath(i));
InputStream remoteStream = con.getInputStream();

int chunks = (int) Math.ceil((float)file_size / (float)CHUNK_SIZE);

// download each chunk
byte[] temp = new byte[CHUNK_SIZE];
for(int a = 0; a < chunks; a++) {
    // calculate chunk size
    int chunk_size = CHUNK_SIZE;

    if(a == chunks-1) {
        // last chunk
        chunk_size = file_size - a * CHUNK_SIZE;
        System.out.println("Download last chunk : " + chunk_size);
    }

    // download chunk
    int bytes = remoteStream.read(temp, 0, chunk_size);
    stream.write(temp, 0 ,chunk_size); // save to local filesystem
}

stream.close();
remoteStream.close();
con.disconnect();

This code “should” simply download the file chunked.. but the point is that it doesn’t do it correctly. I debugged the code and it turned out that it reads up to ~10 chunks correctly but then it reads like the half of chunk_size even if its not the last chunk, and then it returns -1 until the for(int a…) has finished.

To me it seems like the InputStream think it is EOF even if it is not! And yes I tested the HTTP Connection over a usual browser and it worked correctly.

It tested the code with multiple settings for CHUNK_SIZE but it had always the same result.

The file I want to download is about 10~Mbyte ..

  • 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-06-11T01:21:23+00:00Added an answer on June 11, 2026 at 1:21 am

    You are assuming program to be downloading whole chunk size, but it may not be the case, because java says:-

    An attempt is made to read as many as len(CHUNK_SIZE) bytes, but a smaller number
    may be read, possibly zero.

    This may work:-

    byte[] temp = new byte[CHUNK_SIZE];
    int bytes = 0;
    for(int a = 0; a < chunks; a++) {
        bytes =remoteStream.read(temp, 0, CHUNK_SIZE);
        if(bytes == -1) {
          System.out.println("Downloaded last chunk : Terminating ");
          break;
        }
        stream.write(temp, 0 ,bytes);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably has probably been asked before, but I couldn't find anything relevant. Would
This probably is a dummy question but I cannot find a clear indication. I
This is probably a really simple question, but I don't write stored procedures often
Hello this is probably a typical question but i cant seem to find a
I know this probably has been asked before but I am having issues with
Im sorry for this probably dumm question, but I want to simply open modals
Probably this is often problem, but I can't solve it. I need to automaticallly
Ok this might be a brain dead question but I am curious. I often
This question is probably language-agnostic, but I'll focus on the specified languages. While working
It's probably an already asked question, but I just don't know what are the

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.