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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:20:17+00:00 2026-05-12T10:20:17+00:00

So, I’m feeding file data to an API that takes a Reader , and

  • 0

So, I’m feeding file data to an API that takes a Reader, and I’d like a way to report progress.

It seems like it should be straightforward to write a FilterInputStream implementation that wraps the FileInputStream, keeps track of the number of bytes read vs. the total file size, and fires some event (or, calls some update() method) to report fractional progress.

(Alternatively, it could report absolute bytes read, and somebody else could do the math — maybe more generally useful in the case of other streaming situations.)

I know I’ve seen this before and I may even have done it before, but I can’t find the code and I’m lazy. Has anyone got it laying around? Or can someone suggest a better approach?


One year (and a bit) later…

I implemented a solution based on Adamski’s answer below, and it worked, but after some months of usage I wouldn’t recommend it. When you have a lot of updates, firing/handling unnecessary progress events becomes a huge cost. The basic counting mechanism is fine, but much better to have whoever cares about the progress poll for it, rather than pushing it to them.

(If you know the total size, you can try only firing an event every > 1% change or whatever, but it’s not really worth the trouble. And often, you don’t.)

  • 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-12T10:20:17+00:00Added an answer on May 12, 2026 at 10:20 am

    Here’s a fairly basic implementation that fires PropertyChangeEvents when additional bytes are read. Some caveats:

    • The class does not support mark or reset operations, although these would be easy to add.
    • The class does not check whether the total number bytes read ever exceeds the maximum number of bytes anticipated, although this could always be dealt with by client code when displaying progress.
    • I haven’t test the code.

    Code:

    public class ProgressInputStream extends FilterInputStream {
        private final PropertyChangeSupport propertyChangeSupport;
        private final long maxNumBytes;
        private volatile long totalNumBytesRead;
    
        public ProgressInputStream(InputStream in, long maxNumBytes) {
            super(in);
            this.propertyChangeSupport = new PropertyChangeSupport(this);
            this.maxNumBytes = maxNumBytes;
        }
    
        public long getMaxNumBytes() {
            return maxNumBytes;
        }
    
        public long getTotalNumBytesRead() {
            return totalNumBytesRead;
        }
    
        public void addPropertyChangeListener(PropertyChangeListener l) {
            propertyChangeSupport.addPropertyChangeListener(l);
        }
    
        public void removePropertyChangeListener(PropertyChangeListener l) {
            propertyChangeSupport.removePropertyChangeListener(l);
        }
    
        @Override
        public int read() throws IOException {
            int b = super.read();
            updateProgress(1);
            return b;
        }
    
        @Override
        public int read(byte[] b) throws IOException {
            return (int)updateProgress(super.read(b));
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException {
            return (int)updateProgress(super.read(b, off, len));
        }
    
        @Override
        public long skip(long n) throws IOException {
            return updateProgress(super.skip(n));
        }
    
        @Override
        public void mark(int readlimit) {
            throw new UnsupportedOperationException();
        }
    
        @Override
        public void reset() throws IOException {
            throw new UnsupportedOperationException();
        }
    
        @Override
        public boolean markSupported() {
            return false;
        }
    
        private long updateProgress(long numBytesRead) {
            if (numBytesRead > 0) {
                long oldTotalNumBytesRead = this.totalNumBytesRead;
                this.totalNumBytesRead += numBytesRead;
                propertyChangeSupport.firePropertyChange("totalNumBytesRead", oldTotalNumBytesRead, this.totalNumBytesRead);
            }
    
            return numBytesRead;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 182k
  • Answers 182k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The original iPhone and the iPhone 3G use GPS to… May 12, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer I guess you need to use FillBehaviour of the POintAnimation… May 12, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer According to How to: Debug 64-Bit Applications on MSDN, 32-to-64… May 12, 2026 at 4:28 pm

Related Questions

In order to apply a triggered animation to all ToolTip s in my app,
this is what i have right now Drawing an RSS feed into the php,
I have text I am displaying in SIlverlight that is coming from a CMS
I have a French site that I want to parse, but am running into
So, I've read that it is not a good idea to install VS2008 on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.