<commit_before>package com.mnm.asynctaskmanager.core;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.accreativos.R;

public final class Task extends AsyncTask<Void, String, String> {

	protected final Resources mResources;
	protected final InputStream is;
	protected final String existingFileName;

	private String mResult;
	private String mProgressMessage;
	private IProgressTracker mProgressTracker;

	/* UI Thread */
	public Task(Resources resources, InputStream is, String existingFileName) {
		// Keep reference to resources
		mResources = resources;
		this.is = is;
		this.existingFileName = existingFileName;
		// Initialise initial pre-execute message
		mProgressMessage = resources.getString(R.string.task_starting);
	}

	/* UI Thread */
	public void setProgressTracker(IProgressTracker progressTracker) {
		// Attach to progress tracker
		mProgressTracker = progressTracker;
		// Initialise progress tracker with current task state
		if (mProgressTracker != null) {
			mProgressTracker.onProgress(mProgressMessage);
			if (mResult != null) {
				mProgressTracker.onComplete();
			}
		}
	}

	/* UI Thread */
	@Override
	protected void onCancelled() {
		// Detach from progress tracker
		mProgressTracker = null;
	}

	/* UI Thread */
	@Override
	protected void onProgressUpdate(String... values) {
		// Update progress message
		mProgressMessage = values[0];
		// And send it to progress tracker
		if (mProgressTracker != null) {
			mProgressTracker.onProgress(mProgressMessage);
		}
	}

	/* UI Thread */
	@Override
	protected void onPostExecute(String result) {
		// Update result
		mResult = result;
		// And send it to progress tracker
		if (mProgressTracker != null) {
			mProgressTracker.onComplete();
		}
		// Detach from progress tracker
		mProgressTracker = null;
	}

	/* Separate Thread */
	@Override
	protected String doInBackground(Void... arg0) {
		// Working in separate thread

		if (isCancelled()) {
			// This return causes onPostExecute call on UI thread
			return "Cancelado";
		}

		// This call causes onProgressUpdate call on UI thread
		publishProgress(mResources.getString(R.string.task_working, 0));
	
		byte[] data = getBytesFromFile(is);
	    
		// This return causes onPostExecute call on UI thread
		return doFileUpload(existingFileName,data);
	}

	private String doFileUpload(String existingFileName, byte[] buffer) {

		HttpURLConnection conn = null;
		DataOutputStream dos = null;
		DataInputStream inStream = null;
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		String urlString = "http://192.168.1.151:8080/image/attached";

		try {
			// ------------------ CLIENT REQUEST
			// open a URL connection to the Servlet
			URL url = new URL(urlString);
			// Open a HTTP connection to the URL
			conn = (HttpURLConnection) url.openConnection();
			// Allow Inputs
			conn.setDoInput(true);
			// Allow Outputs
			conn.setDoOutput(true);
			// Don't use a cached copy.
			conn.setUseCaches(false);
			// Use a post method.
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);

			dos = new DataOutputStream(conn.getOutputStream());
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\""
					+ existingFileName + "\"" + lineEnd);
			dos.writeBytes(lineEnd);

			dos.write(buffer, 0, buffer.length);<commit_msg>Dejamos apuntando a pro<commit_after>package com.mnm.asynctaskmanager.core;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.accreativos.R;

public final class Task extends AsyncTask<Void, String, String> {

	protected final Resources mResources;
	protected final InputStream is;
	protected final String existingFileName;

	private String mResult;
	private String mProgressMessage;
	private IProgressTracker mProgressTracker;

	/* UI Thread */
	public Task(Resources resources, InputStream is, String existingFileName) {
		// Keep reference to resources
		mResources = resources;
		this.is = is;
		this.existingFileName = existingFileName;
		// Initialise initial pre-execute message
		mProgressMessage = resources.getString(R.string.task_starting);
	}

	/* UI Thread */
	public void setProgressTracker(IProgressTracker progressTracker) {
		// Attach to progress tracker
		mProgressTracker = progressTracker;
		// Initialise progress tracker with current task state
		if (mProgressTracker != null) {
			mProgressTracker.onProgress(mProgressMessage);
			if (mResult != null) {
				mProgressTracker.onComplete();
			}
		}
	}

	/* UI Thread */
	@Override
	protected void onCancelled() {
		// Detach from progress tracker
		mProgressTracker = null;
	}

	/* UI Thread */
	@Override
	protected void onProgressUpdate(String... values) {
		// Update progress message
		mProgressMessage = values[0];
		// And send it to progress tracker
		if (mProgressTracker != null) {
			mProgressTracker.onProgress(mProgressMessage);
		}
	}

	/* UI Thread */
	@Override
	protected void onPostExecute(String result) {
		// Update result
		mResult = result;
		// And send it to progress tracker
		if (mProgressTracker != null) {
			mProgressTracker.onComplete();
		}
		// Detach from progress tracker
		mProgressTracker = null;
	}

	/* Separate Thread */
	@Override
	protected String doInBackground(Void... arg0) {
		// Working in separate thread

		if (isCancelled()) {
			// This return causes onPostExecute call on UI thread
			return "Cancelado";
		}

		// This call causes onProgressUpdate call on UI thread
		publishProgress(mResources.getString(R.string.task_working, 0));
	
		byte[] data = getBytesFromFile(is);
	    
		// This return causes onPostExecute call on UI thread
		return doFileUpload(existingFileName,data);
	}

	private String doFileUpload(String existingFileName, byte[] buffer) {

		HttpURLConnection conn = null;
		DataOutputStream dos = null;
		DataInputStream inStream = null;
		String lineEnd = "\r\n";
		String twoHyphens = "--";
		String boundary = "*****";
		String urlString = "http://whatshappening.no-ip.org/image/attached";

		try {
			// ------------------ CLIENT REQUEST
			// open a URL connection to the Servlet
			URL url = new URL(urlString);
			// Open a HTTP connection to the URL
			conn = (HttpURLConnection) url.openConnection();
			// Allow Inputs
			conn.setDoInput(true);
			// Allow Outputs
			conn.setDoOutput(true);
			// Don't use a cached copy.
			conn.setUseCaches(false);
			// Use a post method.
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);

			dos = new DataOutputStream(conn.getOutputStream());
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\""
					+ existingFileName + "\"" + lineEnd);
			dos.writeBytes(lineEnd);

			dos.write(buffer, 0, buffer.length);
{'fraction_non_alphanumeric': 0.11115414407436096, 'fraction_numerical': 0.0028401755744900596, 'mean_word_length': 3.909378960709759, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '4559092', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2222, 'n_words': 610}