Javadocs and unit tests. Woo!

This commit is contained in:
akwizgran
2011-06-22 11:42:33 +01:00
parent 9e76cc6a4f
commit eb1c855278
14 changed files with 287 additions and 28 deletions

View File

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

View File

@@ -67,7 +67,8 @@ public class FileUtils {
}
/**
* Copies the source file or directory to the destination directory.
* Copies the source file or directory to the destination directory. If the
* callback is not null it's called once for each file created.
*/
public static void copyRecursively(File src, File dest, Callback callback)
throws IOException {

View File

@@ -2,11 +2,19 @@ package net.sf.briar.util;
public class StringUtils {
/**
* Trims the given string to the given length, returning the head and
* appending "..." if the string was trimmed.
*/
public static String head(String s, int length) {
if(s.length() > length) return s.substring(0, length) + "...";
else return s;
}
/**
* Trims the given string to the given length, returning the tail and
* prepending "..." if the string was trimmed.
*/
public static String tail(String s, int length) {
if(s.length() > length) return "..." + s.substring(s.length() - length);
else return s;

View File

@@ -11,9 +11,13 @@ import java.util.zip.ZipOutputStream;
public class ZipUtils {
/**
* Copies the given file to the given zip, using the given path for the
* zip entry.
*/
public static void copyToZip(String path, File file, ZipOutputStream zip)
throws IOException {
assert file.isFile() : file.getAbsolutePath();
assert file.isFile();
zip.putNextEntry(new ZipEntry(path));
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[1024];
@@ -23,6 +27,12 @@ public class ZipUtils {
zip.closeEntry();
}
/**
* Copies the given directory to the given zip recursively, using the
* given path in place of the directory's name as the parent of all the zip
* entries. If the callback is not null it's called once for each file
* added.
*/
public static void copyToZipRecursively(String path, File dir,
ZipOutputStream zip, Callback callback) throws IOException {
assert dir.isDirectory();
@@ -42,6 +52,11 @@ public class ZipUtils {
else return path + "/" + name;
}
/**
* Unzips the given stream to the given directory, skipping any zip entries
* that don't match the given regex. If the callback is not null it's
* called once for each file extracted.
*/
public static void unzipStream(InputStream in, File dir, String regex,
Callback callback) throws IOException {
String path = dir.getCanonicalPath();