Android Open Source - dCache-Cloud Counting Input Stream Entity






From Project

Back to project page dCache-Cloud.

License

The source code is released under:

Copyright ? 2013, Michael Stapelberg and contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following ...

If you think the Android project dCache-Cloud listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

// Based on code from Ben Hardill:
// http://www.hardill.me.uk/wordpress/?p=646
package de.desy.dCacheCloud;
//from w w  w  .  j  av a  2  s. c om
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.entity.InputStreamEntity;

public class CountingInputStreamEntity extends InputStreamEntity {
  private UploadListener listener;
  private long length;

  public CountingInputStreamEntity(InputStream instream, long length) {
    super(instream, length);
    this.length = length;
  }

  public void setUploadListener(UploadListener listener) {
    this.listener = listener;
  }

  @Override
  public void writeTo(OutputStream outstream) throws IOException {
    super.writeTo(new CountingOutputStream(outstream));
  }

  class CountingOutputStream extends OutputStream {
    private long counter = 0l;
    private int lastPercent = 0;
    private OutputStream outputStream;

    public CountingOutputStream(OutputStream outputStream) {
      this.outputStream = outputStream;
    }

    @Override
    public void write(int oneByte) throws IOException {
      this.outputStream.write(oneByte);
      counter++;
      if (listener != null) {
        int percent = (int) ((counter * 100) / length);
        // NB: We need to call this only when the percentage actually
        // changed, otherwise updating the notification will churn
        // through memory far too quickly.
        if (lastPercent != percent) {
          listener.onChange(percent);
          lastPercent = percent;
        }
      }
    }
  }

  public interface UploadListener {
    public void onChange(int percent);
  }
}




Java Source Code List

External.Contents.java
External.IntentIntegrator.java
External.IntentResult.java
External.QRCodeEncoder.java
de.desy.dCacheCloud.CountingInputStreamEntity.java
de.desy.dCacheCloud.CryptoHelper.java
de.desy.dCacheCloud.DatabaseHelper.java
de.desy.dCacheCloud.DownloadService.java
de.desy.dCacheCloud.KeyStoreHelper.java
de.desy.dCacheCloud.MySSLSocketFactory.java
de.desy.dCacheCloud.ServerHelper.java
de.desy.dCacheCloud.UploadService.java
de.desy.dCacheCloud.Activities.FriendFoundActivity.java
de.desy.dCacheCloud.Activities.ImportDataActivity.java
de.desy.dCacheCloud.Activities.MainActivity.java
de.desy.dCacheCloud.Activities.ProfileActivity.java
de.desy.dCacheCloud.Activities.ServerViewActivity.java
de.desy.dCacheCloud.Activities.SettingsActivity.java
de.desy.dCacheCloud.Activities.ShareDataActivity.java
de.desy.dCacheCloud.Activities.ShareWithFriendActivity.java
de.desy.dCacheCloud.Activities.UploadActivity.java
de.desy.dCacheCloud.Activities.UserPasswordActivity.java
de.desy.dCacheCloud.BCReceiver.NetworkReceiver.java
de.desy.dCacheCloud.BCReceiver.NewMediaReceiver.java