Changed the root package from net.sf.briar to org.briarproject.

This commit is contained in:
akwizgran
2014-01-08 16:18:30 +00:00
parent dce70f487c
commit 832476412c
427 changed files with 2507 additions and 2507 deletions

View File

@@ -0,0 +1,65 @@
package org.briarproject.util;
public class ByteUtils {
/**
* The maximum value that can be represented as an unsigned 16-bit integer.
*/
public static final int MAX_16_BIT_UNSIGNED = 65535; // 2^16 - 1
/**
* The maximum value that can be represented as an unsigned 32-bit integer.
*/
public static final long MAX_32_BIT_UNSIGNED = 4294967295L; // 2^32 - 1
public static void writeUint8(int i, byte[] b, int offset) {
if(i < 0) throw new IllegalArgumentException();
if(i > 255) throw new IllegalArgumentException();
if(b.length < offset) throw new IllegalArgumentException();
b[offset] = (byte) i;
}
public static void writeUint16(int i, byte[] b, int offset) {
if(i < 0) throw new IllegalArgumentException();
if(i > MAX_16_BIT_UNSIGNED) throw new IllegalArgumentException();
if(b.length < offset + 2) throw new IllegalArgumentException();
b[offset] = (byte) (i >> 8);
b[offset + 1] = (byte) (i & 0xFF);
}
public static void writeUint32(long i, byte[] b, int offset) {
if(i < 0) throw new IllegalArgumentException();
if(i > MAX_32_BIT_UNSIGNED) throw new IllegalArgumentException();
if(b.length < offset + 4) throw new IllegalArgumentException();
b[offset] = (byte) (i >> 24);
b[offset + 1] = (byte) (i >> 16 & 0xFF);
b[offset + 2] = (byte) (i >> 8 & 0xFF);
b[offset + 3] = (byte) (i & 0xFF);
}
public static int readUint16(byte[] b, int offset) {
if(b.length < offset + 2) throw new IllegalArgumentException();
return ((b[offset] & 0xFF) << 8) | (b[offset + 1] & 0xFF);
}
public static long readUint32(byte[] b, int offset) {
if(b.length < offset + 4) throw new IllegalArgumentException();
return ((b[offset] & 0xFFL) << 24) | ((b[offset + 1] & 0xFFL) << 16)
| ((b[offset + 2] & 0xFFL) << 8) | (b[offset + 3] & 0xFFL);
}
public static void erase(byte[] b) {
for(int i = 0; i < b.length; i++) b[i] = 0;
}
public static int readUint(byte[] b, int bits) {
if(b.length << 3 < bits) throw new IllegalArgumentException();
int result = 0;
for(int i = 0; i < bits; i++) {
if((b[i >> 3] & 128 >> (i & 7)) != 0) result |= 1 << bits - i - 1;
}
assert result >= 0;
assert result < 1 << bits;
return result;
}
}

View File

@@ -0,0 +1,29 @@
package org.briarproject.util;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
public class LatchedReference<T> {
private final CountDownLatch latch = new CountDownLatch(1);
private final AtomicReference<T> reference = new AtomicReference<T>();
public boolean isSet() {
return reference.get() != null;
}
public boolean set(T t) {
if(t == null) throw new IllegalArgumentException();
if(reference.compareAndSet(null, t)) {
latch.countDown();
return true;
}
return false;
}
public T waitForReference(long timeout) throws InterruptedException {
latch.await(timeout, TimeUnit.MILLISECONDS);
return reference.get();
}
}

View File

@@ -0,0 +1,37 @@
package org.briarproject.util;
public class OsUtils {
private static final String os = System.getProperty("os.name");
private static final String version = System.getProperty("os.version");
private static final String vendor = System.getProperty("java.vendor");
public static boolean isWindows() {
return os != null && os.indexOf("Windows") != -1;
}
public static boolean isMac() {
return os != null && os.indexOf("Mac OS") != -1;
}
public static boolean isMacLeopardOrNewer() {
if(!isMac() || version == null) return false;
try {
String[] v = version.split("\\.");
if(v.length != 3) return false;
int major = Integer.parseInt(v[0]);
int minor = Integer.parseInt(v[1]);
return major >= 10 && minor >= 5;
} catch(NumberFormatException e) {
return false;
}
}
public static boolean isLinux() {
return os != null && os.indexOf("Linux") != -1 && !isAndroid();
}
public static boolean isAndroid() {
return vendor != null && vendor.indexOf("Android") != -1;
}
}

View File

@@ -0,0 +1,66 @@
package org.briarproject.util;
public class StringUtils {
private static final char[] HEX = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
public static boolean isNullOrEmpty(String s) {
return s == null || s.equals("");
}
/**
* 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;
}
/** Converts the given byte array to a hex character array. */
public static char[] toHexChars(byte[] bytes) {
char[] hex = new char[bytes.length * 2];
for(int i = 0, j = 0; i < bytes.length; i++) {
hex[j++] = HEX[(bytes[i] >> 4) & 0xF];
hex[j++] = HEX[bytes[i] & 0xF];
}
return hex;
}
/** Converts the given byte array to a hex string. */
public static String toHexString(byte[] bytes) {
return new String(toHexChars(bytes));
}
/** Converts the given hex string to a byte array. */
public static byte[] fromHexString(String hex) {
int len = hex.length();
if(len % 2 != 0) throw new IllegalArgumentException("Not a hex string");
byte[] bytes = new byte[len / 2];
for(int i = 0, j = 0; i < len; i += 2, j++) {
int high = hexDigitToInt(hex.charAt(i));
int low = hexDigitToInt(hex.charAt(i + 1));
bytes[j] = (byte) ((high << 4) + low);
}
return bytes;
}
private static int hexDigitToInt(char c) {
if(c >= '0' && c <= '9') return c - '0';
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
throw new IllegalArgumentException("Not a hex digit: " + c);
}
}

View File

@@ -0,0 +1,93 @@
package org.briarproject.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 {
/**
* 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();
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();
}
/**
* 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();
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;
}
/**
* Unzips the given stream to the given directory, skipping any zip entries
* that don't match the given regex (a null regex matches all entries). 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.getPath();
ZipInputStream zip = new ZipInputStream(in);
byte[] buf = new byte[1024];
ZipEntry entry;
while((entry = zip.getNextEntry()) != null) {
String name = entry.getName();
if(regex == null || 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);
}
}