package chartgeneration.parser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * The parser converts Jmeter test logs to data tables (in CSV format). The raw
 * data is can be XML or CSV format.
 * 
 * @author Rayson Zhu
 *
 */
public class JmeterParser implements DataParser {

	@Override
	public void parse(InputStream in, OutputStream out) throws Exception {
		in.mark(512);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String firstLine = reader.readLine();
		if (firstLine == null || firstLine.isEmpty())
			return;
		in.reset();
		if (firstLine.startsWith("<?xml")) {
			new JmeterXMLParser().parse(in, out);
		} else {
			new JmeterCSVParser().parse(in, out);
		}
	}

	static void writeFields(OutputStreamWriter writer, Object... objs)
			throws IOException {
		if (objs.length < 1)
			return;
		writeField(writer, objs[0]);
		for (int i = 1; i < objs.length; ++i) {
			writer.write(',');
			writeField(writer, objs[i]);
		}
		writer.write('\n');
	}

	static void writeField(OutputStreamWriter writer, Object obj)
			throws IOException {
		writer.write('\"');
		writer.write(obj.toString().replace("\"", "\"\""));
		writer.write('\"');
	}

}

@@ -506,192 +506,185 @@
 %7B%0A%09%09
-in.mark(512);%0A%09%09BufferedReader reader = new BufferedReader(new InputStreamReader(in));%0A%09%09String firstLine = reader.readLine();%0A%09%09if (firstLine == null %7C%7C firstLine.isEmpty())%0A%09%09%09return
+final int BUFFER_SIZE = 512;%0A%09%09in.mark(BUFFER_SIZE);%0A%09%09byte%5B%5D buffer = new byte%5BBUFFER_SIZE%5D;%0A%09%09in.read(buffer, 0, BUFFER_SIZE);%0A%09%09String firstLine = new String(buffer, %22ascii%22)
 ;%0A%09%09

change the logic for Jmeter jtl file format detection