<commit_before>package zutil.jee.upload;

import org.apache.commons.fileupload.ProgressListener;

import zutil.StringUtil;
import zutil.parser.json.JSONNode;
import zutil.parser.json.JSONNode.JSONType;


/**
 * This is a File Upload Listener that is used by Apache
 * Commons File Upload to monitor the progress of the 
 * uploaded file.
 */
public class FileUploadListener implements ProgressListener{
	private static final long serialVersionUID = 1L;
	public static enum Status{
		Initializing,
		Uploading,
		Processing,
		Done,
		Error
	}
	
	private String id;
	private volatile Status status;
	private volatile String filename;
	private volatile String message;
	private volatile long bytes = 0l;
	private volatile long length = 0l;
	private volatile int item = 0;
	private volatile long time;
	
	// Speed
	private volatile int speed;
	private volatile long speedRead;
	private volatile long speedTime;
	
	public FileUploadListener(){
		id = ""+(int)(Math.random()*Integer.MAX_VALUE);
		status = Status.Initializing;
	}
	
	public void update(long pBytesRead, long pContentLength, int pItems) {
		if(pContentLength < 0)	this.length = pBytesRead;
		else					this.length = pContentLength;
		this.bytes = pBytesRead;
		this.item = pItems;
		
		// Calculate Speed
		if(speedTime == 0 || speedTime+1000<System.currentTimeMillis()){
			speedTime = System.currentTimeMillis();
			speed = (int)(pBytesRead-speedRead);
			speedRead = pBytesRead;
		}

		// Set Status
		status = Status.Uploading;
		time = System.currentTimeMillis();
	}
	
	protected void setFileName(String filename){
		this.filename = filename;
	}
	protected void setStatus(Status status){
		this.status = status;
		time = System.currentTimeMillis();
	}
	protected void setMessage(String msg){
		this.message = msg;
	}
	
	
	public String getID(){
		return id;
	}
	
	public String getFilename() {
		return filename;
	}
	
	public long getBytesRead() {
		return bytes;
	}

	public long getContentLength() {
		return length;
	}

	public long getItem() {
		return item;
	}
	
	public Status getStatus(){
		return status;
	}
	
	protected long getTime(){
		return time;
	}
	
	protected String getMessage(){
		return message;
	}
	
	/**
	 * @return bytes per second
	 */
	public int getSpeed(){
		return speed;
	}

	/**
	 * Calculate the percent complete
	 */
	public int getPercentComplete(){
		if(length == 0)
			return 0;
		return (int)((100 * bytes) / length);
	}

	public JSONNode getJSON() {
		JSONNode node = new JSONNode( JSONType.Map );
		node.add("id", id);
		
		node.add("status", status.toString());
		node.add("message", message);
		node.add("filename", filename);
		node.add("percent", getPercentComplete());<commit_msg>Bug fix<commit_after>package zutil.jee.upload;

import org.apache.commons.fileupload.ProgressListener;

import zutil.StringUtil;
import zutil.parser.json.JSONNode;
import zutil.parser.json.JSONNode.JSONType;


/**
 * This is a File Upload Listener that is used by Apache
 * Commons File Upload to monitor the progress of the 
 * uploaded file.
 */
public class FileUploadListener implements ProgressListener{
	private static final long serialVersionUID = 1L;
	public static enum Status{
		Initializing,
		Uploading,
		Processing,
		Done,
		Error
	}
	
	private String id;
	private volatile Status status;
	private volatile String filename;
	private volatile String message;
	private volatile long bytes = 0l;
	private volatile long length = 0l;
	private volatile int item = 0;
	private volatile long time;
	
	// Speed
	private volatile int speed;
	private volatile long speedRead;
	private volatile long speedTime;
	
	public FileUploadListener(){
		id = ""+(int)(Math.random()*Integer.MAX_VALUE);
		status = Status.Initializing;
	}
	
	public void update(long pBytesRead, long pContentLength, int pItems) {
		if(pContentLength < 0)	this.length = pBytesRead;
		else					this.length = pContentLength;
		this.bytes = pBytesRead;
		this.item = pItems;
		
		// Calculate Speed
		if(speedTime == 0 || speedTime+1000<System.currentTimeMillis()){
			speedTime = System.currentTimeMillis();
			speed = (int)(pBytesRead-speedRead);
			speedRead = pBytesRead;
		}

		// Set Status
		status = Status.Uploading;
		time = System.currentTimeMillis();
	}
	
	protected void setFileName(String filename){
		this.filename = filename;
	}
	protected void setStatus(Status status){
		this.status = status;
		time = System.currentTimeMillis();
	}
	protected void setMessage(String msg){
		this.message = msg;
	}
	
	
	public String getID(){
		return id;
	}
	
	public String getFilename() {
		return filename;
	}
	
	public long getBytesRead() {
		return bytes;
	}

	public long getContentLength() {
		return length;
	}

	public long getItem() {
		return item;
	}
	
	public Status getStatus(){
		return status;
	}
	
	protected long getTime(){
		return time;
	}
	
	protected String getMessage(){
		return message;
	}
	
	/**
	 * @return bytes per second
	 */
	public int getSpeed(){
		return speed;
	}

	/**
	 * Calculate the percent complete
	 */
	public int getPercentComplete(){
		if(length == 0)
			return 0;
		return (int)((100 * bytes) / length);
	}

	public JSONNode getJSON() {
		JSONNode node = new JSONNode( JSONType.Map );
		node.add("id", id);
		
		node.add("status", status.toString());
		node.add("message", message.replaceAll("\"", "\\\""));
		node.add("filename", filename);
		node.add("percent", getPercentComplete());
{'fraction_non_alphanumeric': 0.10907067334446299, 'fraction_numerical': 0.005564830272676683, 'mean_word_length': 3.857657657657658, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3860875', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1621, 'n_words': 486}