package com.temporaryteam.noticeditor.io;

import java.io.*;
import java.net.URI;
import java.net.URLEncoder;
import java.util.Deque;
import java.util.LinkedList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
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));
	}
	
	/** 
	 * Pack directory
	 */
	public static void pack(File directory, String toSave) throws IOException {
		final URI root = directory.toURI();
		Deque<File> queue = new LinkedList<File>();
		queue.push(directory);
		
		try (OutputStream out = new FileOutputStream(new File(toSave));
				ZipOutputStream zout = new ZipOutputStream(out)) {
			
			while(!queue.isEmpty()) {
				directory = queue.pop();
				for(File child : directory.listFiles()) {
					String name = root.relativize(child.toURI()).getPath();
					if (child.isDirectory()) {
						queue.push(child);
						name = name.endsWith("/") ? name : (name + "/");
						zout.putNextEntry(new ZipEntry(name));
					} else {
						zout.putNextEntry(new ZipEntry(name));
						try (InputStream in = new FileInputStream(child)) {
							byte[] buffer = new byte[1024];
							while (true) {
								int readCount = in.read(buffer);
								if (readCount < 0) break;
								zout.write(buffer, 0, readCount);
							}
						}
						zout.closeEntry();
					}
				}
			}
		}
	}
	
}

@@ -76,159 +76,16 @@
 t.UR
-I;%0Aimport java.net.URLEncoder;%0Aimport java.util.Deque;%0Aimport java.util.LinkedList;%0Aimport java.util.zip.ZipEntry;%0Aimport java.util.zip.ZipOutputStream
+LEncoder
 ;%0Aim
@@ -2296,1036 +2296,6 @@
 %7D%0A%09%0A
-%09/** %0A%09 * Pack directory%0A%09 */%0A%09public static void pack(File directory, String toSave) throws IOException %7B%0A%09%09final URI root = directory.toURI();%0A%09%09Deque%3CFile%3E queue = new LinkedList%3CFile%3E();%0A%09%09queue.push(directory);%0A%09%09%0A%09%09try (OutputStream out = new FileOutputStream(new File(toSave));%0A%09%09%09%09ZipOutputStream zout = new ZipOutputStream(out)) %7B%0A%09%09%09%0A%09%09%09while(!queue.isEmpty()) %7B%0A%09%09%09%09directory = queue.pop();%0A%09%09%09%09for(File child : directory.listFiles()) %7B%0A%09%09%09%09%09String name = root.relativize(child.toURI()).getPath();%0A%09%09%09%09%09if (child.isDirectory()) %7B%0A%09%09%09%09%09%09queue.push(child);%0A%09%09%09%09%09%09name = name.endsWith(%22/%22) ? name : (name + %22/%22);%0A%09%09%09%09%09%09zout.putNextEntry(new ZipEntry(name));%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09zout.putNextEntry(new ZipEntry(name));%0A%09%09%09%09%09%09try (InputStream in = new FileInputStream(child)) %7B%0A%09%09%09%09%09%09%09byte%5B%5D buffer = new byte%5B1024%5D;%0A%09%09%09%09%09%09%09while (true) %7B%0A%09%09%09%09%09%09%09%09int readCount = in.read(buffer);%0A%09%09%09%09%09%09%09%09if (readCount %3C 0) break;%0A%09%09%09%09%09%09%09%09zout.write(buffer, 0, readCount);%0A%09%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09zout.closeEntry();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%0A
 %7D%0A

Remove unused method in IOUtil