package com.temporaryteam.noticeditor.io;

import java.io.*;
import java.net.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;

public final class IOUtil {
	
	private static final int FILENAME_LIMIT = 60;
	private static final String NEW_LINE = System.lineSeparator();

	public static String readContent(File file) throws IOException {
		final StringBuilder result = new StringBuilder();
		try (InputStream is = new FileInputStream(file);
				Reader isr = new InputStreamReader(is, "UTF-8");
				BufferedReader reader = new BufferedReader(isr)) {
			result.append(reader.readLine()).append(NEW_LINE);
		}
		return result.toString();
	}
	
	public static void writeContent(File file, String content) throws IOException {
		try (OutputStream os = new FileOutputStream(file);
				Writer writer = new OutputStreamWriter(os, "UTF-8")) {
			writer.write(content);
		}
	}
	
	public static void writeJson(File file, JSONObject json) throws IOException, JSONException {
		try (OutputStream os = new FileOutputStream(file);
				Writer writer = new OutputStreamWriter(os, "UTF-8")) {
			json.write(writer);
		}
	}
	
	public static void removeDirectory(File directory) {
		if (directory.isFile() || !directory.exists()) return;
		removeDirectoryHelper(directory);
	}
	
	private static void removeDirectoryHelper(File file) {
		if (file.isDirectory()) {
			for (File f : file.listFiles()) {
				removeDirectoryHelper(f);
			}
		}
		file.delete();
	}
	
	public static String sanitizeFilename(String name) {
		if (name == null || name.isEmpty()) return "empty";
		
		// Convert non-ascii chars to char code
		String newName = name;
		try {
			newName = URLEncoder.encode(newName, "UTF-8");
		} catch (UnsupportedEncodingException ex) { }
		// Allow only english chars, numbers and some specific symbols
		newName = newName.toLowerCase().replaceAll("[^a-z0-9._\\(\\)]", "_");
		// Limit filename length
		if (newName.length() > FILENAME_LIMIT) {
			newName = newName.substring(0, FILENAME_LIMIT);
		}
		return newName;
	}
	
	public static InputStream toStream(String content) throws IOException {
		return toStream(content, "UTF-8");
	}
	
	public static InputStream toStream(String content, String charset) throws IOException {
		return new ByteArrayInputStream(content.getBytes(charset));
	}
	
}

@@ -356,300 +356,64 @@
 %7B%0A%09%09
-final StringBuilder result = new StringBuilder();%0A%09%09try (InputStream is = new FileInputStream(file);%0A%09%09%09%09Reader isr = new InputStreamReader(is, %22UTF-8%22);%0A%09%09%09%09BufferedReader reader = new BufferedReader(isr)) %7B%0A%09%09%09result.append(reader.readLine()).append(NEW_LINE);%0A%09%09%7D%0A%09%09return result.toString(
+return IOUtil.stringFromStream(new FileInputStream(file)
 );%0A%09
@@ -2056,10 +2056,550 @@
 );%0A%09%7D%0A%09%0A
+%09public static String stringFromStream(InputStream stream) throws IOException %7B%0A%09%09return stringFromStream(stream, %22UTF-8%22);%0A%09%7D%0A%09%0A%09public static String stringFromStream(InputStream stream, String charset) throws IOException %7B%0A%09%09final StringBuilder result = new StringBuilder();%0A%09%09try (Reader isr = new InputStreamReader(stream, charset);%0A%09%09%09%09BufferedReader reader = new BufferedReader(isr)) %7B%0A%09%09%09String line;%0A%09%09%09while ( (line = reader.readLine()) != null ) %7B%0A%09%09%09%09result.append(line).append(NEW_LINE);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return result.toString();%0A%09%7D%0A
 %7D%0A

Add method for read string content from stream