0

I have an upload with a BufferedInputStream and a BufferedOutputStream and now I want to get a progress in percent of this upload.

How to get this??

     BufferedInputStream bis = null;
     BufferedOutputStream bos = null;
     try
     {
        URL url = new URL( sb.toString() );
        URLConnection urlc = url.openConnection();

        bos = new BufferedOutputStream( urlc.getOutputStream() );
        bis = new BufferedInputStream( new FileInputStream( source ) );

        int i;
        int progress = 0;
        while ((i = bis.read()) != -1)
        {
           bos.write( i );
           //how to get the progress here?!

        }
     }
     finally
     {
        if (bis != null)
           try
           {
              bis.close();
           }
           catch (IOException ioe)
           {
              ioe.printStackTrace();
           }
        if (bos != null)
           try
           {
              bos.close();
           }
           catch (IOException ioe)
           {
              ioe.printStackTrace();
           }

     }
bbholzbb
  • 1,832
  • 3
  • 19
  • 28

1 Answers1

1

No problem. You can use CountingInputStream and CountingOutputStream wrappers for these purposes. See Apache Commons IO 2.5 library.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Is there a way to do this with a common library which is included in java? – bbholzbb Apr 29 '13 at 19:55
  • Well, actually you could implement these simple wrappers by your self, just see how they are constructed (Commons library is open-source). But there aren't ready classes like `Counint*Stream` like these in standard Java classes. – Andremoniy Apr 29 '13 at 20:01