Android Open Source - AndroidUploader Stream Uploader






From Project

Back to project page AndroidUploader.

License

The source code is released under:

Apache License

If you think the Android project AndroidUploader 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

package z.hol.uploader;
// w w  w. j  a  v a  2s .  c o m
import org.apache.http.entity.InputStreamEntity;
import z.hol.general.ConcurrentCanceler;
import z.hol.uploader.bridge.Bridge;
import z.hol.uploader.bridge.StreamBridge;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * ????
 * Created by holmes on 10/15/14.
 */
public class StreamUploader extends InputStreamEntity implements Uploader, Runnable{

    /**
     * ?????
     */
    protected InputStream mSrcStream;
    private StreamBridge mBridge;

    /** ????????? */
    private long mTotal = 0l;
    /** ??????? */
    private long mCurrent = 0l;
    /** ????? */
    private ConcurrentCanceler mCanceler;

    /** ??????? */
    private boolean mConsumed = false;

    /** id */
    private Integer mId;

    private UploadUiHandler mUiHandler;

    public StreamUploader(InputStream inputStream, long length, StreamBridge bridge){
        super(inputStream, length);
        mTotal = length;
        mSrcStream = inputStream;
        mBridge = bridge;
        mCanceler = new ConcurrentCanceler();
    }

    public void setUiHandler(UploadUiHandler handler){
        mUiHandler = handler;
    }

    @Override
    public int getId() {
        if (mId == null){
            mId = super.hashCode();
        }
        return mId.intValue();
    }

    @Override
    public void setId(int id) {
        if (mId == null ||
                mId.intValue() != id){
            mId = id;
        }
    }

    /**
     * ???????????.
     * ??????????????
     * @param total
     */
    protected void setTotal(long total){
        mTotal = total;
    }

    /**
     * ?????????
     * @return
     */
    public long getTotal(){
        return mTotal;
    }

    /**
     * ????????. ???0
     * @param current
     */
    protected void setCurrent(long current){
        mCurrent = current;
    }

    /**
     * ???????
     * @return
     */
    public long getCurrent(){
        return mCurrent;
    }

    @Override
    public void setBridge(Bridge bridge) {
        if (bridge instanceof StreamBridge){
            mBridge = (StreamBridge) bridge;
        }else {
            throw new IllegalArgumentException("Only StreamBridge allowed!");
        }
    }

    @Override
    public Bridge getBridge() {
        return mBridge;
    }

    @Override
    public void uploader() {
        mCanceler.restore();
        onUploadStart();
        //internalUploader();
    }

    @Override
    public void run() {

    }

    @Override
    public void cancel() {
        mCanceler.cancel();
    }

    public boolean isCanceled(){
        return mCanceler.isCanceled();
    }

    /**
     * ??, ????????
     */
    protected void internalUploader(OutputStream out){
        if (mBridge == null){
            onError(1);
            return;
        }
        mBridge.setOutStream(out);

        byte[] buff = new byte[4096];
        int readed = -1;

        // ??
        while (true) {
            if (mCanceler.isCanceled()){
                onCancel();
                break;
            }

            try {
                readed = mSrcStream.read(buff);
            } catch (IOException e) {
                e.printStackTrace();
                onError(2);
                break;
            }

            if (mCanceler.isCanceled()){
                onCancel();
                break;
            }
            if (readed != -1){
                int uploaded;
                try {
                    uploaded = mBridge.uploadData(buff, readed);
                } catch (IOException e) {
                    e.printStackTrace();
                    onError(3);
                    break;
                }
                mCurrent += uploaded;
                onProgress(mTotal, mCurrent);
            }else {
                if (mCurrent != mTotal){
                    mCurrent = mTotal;
                    onProgress(mTotal, mCurrent);
                }
                onCompleted();
                break;
            }
        }

        onUploadFinished();
    }

    @Override
    public void writeTo(OutputStream outstream) throws IOException {
        //super.writeTo(outstream);
        internalUploader(outstream);
        this.mConsumed = true;
    }

    @Override
    public boolean isStreaming() {
        return !this.mConsumed;
    }

    @Override
    public void consumeContent() throws IOException {
        this.mConsumed = true;
        mSrcStream.close();
    }

    /**
     * ????
     */
    protected void onUploadStart(){
        if (mUiHandler != null){
            mUiHandler.uploadStart(getId());
        }
    }

    /**
     * ??????????????, ???????????
     */
    protected void onUploadFinished(){
        if (mUiHandler != null){
            mUiHandler.uploadFinished(getId());
        }

    }

    /**
     * ??
     * @param code
     */
    protected void onError(int code){
        if (mUiHandler != null){
            mUiHandler.error(getId(), code);
        }

    }

    /**
     * ???
     */
    protected void onCompleted(){
        if (mUiHandler != null){
            mUiHandler.completed(getId());
        }

    }

    /**
     * ??
     * @param total
     * @param current
     */
    protected void onProgress(long total, long current){
        if (mUiHandler != null){
            mUiHandler.progress(getId(), total, current);
        }

    }

    /**
     * ?????
     */
    protected void onCancel(){
        if (mUiHandler != null){
            mUiHandler.cancel(getId());
        }

    }
}




Java Source Code List

z.hol.uploader.FileHttpUploader.java
z.hol.uploader.HttpUploader.java
z.hol.uploader.StreamUploader.java
z.hol.uploader.UploadStateCallback.java
z.hol.uploader.UploadUiHandler.java
z.hol.uploader.Uploader.java
z.hol.uploader.bridge.Bridge.java
z.hol.uploader.bridge.HttpBridge.java
z.hol.uploader.bridge.StreamBridge.java