All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xiaomi.infra.galaxy.fds.client.model.FDSProgressInputStream Maven / Gradle / Ivy

The newest version!
package com.xiaomi.infra.galaxy.fds.client.model;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by maxiaoxin on 17-6-21.
 */
public class FDSProgressInputStream extends FilterInputStream{
	private ProgressListener listener;
	private long lastNotifyTime;

	public FDSProgressInputStream(InputStream in, ProgressListener listener) {
		super(in);
		this.listener = listener;
		this.lastNotifyTime = System.currentTimeMillis();
	}

	private void notifyListener(boolean needsCheckTime) {
		if (listener != null) {
			long now = System.currentTimeMillis();
			if (!needsCheckTime || now - lastNotifyTime >= listener.progressInterval()) {
				lastNotifyTime = now;
				listener.onProgress(listener.getTransferred(), listener.getTotal());
			}
		}
	}

	@Override
	public int read(byte[] buffer, int byteOffset, int byteCount)
		throws IOException {
		int bytesRead = super.read(buffer, byteOffset, byteCount);
		if (bytesRead != -1 && listener != null) {
			listener.transfer(bytesRead);
			notifyListener(true);
		}
		return bytesRead;
	}

	@Override
	public int read() throws IOException {
		int data = super.read();
		if (data != -1 && listener != null) {
			listener.transfer(1);
			notifyListener(true);
		}
		return data;
	}

	@Override
	public void close() throws IOException {
		super.close();
		notifyListener(false);
	}

	public ProgressListener getListener() {
		return listener;
	}

	public void setListener(ProgressListener listener) {
		this.listener = listener;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy