Initial source import.

This commit is contained in:
2014-05-29 20:45:10 +02:00
commit 400ee6a079
411 changed files with 18552 additions and 0 deletions
@@ -0,0 +1,46 @@
package mightypork.utils.files;
import java.io.File;
import java.io.FileFilter;
/**
* File filter for certain suffixes
*
* @author Ondřej Hruška (MightyPork)
*/
public class FileSuffixFilter implements FileFilter {
/** Array of allowed suffixes */
private String[] suffixes = null;
/**
* Suffix filter
*
* @param suffixes var-args allowed suffixes, case insensitive
*/
public FileSuffixFilter(String... suffixes)
{
this.suffixes = suffixes;
}
@Override
public boolean accept(File pathname)
{
if (!pathname.isFile()) return false;
final String fname = pathname.getName().toLowerCase().trim();
for (final String suffix : suffixes) {
if (fname.endsWith(suffix.toLowerCase().trim())) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,153 @@
package mightypork.utils.files;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.Checksum;
import mightypork.utils.logging.Log;
public class FileTreeDiff {
private static final byte[] BUFFER = new byte[2048];
private final Checksum ck1 = new Adler32();
private final Checksum ck2 = new Adler32();
private boolean logging = true;
private final List<Tuple<File>> compared = new ArrayList<>();
private final Comparator<File> fileFirstSorter = new Comparator<File>() {
@Override
public int compare(File o1, File o2)
{
if (!o1.isDirectory() && o2.isDirectory()) return -1;
if (o1.isDirectory() && !o2.isDirectory()) return 1;
return o1.getName().compareTo(o2.getName());
}
};
public void enableLogging(boolean state)
{
logging = state;
}
public boolean areEqual(File dir1, File dir2)
{
if (logging) Log.f3("Comparing directory trees:\n 1. " + dir1 + "\n 2. " + dir2);
try {
compared.clear();
buildList(dir1, dir2);
calcChecksum();
if (logging) Log.f3("No difference found.");
return true;
} catch (final NotEqualException e) {
if (logging) Log.f3("Difference found:\n" + e.getMessage());
return false;
}
}
private void calcChecksum() throws NotEqualException
{
for (final Tuple<File> pair : compared) {
ck1.reset();
ck2.reset();
try(FileInputStream in1 = new FileInputStream(pair.a);
FileInputStream in2 = new FileInputStream(pair.b)) {
try(CheckedInputStream cin1 = new CheckedInputStream(in1, ck1);
CheckedInputStream cin2 = new CheckedInputStream(in2, ck2)) {
while (true) {
final int read1 = cin1.read(BUFFER);
final int read2 = cin2.read(BUFFER);
if (read1 != read2 || ck1.getValue() != ck2.getValue()) {
throw new NotEqualException("Bytes differ:\n" + pair.a + "\n" + pair.b);
}
if (read1 == -1) break;
}
}
} catch (final IOException e) {
// ignore
}
}
}
private void buildList(File f1, File f2) throws NotEqualException
{
if (f1.isDirectory() != f2.isDirectory()) throw new NotEqualException("isDirectory differs:\n" + f1 + "\n" + f2);
if (f1.isFile() && f2.isFile()) {
if (f1.length() != f2.length()) throw new NotEqualException("Sizes differ:\n" + f1 + "\n" + f2);
}
if (f1.isDirectory()) {
final File[] children1 = f1.listFiles();
final File[] children2 = f2.listFiles();
Arrays.sort(children1, fileFirstSorter);
Arrays.sort(children2, fileFirstSorter);
if (children1.length != children2.length) throw new NotEqualException("Child counts differ:\n" + f1 + "\n" + f2);
for (int i = 0; i < children1.length; i++) {
final File ch1 = children1[i];
final File ch2 = children2[i];
if (!ch1.getName().equals(ch2.getName())) throw new NotEqualException("Filenames differ:\n" + ch1 + "\n" + ch2);
buildList(ch1, ch2);
}
} else {
compared.add(new Tuple<>(f1, f2));
}
}
private class NotEqualException extends Exception {
public NotEqualException(String msg)
{
super(msg);
}
}
private class Tuple<T> {
public T a;
public T b;
public Tuple(T a, T b)
{
this.a = a;
this.b = b;
}
}
}
+438
View File
@@ -0,0 +1,438 @@
package mightypork.utils.files;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import mightypork.utils.logging.Log;
import mightypork.utils.string.StringUtil;
import mightypork.utils.string.validation.StringFilter;
public class FileUtils {
/**
* Copy directory recursively.
*
* @param source source file
* @param target target file
* @throws IOException on error
*/
public static void copyDirectory(File source, File target) throws IOException
{
copyDirectory(source, target, null, null);
}
/**
* Copy directory recursively - advanced variant.
*
* @param source source file
* @param target target file
* @param filter filter accepting only files and dirs to be copied
* @param filesCopied list into which all the target files will be added
* @throws IOException on error
*/
public static void copyDirectory(File source, File target, FileFilter filter, List<File> filesCopied) throws IOException
{
if (!source.exists()) return;
if (source.isDirectory()) {
if (!target.exists() && !target.mkdir()) {
throw new IOException("Could not open destination directory.");
}
final String[] children = source.list();
for (final String element : children) {
copyDirectory(new File(source, element), new File(target, element), filter, filesCopied);
}
} else {
if (filter != null && !filter.accept(source)) {
return;
}
if (filesCopied != null) filesCopied.add(target);
copyFile(source, target);
}
}
/**
* List directory recursively
*
* @param source source file
* @param filter filter accepting only files and dirs to be copied (or null)
* @param files list of the found files
* @throws IOException on error
*/
public static void listDirectoryRecursive(File source, StringFilter filter, List<File> files) throws IOException
{
if (source.isDirectory()) {
final String[] children = source.list();
for (final String element : children) {
listDirectoryRecursive(new File(source, element), filter, files);
}
} else {
if (filter != null && !filter.isValid(source.getAbsolutePath())) {
return;
}
files.add(source);
}
}
/**
* Copy file using streams. Make sure target directory exists!
*
* @param source source file
* @param target target file
* @throws IOException on error
*/
public static void copyFile(File source, File target) throws IOException
{
try(InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target)) {
copyStream(in, out);
}
}
/**
* Copy bytes from input to output stream, leaving out stream open
*
* @param in input stream
* @param out output stream
* @throws IOException on error
*/
public static void copyStream(InputStream in, OutputStream out) throws IOException
{
if (in == null) {
throw new NullPointerException("Input stream is null");
}
if (out == null) {
throw new NullPointerException("Output stream is null");
}
final byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
/**
* Improved delete
*
* @param path deleted path
* @param recursive recursive delete
* @return success
*/
public static boolean delete(File path, boolean recursive)
{
if (!path.exists()) {
return true;
}
if (!recursive || !path.isDirectory()) return path.delete();
final String[] list = path.list();
for (int i = 0; i < list.length; i++) {
if (!delete(new File(path, list[i]), true)) return false;
}
return path.delete();
}
/**
* Read entire file to a string.
*
* @param file file
* @return file contents
* @throws IOException
*/
public static String fileToString(File file) throws IOException
{
try(FileInputStream fin = new FileInputStream(file)) {
return streamToString(fin);
}
}
/**
* Get files in a folder (create folder if needed)
*
* @param dir folder
* @return list of files
*/
public static List<File> listDirectory(File dir)
{
return FileUtils.listDirectory(dir, null);
}
/**
* Get files in a folder (create folder if needed)
*
* @param dir folder
* @param filter file filter
* @return list of files
*/
public static List<File> listDirectory(File dir, FileFilter filter)
{
dir.mkdir();
final List<File> list = new ArrayList<>();
for (final File f : dir.listFiles(filter)) {
list.add(f);
}
return list;
}
/**
* Remove extension.
*
* @param file file
* @return filename without extension
*/
public static String[] getFilenameParts(File file)
{
return getFilenameParts(file.getName());
}
public static String getExtension(File file)
{
return getExtension(file.getName());
}
public static String getExtension(String file)
{
return StringUtil.fromLastChar(file, '.');
}
/**
* Remove extension.
*
* @param filename
* @return filename and extension
*/
public static String[] getFilenameParts(String filename)
{
String ext, name;
try {
ext = StringUtil.fromLastDot(filename);
} catch (final StringIndexOutOfBoundsException e) {
ext = "";
}
try {
name = StringUtil.toLastDot(filename);
} catch (final StringIndexOutOfBoundsException e) {
name = "";
Log.w("Error extracting extension from file " + filename);
}
return new String[] { name, ext };
}
/**
* Read entire input stream to a string, and close it.
*
* @param in input stream
* @return file contents
*/
public static String streamToString(InputStream in)
{
return streamToString(in, -1);
}
/**
* Read input stream to a string, and close it.
*
* @param in input stream
* @param lines max number of lines (-1 to disable limit)
* @return file contents
*/
public static String streamToString(InputStream in, int lines)
{
if (in == null) {
Log.e(new NullPointerException("Null stream to be converted to String."));
return ""; // to avoid NPE's
}
BufferedReader br = null;
final StringBuilder sb = new StringBuilder();
String line;
try {
int cnt = 0;
br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
while ((line = br.readLine()) != null && (cnt < lines || lines <= 0)) {
sb.append(line + "\n");
cnt++;
}
if (cnt == lines && lines > 0) {
sb.append("--- end of preview ---\n");
}
} catch (final IOException e) {
Log.e(e);
} finally {
try {
if (br != null) br.close();
} catch (final IOException e) {
// ignore
}
}
return sb.toString();
}
public static InputStream stringToStream(String text)
{
if (text == null) return null;
try {
return new ByteArrayInputStream(text.getBytes("UTF-8"));
} catch (final UnsupportedEncodingException e) {
Log.e(e);
return null;
}
}
public static InputStream getResource(String path)
{
final InputStream in = FileUtils.class.getResourceAsStream(path);
if (in != null) return in;
try {
return new FileInputStream(new File(".", path));
} catch (final FileNotFoundException e) {
// error
Log.w("Could not open resource stream: " + path);
return null;
}
}
public static String getResourceAsString(String path)
{
return streamToString(FileUtils.class.getResourceAsStream(path));
}
/**
* Save string to file
*
* @param file file
* @param text string
* @throws IOException on error
*/
public static void stringToFile(File file, String text) throws IOException
{
try(PrintStream out = new PrintStream(new FileOutputStream(file), false, "UTF-8")) {
out.print(text);
out.flush();
}
}
public static void deleteEmptyDirs(File base) throws IOException
{
for (final File f : listDirectory(base)) {
if (!f.isDirectory()) continue;
deleteEmptyDirs(f);
final List<File> children = listDirectory(f);
if (children.size() == 0) {
if (!f.delete()) throw new IOException("Could not delete a directory: " + f);
continue;
}
}
}
public static String getBasename(String name)
{
return StringUtil.toLastChar(StringUtil.fromLastChar(name, '/'), '.');
}
public static String getFilename(String name)
{
return StringUtil.fromLastChar(name, '/');
}
/**
* Copy resource to file
*
* @param resname resource name
* @param file out file
* @throws IOException
*/
public static void resourceToFile(String resname, File file) throws IOException
{
try(InputStream in = FileUtils.getResource(resname);
OutputStream out = new FileOutputStream(file)) {
FileUtils.copyStream(in, out);
}
}
/**
* Get resource as string, safely closing streams.
*
* @param resname resource name
* @return resource as string, empty string on failure
* @throws IOException on fail
*/
public static String resourceToString(String resname) throws IOException
{
try(InputStream in = FileUtils.getResource(resname)) {
return streamToString(in);
}
}
}
@@ -0,0 +1,54 @@
package mightypork.utils.files;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
/**
* Instance lock (avoid running twice)
*
* @author Ondřej Hruška (MightyPork)
*/
public class InstanceLock {
@SuppressWarnings("resource")
public static boolean onFile(final File lockFile)
{
try {
lockFile.getParentFile().mkdirs();
final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw");
final FileLock fileLock = randomAccessFile.getChannel().tryLock();
if (fileLock != null) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run()
{
try {
fileLock.release();
randomAccessFile.close();
if (!lockFile.delete()) throw new IOException();
} catch (final Throwable t) {
System.err.println("Unable to remove lock file.");
t.printStackTrace();
}
}
});
return true;
}
return false;
} catch (final IOException e) {
System.err.println("IO error while obtaining lock.");
e.printStackTrace();
return false;
}
}
}
+94
View File
@@ -0,0 +1,94 @@
package mightypork.utils.files;
import java.io.File;
public class OsUtils {
public static enum EnumOS
{
linux, macos, solaris, unknown, windows;
public boolean isLinux()
{
return this == linux || this == solaris;
}
public boolean isMac()
{
return this == macos;
}
public boolean isWindows()
{
return this == windows;
}
}
private static EnumOS cachedOs;
public static File getHomeWorkDir(String dirname)
{
final String userhome = System.getProperty("user.home", ".");
File file;
switch (getOs()) {
case linux:
case solaris:
file = new File(userhome, dirname + '/');
break;
case windows:
final String appdata = System.getenv("APPDATA");
if (appdata != null) {
file = new File(appdata, dirname + '/');
} else {
file = new File(userhome, dirname + '/');
}
break;
case macos:
file = new File(userhome, "Library/Application Support/" + dirname);
break;
default:
file = new File(userhome, dirname + "/");
break;
}
return file;
}
public static EnumOS getOs()
{
if (cachedOs != null) return cachedOs;
final String s = System.getProperty("os.name").toLowerCase();
if (s.contains("win")) {
cachedOs = EnumOS.windows;
} else if (s.contains("mac")) {
cachedOs = EnumOS.macos;
} else if (s.contains("linux") || s.contains("unix")) {
cachedOs = EnumOS.linux;
} else if (s.contains("solaris") || s.contains("sunos")) {
cachedOs = EnumOS.solaris;
} else {
cachedOs = EnumOS.unknown;
}
return cachedOs;
}
}
@@ -0,0 +1,71 @@
package mightypork.utils.files.config;
import mightypork.utils.Convert;
public abstract class Property<T extends Object> {
private final String comment;
private final String key;
private T value;
private final T defaultValue;
public Property(String key, T defaultValue, String comment)
{
super();
this.comment = comment;
this.key = key;
this.value = defaultValue;
this.defaultValue = defaultValue;
}
public final void parse(String string)
{
setValue(decode(string, defaultValue));
}
public abstract T decode(String string, T defval);
public String encode(T value)
{
return Convert.toString(value, Convert.toString(defaultValue));
}
@Override
public final String toString()
{
return encode(value);
}
public T getValue()
{
return value;
}
@SuppressWarnings("unchecked")
public void setValue(Object value)
{
this.value = (T) value;
}
public String getComment()
{
return comment;
}
public String getKey()
{
return key;
}
}
@@ -0,0 +1,377 @@
package mightypork.utils.files.config;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.TreeMap;
import mightypork.utils.Convert;
import mightypork.utils.logging.Log;
/**
* Property manager with advanced formatting and value checking.
*
* @author Ondřej Hruška (MightyPork)
*/
public class PropertyManager {
private class BooleanProperty extends Property<Boolean> {
public BooleanProperty(String key, Boolean defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public Boolean decode(String string, Boolean defval)
{
return Convert.toBoolean(string, defval);
}
}
private class IntegerProperty extends Property<Integer> {
public IntegerProperty(String key, Integer defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public Integer decode(String string, Integer defval)
{
return Convert.toInteger(string, defval);
}
}
private class DoubleProperty extends Property<Double> {
public DoubleProperty(String key, Double defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public Double decode(String string, Double defval)
{
return Convert.toDouble(string, defval);
}
}
private class StringProperty extends Property<String> {
public StringProperty(String key, String defaultValue, String comment)
{
super(key, defaultValue, comment);
}
@Override
public String decode(String string, String defval)
{
return Convert.toString(string, defval);
}
}
/** put newline before entry comments */
private boolean cfgNewlineBeforeComments = true;
/** Put newline between sections. */
private boolean cfgSeparateSections = true;
private final File file;
private String fileComment = "";
private final TreeMap<String, Property<?>> entries;
private final TreeMap<String, String> renameTable;
private SortedProperties props = new SortedProperties();
/**
* Create property manager from file path and an initial comment.
*
* @param file file with the props
* @param comment the initial comment. Use \n in it if you want.
*/
public PropertyManager(File file, String comment)
{
this.file = file;
this.entries = new TreeMap<>();
this.renameTable = new TreeMap<>();
this.fileComment = comment;
}
/**
* Load, fix and write to file.
*/
public void load()
{
if (!file.getParentFile().mkdirs()) {
if (!file.getParentFile().exists()) {
throw new RuntimeException("Cound not create config file.");
}
}
try(FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
} catch (final IOException e) {
props = new SortedProperties();
}
props.cfgBlankRowBetweenSections = cfgSeparateSections;
props.cfgBlankRowBeforeComment = cfgNewlineBeforeComments;
// rename keys
for (final Entry<String, String> entry : renameTable.entrySet()) {
final String pr = props.getProperty(entry.getKey());
if (pr == null) continue;
props.remove(entry.getKey());
props.setProperty(entry.getValue(), pr);
}
for (final Property<?> entry : entries.values()) {
entry.parse(props.getProperty(entry.getKey()));
}
renameTable.clear();
}
public void save()
{
try {
final ArrayList<String> keyList = new ArrayList<>();
// validate entries one by one, replace with default when needed
for (final Property<?> entry : entries.values()) {
keyList.add(entry.getKey());
if (entry.getComment() != null) {
props.setKeyComment(entry.getKey(), entry.getComment());
}
props.setProperty(entry.getKey(), entry.toString());
}
// removed unused props
for (final String propname : props.keySet().toArray(new String[props.size()])) {
if (!keyList.contains(propname)) {
props.remove(propname);
}
}
try(FileOutputStream fos = new FileOutputStream(file)) {
props.store(fos, fileComment);
}
} catch (final IOException ioe) {
ioe.printStackTrace();
}
}
/**
* @param newlineBeforeComments put newline before comments
*/
public void cfgNewlineBeforeComments(boolean newlineBeforeComments)
{
this.cfgNewlineBeforeComments = newlineBeforeComments;
}
/**
* @param separateSections do separate sections by newline
*/
public void cfgSeparateSections(boolean separateSections)
{
this.cfgSeparateSections = separateSections;
}
/**
* Get a property entry (rarely used)
*
* @param k key
* @return the entry
*/
public Property<?> getProperty(String k)
{
try {
return entries.get(k);
} catch (final Exception e) {
Log.w(e);
return null;
}
}
/**
* Get boolean property
*
* @param k key
* @return the boolean found, or false
*/
public Boolean getBoolean(String k)
{
return Convert.toBoolean(getProperty(k).getValue());
}
/**
* Get numeric property
*
* @param k key
* @return the int found, or null
*/
public Integer getInteger(String k)
{
return Convert.toInteger(getProperty(k).getValue());
}
/**
* Get numeric property as double
*
* @param k key
* @return the double found, or null
*/
public Double getDouble(String k)
{
return Convert.toDouble(getProperty(k).getValue());
}
/**
* Get string property
*
* @param k key
* @return the string found, or null
*/
public String getString(String k)
{
return Convert.toString(getProperty(k).getValue());
}
/**
* Get arbitrary property. Make sure it's of the right type!
*
* @param k key
* @return the prioperty found
*/
@SuppressWarnings("unchecked")
public <T> T getValue(String k)
{
try {
return ((Property<T>) getProperty(k)).getValue();
} catch (final ClassCastException e) {
return null;
}
}
/**
* Add a boolean property
*
* @param k key
* @param d default value
* @param comment the in-file comment
*/
public void putBoolean(String k, boolean d, String comment)
{
putProperty(new BooleanProperty(k, d, comment));
}
/**
* Add a numeric property (double)
*
* @param k key
* @param d default value
* @param comment the in-file comment
*/
public void putDouble(String k, double d, String comment)
{
putProperty(new DoubleProperty(k, d, comment));
}
/**
* Add a numeric property
*
* @param k key
* @param d default value
* @param comment the in-file comment
*/
public void putInteger(String k, int d, String comment)
{
putProperty(new IntegerProperty(k, d, comment));
}
/**
* Add a string property
*
* @param k key
* @param d default value
* @param comment the in-file comment
*/
public void putString(String k, String d, String comment)
{
putProperty(new StringProperty(k, d, comment));
}
/**
* Add a range property
*
* @param prop property to put
*/
public <T> void putProperty(Property<T> prop)
{
entries.put(prop.getKey(), prop);
}
/**
* Rename key before loading; value is preserved
*
* @param oldKey old key
* @param newKey new key
*/
public void renameKey(String oldKey, String newKey)
{
renameTable.put(oldKey, newKey);
return;
}
/**
* Set value saved to certain key.
*
* @param key key
* @param value the saved value
*/
public void setValue(String key, Object value)
{
getProperty(key).setValue(value);
}
public void setFileComment(String fileComment)
{
this.fileComment = fileComment;
}
}
@@ -0,0 +1,205 @@
package mightypork.utils.files.config;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
/**
* Utility for parsing simple config files<br>
* # and // mark a comment<br>
* empty lines and lines without "=" are ignored<br>
* lines with "=" must have "key = value" format, or a warning is logged.<br>
* use "NULL" to create empty value.
*
* @author Ondřej Hruška (MightyPork)
*/
public class SimpleConfig {
/**
* Load list from file
*
* @param file file
* @return map of keys and values
* @throws IOException
*/
public static List<String> listFromFile(File file) throws IOException
{
final String fileText = FileUtils.fileToString(file);
return listFromString(fileText);
}
/**
* Load map from file
*
* @param file file
* @return map of keys and values
* @throws IOException
*/
public static Map<String, String> mapFromFile(File file) throws IOException
{
final String fileText = FileUtils.fileToString(file);
return mapFromString(fileText);
}
/**
* Load list from string
*
* @param text text of the file
* @return map of keys and values
*/
public static List<String> listFromString(String text)
{
final List<String> list = new ArrayList<>();
final String[] groupsLines = text.split("\n");
for (String s : groupsLines) {
// ignore invalid lines
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith("//")) continue;
// NULL value
if (s.equalsIgnoreCase("NULL")) s = null;
if (s != null) s = s.replace("\\n", "\n");
// save extracted key-value pair
list.add(s);
}
return list;
}
/**
* Load map from string
*
* @param text text of the file
* @return map of keys and values
*/
public static Map<String, String> mapFromString(String text)
{
final LinkedHashMap<String, String> pairs = new LinkedHashMap<>();
final String[] groupsLines = text.split("\n");
for (final String s : groupsLines) {
// ignore invalid lines
if (s.length() == 0) continue;
if (s.startsWith("#") || s.startsWith("//")) continue;
if (!s.contains("=")) continue;
// split and trim
String[] parts = s.split("=");
for (int i = 0; i < parts.length; i++) {
parts[i] = parts[i].trim();
}
// check if both parts are valid
if (parts.length == 0) {
Log.w("Bad line in config file: " + s);
continue;
}
if (parts.length == 1) {
parts = new String[] { parts[0], "" };
}
if (parts.length != 2) {
Log.w("Bad line in config file: " + s);
continue;
}
// NULL value
if (parts[0].equalsIgnoreCase("NULL")) parts[0] = null;
if (parts[1].equalsIgnoreCase("NULL")) parts[1] = null;
if (parts[0] != null) parts[0] = parts[0].replace("\\n", "\n");
if (parts[1] != null) parts[1] = parts[1].replace("\\n", "\n");
// save extracted key-value pair
pairs.put(parts[0], parts[1]);
}
return pairs;
}
/**
* Save map to file
*
* @param target
* @param data
* @param allowNulls allow nulls.
* @throws IOException
*/
public static void mapToFile(File target, Map<String, String> data, boolean allowNulls) throws IOException
{
final List<String> lines = new ArrayList<>();
for (final Entry<String, String> e : data.entrySet()) {
String key = e.getKey();
String value = e.getValue();
if (!allowNulls && (key == null || value == null || key.length() == 0 || value.length() == 0)) continue;
if (key == null) key = "NULL";
if (value == null) value = "NULL";
key = key.replace("\n", "\\n");
value = value.replace("\n", "\\n");
lines.add(key + " = " + value);
}
String text = ""; // # File written by SimpleConfig
for (final String s : lines) {
if (text.length() > 0) text += "\n";
text += s;
}
FileUtils.stringToFile(target, text);
}
/**
* Save list to file
*
* @param target
* @param data
* @throws IOException
*/
public static void listToFile(File target, List<String> data) throws IOException
{
String text = ""; // # File written by SimpleConfig
for (String s : data) {
if (text.length() > 0) text += "\n";
if (s == null) s = "NULL";
s = s.replace("\n", "\\n");
text += s;
}
FileUtils.stringToFile(target, text);
}
}
@@ -0,0 +1,309 @@
package mightypork.utils.files.config;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
* Properties stored in file, alphabetically sorted.<br>
* Uses UTF-8 encoding and each property can have it's own comment.
*
* @author Ondřej Hruška (MightyPork)
*/
public class SortedProperties extends java.util.Properties {
/** Option: put empty line before each comment. */
public boolean cfgBlankRowBeforeComment = true;
/**
* Option: Separate sections by newline<br>
* Section = string before first dot in key.
*/
public boolean cfgBlankRowBetweenSections = true;
/** Comments for individual keys */
private final Hashtable<String, String> keyComments = new Hashtable<>();
private static void writeComments(BufferedWriter bw, String comm) throws IOException
{
final String comments = comm.replace("\n\n", "\n \n");
final int len = comments.length();
int current = 0;
int last = 0;
final char[] uu = new char[6];
uu[0] = '\\';
uu[1] = 'u';
while (current < len) {
final char c = comments.charAt(current);
if (c > '\u00ff' || c == '\n' || c == '\r') {
if (last != current) {
bw.write("# " + comments.substring(last, current));
}
if (c > '\u00ff') {
uu[2] = hexDigit(c, 12);
uu[3] = hexDigit(c, 8);
uu[4] = hexDigit(c, 4);
uu[5] = hexDigit(c, 0);
bw.write(new String(uu));
} else {
bw.newLine();
if (c == '\r' && current != len - 1 && comments.charAt(current + 1) == '\n') {
current++;
}
}
last = current + 1;
}
current++;
}
if (last != current) {
bw.write("# " + comments.substring(last, current));
}
bw.newLine();
bw.newLine();
bw.newLine();
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public synchronized Enumeration keys()
{
final Enumeration keysEnum = super.keys();
final Vector keyList = new Vector();
while (keysEnum.hasMoreElements()) {
keyList.add(keysEnum.nextElement());
}
Collections.sort(keyList); //sort!
return keyList.elements();
}
private static String saveConvert(String theString, boolean escapeSpace, boolean escapeUnicode)
{
final int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = Integer.MAX_VALUE;
}
final StringBuffer result = new StringBuffer(bufLen);
for (int x = 0; x < len; x++) {
final char ch = theString.charAt(x);
// Handle common case first, selecting largest block that
// avoids the specials below
if ((ch > 61) && (ch < 127)) {
if (ch == '\\') {
result.append('\\');
result.append('\\');
continue;
}
result.append(ch);
continue;
}
switch (ch) {
case ' ':
if (x == 0 || escapeSpace) {
result.append('\\');
}
result.append(' ');
break;
case '\t':
result.append('\\');
result.append('t');
break;
case '\n':
result.append('\\');
result.append('n');
break;
case '\r':
result.append('\\');
result.append('r');
break;
case '\f':
result.append('\\');
result.append('f');
break;
case '=': // Fall through
case ':': // Fall through
case '#': // Fall through
case '!':
result.append('\\');
result.append(ch);
break;
default:
if (((ch < 0x0020) || (ch > 0x007e)) & escapeUnicode) {
result.append('\\');
result.append('u');
result.append(hexDigit(ch, 12));
result.append(hexDigit(ch, 8));
result.append(hexDigit(ch, 4));
result.append(hexDigit(ch, 0));
} else {
result.append(ch);
}
}
}
return result.toString();
}
/**
* Set additional comment to a key
*
* @param key key for comment
* @param comment the comment
*/
public void setKeyComment(String key, String comment)
{
keyComments.put(key, comment);
}
@SuppressWarnings("rawtypes")
@Override
public void store(OutputStream out, String comments) throws IOException
{
final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
final boolean escUnicode = false;
boolean firstEntry = true;
String lastSectionBeginning = "";
if (comments != null) {
writeComments(bw, comments);
}
synchronized (this) {
for (final Enumeration e = keys(); e.hasMoreElements();) {
boolean wasNewLine = false;
String key = (String) e.nextElement();
String val = (String) get(key);
key = saveConvert(key, true, escUnicode);
val = saveConvert(val, false, escUnicode);
if (cfgBlankRowBetweenSections && !lastSectionBeginning.equals(key.split("[.]")[0])) {
if (!firstEntry) {
bw.newLine();
bw.newLine();
}
wasNewLine = true;
lastSectionBeginning = key.split("[.]")[0];
}
if (keyComments.containsKey(key)) {
String cm = keyComments.get(key);
cm = cm.replace("\r", "\n");
cm = cm.replace("\r\n", "\n");
cm = cm.replace("\n\n", "\n \n");
final String[] cmlines = cm.split("\n");
if (!wasNewLine && !firstEntry && cfgBlankRowBeforeComment) {
bw.newLine();
}
for (final String cmline : cmlines) {
bw.write("# " + cmline);
bw.newLine();
}
}
bw.write(key + " = " + val);
bw.newLine();
firstEntry = false;
}
}
bw.flush();
}
private static String escapifyStr(String str)
{
final StringBuilder result = new StringBuilder();
final int len = str.length();
for (int x = 0; x < len; x++) {
final char ch = str.charAt(x);
if (ch <= 0x007e) {
result.append(ch);
continue;
}
result.append('\\');
result.append('u');
result.append(hexDigit(ch, 12));
result.append(hexDigit(ch, 8));
result.append(hexDigit(ch, 4));
result.append(hexDigit(ch, 0));
}
return result.toString();
}
private static char hexDigit(char ch, int offset)
{
final int val = (ch >> offset) & 0xF;
if (val <= 9) {
return (char) ('0' + val);
}
return (char) ('A' + val - 10);
}
@Override
public synchronized void load(InputStream is) throws IOException
{
load(is, "utf-8");
}
public void load(InputStream is, String encoding) throws IOException
{
final StringBuilder sb = new StringBuilder();
final InputStreamReader isr = new InputStreamReader(is, encoding);
while (true) {
final int temp = isr.read();
if (temp < 0) {
break;
}
final char c = (char) temp;
sb.append(c);
}
final String read = sb.toString().replaceAll("(#|;|//|--)[^\n]*\n", "\n");
final String inputString = escapifyStr(read);
final byte[] bs = inputString.getBytes("ISO-8859-1");
final ByteArrayInputStream bais = new ByteArrayInputStream(bs);
super.load(bais);
}
}
@@ -0,0 +1,131 @@
package mightypork.utils.files.zip;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
/**
* Class for building a zip file
*
* @author Ondřej Hruška (MightyPork)
*/
public class ZipBuilder {
private final ZipOutputStream out;
private final HashSet<String> included = new HashSet<>();
/**
* @param target target zip file
* @throws IOException if the file is directory or cannot be created
*/
public ZipBuilder(File target) throws IOException
{
if (!target.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
final FileOutputStream dest = new FileOutputStream(target);
out = new ZipOutputStream(new BufferedOutputStream(dest));
}
/**
* Add stream to a path
*
* @param path path
* @param in stream
* @throws IOException
*/
public void addStream(String path, InputStream in) throws IOException
{
path = preparePath(path);
if (included.contains(path)) {
Log.f3("Zip already contains file " + path + ", skipping.");
return; // ignore
}
included.add(path);
out.putNextEntry(new ZipEntry(path));
FileUtils.copyStream(in, out);
}
/**
* Add string as a file
*
* @param path path
* @param text text to write
* @throws IOException
*/
public void addString(String path, String text) throws IOException
{
path = preparePath(path);
if (included.contains(path)) return; // ignore
included.add(path);
out.putNextEntry(new ZipEntry(path));
try(InputStream in = FileUtils.stringToStream(text)) {
FileUtils.copyStream(in, out);
}
}
/**
* Add resource obtained via FileUtils.getResource()
*
* @param path path
* @param resPath resource path
* @throws IOException
*/
public void addResource(String path, String resPath) throws IOException
{
path = preparePath(path);
if (included.contains(path)) return; // ignore
included.add(path);
out.putNextEntry(new ZipEntry(path));
try(InputStream in = FileUtils.getResource(resPath)) {
FileUtils.copyStream(in, out);
}
}
/**
* Normalize path
*
* @param path original path
* @return normalized path
*/
private static String preparePath(String path)
{
path = path.replace("\\", "/");
if (path.charAt(0) == '/') path = path.substring(1);
return path;
}
/**
* Close the zip stream
*
* @throws IOException
*/
public void close() throws IOException
{
out.close();
}
}
@@ -0,0 +1,187 @@
package mightypork.utils.files.zip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
import mightypork.utils.string.validation.StringFilter;
/**
* Utilities for manipulating zip files
*
* @author Ondřej Hruška (MightyPork)
*/
public class ZipUtils {
private static final int BUFFER_SIZE = 2048;
/**
* Extract zip file to target directory
*
* @param file zip file
* @param outputDir target directory
* @param filter string filter (will be used to test entry names (paths))
* @return list of entries extracted (paths)
* @throws IOException
*/
public static List<String> extractZip(File file, File outputDir, StringFilter filter) throws IOException
{
try(ZipFile zip = new ZipFile(file)) {
return extractZip(zip, outputDir, filter);
}
}
/**
* Extract zip file to target directory
*
* @param zip open zip file
* @param outputDir target directory
* @param filter string filter (will be used to test entry names (paths))
* @return list of entries extracted (paths)
* @throws IOException if the file)s) cannot be created
*/
public static List<String> extractZip(ZipFile zip, File outputDir, StringFilter filter) throws IOException
{
final ArrayList<String> files = new ArrayList<>();
if (!outputDir.mkdirs()) throw new IOException("Could not create output directory.");
final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
// process each entry
while (zipFileEntries.hasMoreElements()) {
final ZipEntry entry = zipFileEntries.nextElement();
// parse filename and path
final String entryPath = entry.getName();
final File destFile = new File(outputDir, entryPath);
final File destinationParent = destFile.getParentFile();
if (entry.isDirectory() || (filter != null && !filter.isValid(entryPath))) continue;
// make sure directories exist
if (!destinationParent.mkdirs()) throw new IOException("Could not create directory.");
if (!entry.isDirectory()) {
extractZipEntry(zip, entry, destFile);
files.add(entryPath);
}
}
return files;
}
/**
* Read zip entries and add their paths to a list
*
* @param zipFile open zip file
* @return list of entry names
* @throws IOException on error
*/
public static List<String> listZip(File zipFile) throws IOException
{
try(ZipFile zip = new ZipFile(zipFile)) {
return listZip(zip);
}
}
/**
* Read zip entries and add their paths to a list
*
* @param zip open zip file
* @return list of entry names
* @throws IOException on error
*/
public static List<String> listZip(ZipFile zip) throws IOException
{
final ArrayList<String> files = new ArrayList<>();
final Enumeration<? extends ZipEntry> zipFileEntries = zip.entries();
// process each entry
while (zipFileEntries.hasMoreElements()) {
final ZipEntry entry = zipFileEntries.nextElement();
if (!entry.isDirectory()) {
files.add(entry.getName());
}
}
return files;
}
/**
* Extract one zip entry to target file
*
* @param zip open zip file
* @param entry entry from the zip file
* @param destFile destination file ((NOT directory!)
* @throws IOException on error
*/
public static void extractZipEntry(ZipFile zip, ZipEntry entry, File destFile) throws IOException
{
if (!destFile.getParentFile().mkdirs()) throw new IOException("Could not create output directory.");
try(InputStream in = zip.getInputStream(entry);
BufferedInputStream is = new BufferedInputStream(in);
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE)) {
FileUtils.copyStream(is, dest);
}
}
/**
* Load zip entry to String
*
* @param zip open zip file
* @param entry entry from the zip file
* @return loaded string
* @throws IOException on error
*/
public static String zipEntryToString(ZipFile zip, ZipEntry entry) throws IOException
{
BufferedInputStream is = null;
try {
is = new BufferedInputStream(zip.getInputStream(entry));
final String s = FileUtils.streamToString(is);
return s;
} finally {
try {
if (is != null) is.close();
} catch (final IOException e) {
// ignore
}
}
}
public static boolean entryExists(File selectedFile, String string)
{
try(ZipFile zf = new ZipFile(selectedFile)) {
return zf.getEntry(string) != null;
} catch (final IOException | RuntimeException e) {
Log.w("Error reading zip.", e);
return false;
}
}
}