package bakersoftware.maven_replacer_plugin;

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

public class TokenReplacer {
	private final String lineSeparator;
	private final StreamFactory streamFactory;

	public TokenReplacer(StreamFactory streamFactory, String lineSeparator) {
		this.lineSeparator = lineSeparator;
		this.streamFactory = streamFactory;
	}

	public void replaceTokens(String token, String value, boolean isTokenRegex) throws IOException {
		StringBuffer buffer = new StringBuffer();

		InputStream inputStream = streamFactory.getNewInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

		String line = reader.readLine();
		if (line == null) {
			throw new IOException("Could not read from stream");
		}
		while (line != null) {
			buffer.append(line);
			if (lineSeparator != null) {
				buffer.append(lineSeparator);
			}
			line = reader.readLine();
		}
		reader.close();

		final String result;
		String valueToReplaceWith = value == null ? "" : value;
		if (isTokenRegex) {
			result = buffer.toString().replaceAll(token, valueToReplaceWith);
		} else {
			result = replaceNonRegex(buffer.toString(), token, valueToReplaceWith);
		}

		Writer writer = new OutputStreamWriter(streamFactory.getNewOutputStream());
		writer.write(result);
		writer.close();
	}

	private String replaceNonRegex(String input, String token, String value) {
		if (input.length() == 0) {
			return input;
		}

		StringBuffer buffer = new StringBuffer();
		int start = 0;
		int end = 0;
		while ((end = input.indexOf(token, start)) >= 0) {
			buffer.append(input.substring(start, end));
			buffer.append(value);
			start = end + token.length();
		}
		return buffer.append(input.substring(start)).toString();
	}

}

@@ -606,55 +606,8 @@
  %7B%0D%0A
-%09%09StringBuffer buffer = new StringBuffer();%0D%0A%0D%0A
 %09%09In
@@ -747,24 +747,410 @@
 tream));%0D%0A%0D%0A
+%09%09StringBuffer buffer = readContents(reader);%0D%0A%09%09String result = replaceContents(buffer.toString(), token, value, isTokenRegex);%0D%0A%0D%0A%09%09Writer writer = new OutputStreamWriter(streamFactory.getNewOutputStream());%0D%0A%09%09writer.write(result);%0D%0A%09%09writer.close();%0D%0A%09%7D%0D%0A%0D%0A%09private StringBuffer readContents(BufferedReader reader) throws IOException %7B%0D%0A%09%09StringBuffer buffer = new StringBuffer();%0D%0A
 %09%09String lin
@@ -1433,24 +1433,148 @@
 r.close();%0D%0A
+%09%09return buffer;%0D%0A%09%7D%0D%0A%0D%0A%09private String replaceContents(String contents, String token, String value, boolean isTokenRegex) %7B
 %0D%0A%09%09final St
@@ -1681,33 +1681,24 @@
 esult = 
-buffer.toString()
+contents
 .replace
@@ -1770,33 +1770,24 @@
 onRegex(
-buffer.toString()
+contents
 , token,
@@ -1818,130 +1818,23 @@
 %09%7D%0D%0A
-%0D%0A%09%09Writer writer = new OutputStreamWriter(streamFactory.getNewOutputStream());%0D%0A%09%09writer.write(result);%0D%0A%09%09writer.close()
+%09%09return result
 ;%0D%0A%09

refactoring out different steps