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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:42:58+00:00 2026-06-05T05:42:58+00:00

I found myself to solve a strange bug in my software: the problem is

  • 0

I found myself to solve a strange bug in my software: the problem is that it manifested only when i package my application into a runnable JAR.

The problem was in this simple code: i added loopCounter to count how many times the cycle is taken

private static byte[] read(InputStream source) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int loopCounter = 0;
    int bytesRead;

    try {
        byte[] buffer = new byte[4096]; 

        while ((bytesRead = source.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
            loopCounter++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return out.toByteArray();
}

An example:

source = ClassLoader.class.getResourceAsStream("file.lol");

loopCounter in Eclipse = 1366
loopCounter in JAR     = 1405

My question is: why this significative difference for the same InputStream?

EDIT : i change my code with the correct one, but the loopCounters are still different.

  • 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-05T05:42:59+00:00Added an answer on June 5, 2026 at 5:42 am

    InputStream.read() doesn’t guarantee to fill the whole buffer at once, therefore you need to keep track of the number of bytes actually read:

    byte[] buffer = new byte[4096];
    int bytesRead = 0;
    while ((bytesRead = source.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
        loopCounter++;
    } 
    

    So, it’s no surprise that number of bytes read per iteration may vary for different implementations of InputStream, and therefore the number of iterations may vary as well.

    Actually, number of bytes read by specific call of InputStream.read() depends on many factors.

    The first factor is implementation of InputStream: when you run your application from Eclipse, you use InputStream that reads resources directly from the file system, whereas when you run it from the jar file, you use InputStream that extracts resources form the jar file. Obviously, some internals of decompression algorithm for jar files may affect the size of chunks you get.

    Another factor is behaviour of underlying environment. For example, system calls that read file from the file system may return chunks of different sizes as well, depending on some internal behaviour of the operating system, and so on.

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

Sidebar

Related Questions

I have found myself designing a language for fun that is a cross between
I recently found myself writing a piece of code that executed a Core Data
I have linkers errors(undefined reference) that I cannot solve by myself. I use GCC
Many times I found out that I need a software to create simple educational
My problem is pretty simple, but I just can't solve it by myself. Does
I found this problem, which I thought would be interesting to solve but couldn't
I've found some code that solves a sudoku puzzle, but I only understand part
So recently I got into a debate about how to solve a problem, the
I found myself writing the following a lot: int location =2; vector<int> vec; vector<int>::iterator
I found myself instantiating the same objects in numerous tests, so I'm trying to

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.