Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

track progress of copying files

I am trying to track the progress of a compression progress. ATM I am doing it like this:

public static void compressGzipTest(final OutputStream os, final File source) throws CompressorException,
            IOException
    {
        final CountingInputStream cis = new CountingInputStream(new FileInputStream(source));
        final GzipCompressorOutputStream gzipOut = (GzipCompressorOutputStream) new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.GZIP,os);

        new Thread() {
            public void run()
            {
                try
                {
                    long fileSize = source.length();

                    while (fileSize > cis.getBytesRead())
                    {
                        Thread.sleep(1000);
                        System.out.println(cis.getBytesRead() / (fileSize / 100.0));
                    }
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }.start();

        IOUtils.copy(cis,gzipOut);
    }

This works fine, but I need the thread, which is giving feedback about the progress not to be implemented in this method, but when calling it (in order to create something like a progressbar on an android device). So this is more like an architectural issue. Any ideas, on how to solve that?

like image 913
Jonas Bausch Avatar asked Nov 21 '25 03:11

Jonas Bausch


1 Answers

I meanwhile solved it via overwriting the IOUtils.copy() by adding an interface as parameter:

public static long copy(final InputStream input, final OutputStream output, int buffersize,
        ProgressListener listener) throws IOException
{
    final byte[] buffer = new byte[buffersize];
    int n = 0;
    long count = 0;
    while (-1 != (n = input.read(buffer)))
    {
        output.write(buffer,0,n);
        count += n;
        listener.onProgress(n);
    }
    return count;
}

which is then called by something like this

copy(input, output, 4096, new ProgressListener() {

                long totalCounter = 0;

                DecimalFormat f = new DecimalFormat("#0.00");

                @Override
                public void onProgress(long bytesRead)
                {
                    totalCounter += bytesRead;
                    System.out.println(f.format(totalCounter / (fileSize / 100.0)));
                }
            });

The only thing, i am challenged by now, is to limit the output on the console not for each byte[4096] but for let's say each two megabyte. I tried something like this:

while (-1 != (n = input.read(buffer)))
    {
        output.write(buffer,0,n);
        count += n;
        while(n % 2097152 == 0)
        {
          listener.onProgress(n);
        }
    }
    return count;

But that does not give me any output at all

like image 66
Jonas Bausch Avatar answered Nov 22 '25 17:11

Jonas Bausch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!