Android Open Source - android-davsync Counting Input Stream Entity






From Project

Back to project page android-davsync.

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 android-davsync 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 net.zekjur.davsync;
//from w  w  w . j a  va  2 s .  com
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

net.zekjur.davsync.CountingInputStreamEntity.java
net.zekjur.davsync.DavSyncOpenHelper.java
net.zekjur.davsync.NetworkReceiver.java
net.zekjur.davsync.NewMediaReceiver.java
net.zekjur.davsync.SettingsActivity.java
net.zekjur.davsync.ShareActivity.java
net.zekjur.davsync.UploadService.java