package eu.scasefp7.eclipse.core.connect.uploader;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.Scanner;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

import eu.scasefp7.eclipse.core.connect.Activator;

public class StringFileHelpers {

	public static String readFileToString(File file) {
		String text = "";
		try {
			Scanner scanner = new Scanner(file);
			text = scanner.useDelimiter("\\A").next();
			scanner.close();
		} catch (FileNotFoundException e) {
			Activator.log("Error reading file " + file.getName(), e);
		}
		return text;
	}

	public static String readFileToString(IFile file) {
		String text = "";
		try {
			Scanner scanner = new Scanner(file.getContents());
			text = scanner.useDelimiter("\\A").next();
			scanner.close();
		} catch (CoreException e) {
			Activator.log("Error reading file " + file.getName(), e);
		}
		return text;
	}

	public static String convertStringToBytes(String text) {
		String byteArray = "";
		byte[] bytes = text.getBytes(Charset.forName("UTF-8"));
		for (byte b : bytes) {
			byteArray += b;
			byteArray += ",";
		}
		byteArray = byteArray.length() > 0 ? byteArray.substring(0, byteArray.length() - 1) : "";
		return byteArray;
	}

}

@@ -1067,16 +1067,23 @@
 %09%09String
+Builder
  byteArr
@@ -1084,21 +1084,45 @@
 yteArray
- = %22%22
+Builder = new StringBuilder()
 ;%0D%0A%09%09byt
@@ -1175,16 +1175,38 @@
 -8%22));%0D%0A
+%09%09String delim = %22%22;%0D%0A
 %09%09for (b
@@ -1239,30 +1239,41 @@
 rray
- += b;%0D%0A%09%09%09byteArray +
+Builder.append(delim);%0D%0A%09%09%09delim 
 = %22,
@@ -1277,20 +1277,16 @@
  %22,%22;%0D%0A%09
-%09%7D%0D%0A
 %09%09byteAr
@@ -1292,88 +1292,31 @@
 rray
- = byteArray.length() %3E 0 ? byteArray.substring(0, byteArray.length() - 1) : %22%22;
+Builder.append(b);%0D%0A%09%09%7D
 %0D%0A%09%09
@@ -1331,16 +1331,34 @@
 yteArray
+Builder.toString()
 ;%0D%0A%09%7D%0D%0A%0D

Optimize conversion of string to bytes