Initial commit with new directory structure.

This commit is contained in:
akwizgran
2011-06-21 18:01:28 +01:00
commit cd4f99df3d
98 changed files with 5811 additions and 0 deletions

1
util/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

3
util/build.xml Normal file
View File

@@ -0,0 +1,3 @@
<project name='util' default='compile'>
<import file='../build-common.xml'/>
</project>

View File

@@ -0,0 +1,90 @@
package net.sf.briar.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.CodeSource;
public class FileUtils {
/**
* Returns the directory where Briar is installed.
*/
public static File getBriarDirectory() {
CodeSource c = FileUtils.class.getProtectionDomain().getCodeSource();
File f = new File(c.getLocation().getPath());
assert f.exists();
if(f.isFile()) {
// Running from a jar - return the jar's grandparent
try {
f = f.getCanonicalFile().getParentFile().getParentFile();
} catch(IOException e) {
throw new RuntimeException(e);
}
} else {
// Running from Eclipse
try {
f = new File(f.getCanonicalFile().getParentFile(), "Briar");
} catch(IOException e) {
throw new RuntimeException(e);
}
f.mkdir();
}
assert f.exists();
assert f.isDirectory();
return f;
}
/**
* Creates and returns a temporary file.
*/
public static File createTempFile() throws IOException {
String rand = String.valueOf(1000 + (int) (Math.random() * 9000));
return File.createTempFile(rand, null);
}
/**
* Copies the contents of the source file to the destination file.
*/
public static void copy(File src, File dest) throws IOException {
FileInputStream in = new FileInputStream(src);
copy(in, dest);
}
/**
* Copies the contents of the input stream to the destination file.
*/
public static void copy(InputStream in, File dest) throws IOException {
FileOutputStream out = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int i;
while((i = in.read(buf, 0, buf.length)) != -1) out.write(buf, 0, i);
in.close();
out.flush();
out.close();
}
/**
* Copies the source file or directory to the destination directory.
*/
public static void copyRecursively(File src, File dest, Callback callback)
throws IOException {
assert dest.exists();
assert dest.isDirectory();
dest = new File(dest, src.getName());
if(src.isDirectory()) {
dest.mkdir();
for(File f : src.listFiles()) copyRecursively(f, dest, callback);
} else {
if(callback != null) callback.processingFile(dest);
copy(src, dest);
}
}
public interface Callback {
void processingFile(File f);
}
}

View File

@@ -0,0 +1,18 @@
package net.sf.briar.util;
public class OsUtils {
private static final String os = System.getProperty("os.name");
public static boolean isWindows() {
return os.indexOf("Windows") != -1;
}
public static boolean isMac() {
return os.indexOf("Mac OS") != -1;
}
public static boolean isLinux() {
return os.indexOf("Linux") != -1;
}
}

View File

@@ -0,0 +1,14 @@
package net.sf.briar.util;
public class StringUtils {
public static String head(String s, int length) {
if(s.length() > length) return s.substring(0, length) + "...";
else return s;
}
public static String tail(String s, int length) {
if(s.length() > length) return "..." + s.substring(s.length() - length);
else return s;
}
}

View File

@@ -0,0 +1,78 @@
package net.sf.briar.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class ZipUtils {
public static void copyToZip(String path, File file, ZipOutputStream zip)
throws IOException {
assert file.isFile() : file.getAbsolutePath();
zip.putNextEntry(new ZipEntry(path));
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[1024];
int i;
while((i = in.read(buf, 0, buf.length)) != -1) zip.write(buf, 0, i);
in.close();
zip.closeEntry();
}
public static void copyToZipRecursively(String path, File dir,
ZipOutputStream zip, Callback callback) throws IOException {
assert dir.isDirectory();
for(File child : dir.listFiles()) {
String childPath = extendPath(path, child.getName());
if(child.isDirectory()) {
copyToZipRecursively(childPath, child, zip, callback);
} else {
if(callback != null) callback.processingFile(child);
copyToZip(childPath, child, zip);
}
}
}
private static String extendPath(String path, String name) {
if(path == null || path.equals("")) return name;
else return path + "/" + name;
}
public static void unzipStream(InputStream in, File dir, String regex,
Callback callback) throws IOException {
String path = dir.getCanonicalPath();
ZipInputStream zip = new ZipInputStream(in);
byte[] buf = new byte[1024];
ZipEntry entry;
while((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
if(name.matches(regex)) {
File file = new File(path + "/" + name);
if(callback != null) callback.processingFile(file);
if(entry.isDirectory()) {
file.mkdirs();
} else {
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
int i;
while((i = zip.read(buf, 0, buf.length)) > 0) {
out.write(buf, 0, i);
}
out.flush();
out.close();
}
}
zip.closeEntry();
}
zip.close();
}
public interface Callback {
void processingFile(File f);
}
}