2013-04-18 23:35:02 +02:00
|
|
|
package net.osmand.util;
|
|
|
|
|
2015-10-21 23:58:22 +02:00
|
|
|
import net.osmand.IProgress;
|
2015-10-01 17:10:46 +02:00
|
|
|
import net.osmand.PlatformUtil;
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
2015-08-01 20:17:51 +02:00
|
|
|
import java.io.BufferedReader;
|
2013-04-18 23:35:02 +02:00
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.EOFException;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2015-07-17 09:15:17 +02:00
|
|
|
import java.io.FileOutputStream;
|
2013-04-18 23:35:02 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2015-08-01 20:17:51 +02:00
|
|
|
import java.io.InputStreamReader;
|
2013-04-18 23:35:02 +02:00
|
|
|
import java.io.OutputStream;
|
2015-07-28 01:44:05 +02:00
|
|
|
import java.util.Arrays;
|
2015-06-30 10:31:26 +02:00
|
|
|
import java.util.Collections;
|
2015-07-28 01:44:05 +02:00
|
|
|
import java.util.Comparator;
|
2015-06-30 10:31:26 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
2016-05-16 19:20:24 +02:00
|
|
|
import java.util.Locale;
|
2015-06-30 10:31:26 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2013-04-18 23:35:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-16 19:13:09 +02:00
|
|
|
* Basic algorithms that are not in jdk
|
2013-04-18 23:35:02 +02:00
|
|
|
*/
|
|
|
|
public class Algorithms {
|
|
|
|
private static final int BUFFER_SIZE = 1024;
|
|
|
|
private static final Log log = PlatformUtil.getLog(Algorithms.class);
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static boolean isEmpty(String s) {
|
2013-04-18 23:35:02 +02:00
|
|
|
return s == null || s.length() == 0;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static boolean isBlank(String s) {
|
2016-05-08 23:25:30 +02:00
|
|
|
return s == null || s.trim().length() == 0;
|
|
|
|
}
|
2015-09-27 20:53:26 +02:00
|
|
|
|
|
|
|
public static boolean stringsEqual(String s1, String s2) {
|
|
|
|
if (s1 == null && s2 == null) {
|
|
|
|
return true;
|
|
|
|
} else if (s1 == null) {
|
|
|
|
return false;
|
|
|
|
} else if (s2 == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 19:20:24 +02:00
|
|
|
return s2.equals(s1);
|
2015-09-27 20:53:26 +02:00
|
|
|
}
|
|
|
|
|
2014-04-26 14:38:27 +02:00
|
|
|
public static long parseLongSilently(String input, long def) {
|
2016-05-16 19:20:24 +02:00
|
|
|
if (input != null && input.length() > 0) {
|
2014-04-26 14:38:27 +02:00
|
|
|
try {
|
|
|
|
return Long.parseLong(input);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return def;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2015-08-01 20:17:51 +02:00
|
|
|
public static String getFileNameWithoutExtension(File f) {
|
|
|
|
String name = f.getName();
|
|
|
|
int i = name.indexOf('.');
|
2016-05-16 19:20:24 +02:00
|
|
|
if (i >= 0) {
|
2015-08-01 20:17:51 +02:00
|
|
|
name = name.substring(0, i);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
public static String getFileExtension(File f) {
|
|
|
|
String name = f.getName();
|
|
|
|
int i = name.lastIndexOf(".");
|
|
|
|
return name.substring(i + 1);
|
|
|
|
}
|
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static File[] getSortedFilesVersions(File dir) {
|
2015-07-28 01:44:05 +02:00
|
|
|
File[] listFiles = dir.listFiles();
|
|
|
|
if (listFiles != null) {
|
2015-07-28 22:22:28 +02:00
|
|
|
Arrays.sort(listFiles, getFileVersionComparator());
|
2015-07-28 01:44:05 +02:00
|
|
|
}
|
|
|
|
return listFiles;
|
|
|
|
}
|
2015-07-28 22:22:28 +02:00
|
|
|
|
|
|
|
public static Comparator<File> getFileVersionComparator() {
|
|
|
|
return new Comparator<File>() {
|
|
|
|
@Override
|
|
|
|
public int compare(File o1, File o2) {
|
|
|
|
return -simplifyFileName(o1.getName()).compareTo(simplifyFileName(o2.getName()));
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-07-28 22:22:28 +02:00
|
|
|
public String simplifyFileName(String fn) {
|
|
|
|
String lc = fn.toLowerCase();
|
2016-05-16 19:20:24 +02:00
|
|
|
if (lc.contains(".")) {
|
2015-07-28 22:22:28 +02:00
|
|
|
lc = lc.substring(0, lc.indexOf("."));
|
|
|
|
}
|
|
|
|
if (lc.endsWith("_2")) {
|
|
|
|
lc = lc.substring(0, lc.length() - "_2".length());
|
|
|
|
}
|
|
|
|
boolean hasTimestampEnd = false;
|
2016-05-16 19:20:24 +02:00
|
|
|
for (int i = 0; i < lc.length(); i++) {
|
|
|
|
if (lc.charAt(i) >= '0' && lc.charAt(i) <= '9') {
|
2015-07-28 22:22:28 +02:00
|
|
|
hasTimestampEnd = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:20:24 +02:00
|
|
|
if (!hasTimestampEnd) {
|
2015-07-28 22:22:28 +02:00
|
|
|
lc += "_00_00_00";
|
|
|
|
}
|
|
|
|
return lc;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-06-30 10:31:26 +02:00
|
|
|
private static final char CHAR_TOSPLIT = 0x01;
|
|
|
|
|
|
|
|
public static Map<String, String> decodeMap(String s) {
|
|
|
|
if (isEmpty(s)) {
|
|
|
|
return Collections.emptyMap();
|
|
|
|
}
|
2016-05-25 14:26:46 +02:00
|
|
|
Map<String, String> names = new HashMap<String, String>();
|
2015-06-30 10:31:26 +02:00
|
|
|
String[] split = s.split(CHAR_TOSPLIT + "");
|
2015-06-30 13:16:01 +02:00
|
|
|
// last split is an empty string
|
2015-06-30 10:31:26 +02:00
|
|
|
for (int i = 1; i < split.length; i += 2) {
|
|
|
|
names.put(split[i - 1], split[i]);
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-06-30 10:31:26 +02:00
|
|
|
public static String encodeMap(Map<String, String> names) {
|
|
|
|
if (names != null) {
|
|
|
|
Iterator<Entry<String, String>> it = names.entrySet().iterator();
|
|
|
|
StringBuilder bld = new StringBuilder();
|
|
|
|
while (it.hasNext()) {
|
|
|
|
Entry<String, String> e = it.next();
|
|
|
|
bld.append(e.getKey()).append(CHAR_TOSPLIT)
|
2016-05-16 19:20:24 +02:00
|
|
|
.append(e.getValue().replace(CHAR_TOSPLIT, (char) (CHAR_TOSPLIT + 1)));
|
2015-06-30 13:16:01 +02:00
|
|
|
bld.append(CHAR_TOSPLIT);
|
2015-06-30 10:31:26 +02:00
|
|
|
}
|
2015-06-30 13:16:01 +02:00
|
|
|
return bld.toString();
|
2015-06-30 10:31:26 +02:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2014-02-02 14:06:09 +01:00
|
|
|
public static int findFirstNumberEndIndex(String value) {
|
|
|
|
int i = 0;
|
|
|
|
boolean valid = false;
|
|
|
|
if (value.length() > 0 && value.charAt(0) == '-') {
|
|
|
|
i++;
|
|
|
|
}
|
2015-08-03 09:44:40 +02:00
|
|
|
while (i < value.length() && (isDigit(value.charAt(i)) || value.charAt(i) == '.')) {
|
2014-02-02 14:06:09 +01:00
|
|
|
i++;
|
|
|
|
valid = true;
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
return i;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-08-04 10:51:46 +02:00
|
|
|
public static boolean isDigit(char charAt) {
|
2015-08-03 09:44:40 +02:00
|
|
|
return charAt >= '0' && charAt <= '9';
|
|
|
|
}
|
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
/**
|
|
|
|
* Determine whether a file is a ZIP File.
|
|
|
|
*/
|
|
|
|
public static boolean isZipFile(File file) throws IOException {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!file.canRead()) {
|
|
|
|
throw new IOException("Cannot read file " + file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
if (file.length() < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
FileInputStream in = new FileInputStream(file);
|
|
|
|
int test = readInt(in);
|
|
|
|
in.close();
|
|
|
|
return test == 0x504b0304;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
private static int readInt(InputStream in) throws IOException {
|
|
|
|
int ch1 = in.read();
|
|
|
|
int ch2 = in.read();
|
|
|
|
int ch3 = in.read();
|
|
|
|
int ch4 = in.read();
|
|
|
|
if ((ch1 | ch2 | ch3 | ch4) < 0)
|
|
|
|
throw new EOFException();
|
|
|
|
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + ch4);
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static String capitalizeFirstLetterAndLowercase(String s) {
|
|
|
|
if (s != null && s.length() > 1) {
|
|
|
|
// not very efficient algorithm
|
|
|
|
return Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase();
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static boolean objectEquals(Object a, Object b) {
|
|
|
|
if (a == null) {
|
2013-04-18 23:35:02 +02:00
|
|
|
return b == null;
|
|
|
|
} else {
|
|
|
|
return a.equals(b);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2014-06-01 11:18:43 +02:00
|
|
|
/**
|
2016-05-16 19:20:24 +02:00
|
|
|
* Parse the color string, and return the corresponding color-int.
|
|
|
|
* If the string cannot be parsed, throws an IllegalArgumentException
|
|
|
|
* exception. Supported formats are:
|
|
|
|
* #RRGGBB
|
|
|
|
* #AARRGGBB
|
|
|
|
* 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
|
|
|
|
* 'yellow', 'lightgray', 'darkgray'
|
|
|
|
*/
|
|
|
|
public static int parseColor(String colorString) {
|
|
|
|
if (colorString.charAt(0) == '#') {
|
|
|
|
// Use a long to avoid rollovers on #ffXXXXXX
|
|
|
|
if (colorString.length() == 4) {
|
|
|
|
colorString = "#" +
|
|
|
|
colorString.charAt(1) + colorString.charAt(1) +
|
|
|
|
colorString.charAt(2) + colorString.charAt(2) +
|
|
|
|
colorString.charAt(3) + colorString.charAt(3);
|
|
|
|
}
|
|
|
|
long color = Long.parseLong(colorString.substring(1), 16);
|
|
|
|
if (colorString.length() == 7) {
|
|
|
|
// Set the alpha value
|
|
|
|
color |= 0x00000000ff000000;
|
|
|
|
} else if (colorString.length() != 9) {
|
|
|
|
throw new IllegalArgumentException("Unknown color " + colorString); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
return (int) color;
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException("Unknown color " + colorString); //$NON-NLS-1$
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static int extractFirstIntegerNumber(String s) {
|
|
|
|
int i = 0;
|
|
|
|
for (int k = 0; k < s.length(); k++) {
|
2015-08-03 09:44:40 +02:00
|
|
|
if (isDigit(s.charAt(k))) {
|
2013-04-18 23:35:02 +02:00
|
|
|
i = i * 10 + (s.charAt(k) - '0');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-06-08 22:54:12 +02:00
|
|
|
public static int extractIntegerNumber(String s) {
|
|
|
|
int i = 0;
|
2016-05-16 19:20:24 +02:00
|
|
|
int k;
|
2015-06-08 22:54:12 +02:00
|
|
|
for (k = 0; k < s.length(); k++) {
|
2015-08-03 09:44:40 +02:00
|
|
|
if (isDigit(s.charAt(k))) {
|
2015-06-08 22:54:12 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; k < s.length(); k++) {
|
2015-08-03 09:44:40 +02:00
|
|
|
if (isDigit(s.charAt(k))) {
|
2015-06-08 22:54:12 +02:00
|
|
|
i = i * 10 + (s.charAt(k) - '0');
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-06-08 22:16:43 +02:00
|
|
|
public static String extractIntegerPrefix(String s) {
|
|
|
|
int k = 0;
|
|
|
|
for (; k < s.length(); k++) {
|
|
|
|
if (Character.isDigit(s.charAt(k))) {
|
|
|
|
return s.substring(0, k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-02-21 19:21:18 +01:00
|
|
|
public static String extractOnlyIntegerSuffix(String s) {
|
|
|
|
int k = 0;
|
|
|
|
for (; k < s.length(); k++) {
|
|
|
|
if (Character.isDigit(s.charAt(k))) {
|
|
|
|
return s.substring(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static String extractIntegerSuffix(String s) {
|
|
|
|
int k = 0;
|
|
|
|
for (; k < s.length(); k++) {
|
|
|
|
if (!Character.isDigit(s.charAt(k))) {
|
|
|
|
return s.substring(k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
@SuppressWarnings("TryFinallyCanBeTryWithResources")
|
2015-07-17 09:15:17 +02:00
|
|
|
public static void fileCopy(File src, File dst) throws IOException {
|
|
|
|
FileOutputStream fout = new FileOutputStream(dst);
|
|
|
|
try {
|
|
|
|
FileInputStream fin = new FileInputStream(src);
|
|
|
|
try {
|
|
|
|
Algorithms.streamCopy(fin, fout);
|
|
|
|
} finally {
|
|
|
|
fin.close();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
fout.close();
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:20:24 +02:00
|
|
|
|
|
|
|
public static void streamCopy(InputStream in, OutputStream out) throws IOException {
|
2013-04-18 23:35:02 +02:00
|
|
|
byte[] b = new byte[BUFFER_SIZE];
|
|
|
|
int read;
|
|
|
|
while ((read = in.read(b)) != -1) {
|
|
|
|
out.write(b, 0, read);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void streamCopy(InputStream in, OutputStream out, IProgress pg, int bytesDivisor) throws IOException {
|
2015-10-21 23:58:22 +02:00
|
|
|
byte[] b = new byte[BUFFER_SIZE];
|
|
|
|
int read;
|
|
|
|
int cp = 0;
|
|
|
|
while ((read = in.read(b)) != -1) {
|
|
|
|
out.write(b, 0, read);
|
|
|
|
cp += read;
|
2016-05-16 19:20:24 +02:00
|
|
|
if (pg != null && cp > bytesDivisor) {
|
2015-10-21 23:58:22 +02:00
|
|
|
pg.progress(cp / bytesDivisor);
|
2016-05-16 19:13:09 +02:00
|
|
|
cp = cp % bytesDivisor;
|
2015-10-21 23:58:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void oneByteStreamCopy(InputStream in, OutputStream out) throws IOException {
|
2013-04-18 23:35:02 +02:00
|
|
|
int read;
|
|
|
|
while ((read = in.read()) != -1) {
|
|
|
|
out.write(read);
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void closeStream(Closeable stream) {
|
2013-04-18 23:35:02 +02:00
|
|
|
try {
|
2016-05-16 19:20:24 +02:00
|
|
|
if (stream != null) {
|
2013-04-18 23:35:02 +02:00
|
|
|
stream.close();
|
|
|
|
}
|
2016-05-16 19:20:24 +02:00
|
|
|
} catch (IOException e) {
|
2013-04-18 23:35:02 +02:00
|
|
|
log.warn("Closing stream warn", e); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2016-05-17 09:45:28 +02:00
|
|
|
public static void updateAllExistingImgTilesToOsmandFormat(File f) {
|
2016-05-16 19:20:24 +02:00
|
|
|
if (f.isDirectory()) {
|
|
|
|
for (File c : f.listFiles()) {
|
2013-04-18 23:35:02 +02:00
|
|
|
updateAllExistingImgTilesToOsmandFormat(c);
|
|
|
|
}
|
2016-05-16 19:20:24 +02:00
|
|
|
} else if (f.getName().endsWith(".png") || f.getName().endsWith(".jpg")) { //$NON-NLS-1$ //$NON-NLS-2$
|
2013-04-18 23:35:02 +02:00
|
|
|
f.renameTo(new File(f.getAbsolutePath() + ".tile")); //$NON-NLS-1$
|
2016-05-16 19:20:24 +02:00
|
|
|
} else if (f.getName().endsWith(".andnav2")) { //$NON-NLS-1$
|
2013-04-18 23:35:02 +02:00
|
|
|
f.renameTo(new File(f.getAbsolutePath().substring(0, f.getAbsolutePath().length() - ".andnav2".length()) + ".tile")); //$NON-NLS-1$ //$NON-NLS-2$
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2015-08-01 20:17:51 +02:00
|
|
|
public static StringBuilder readFromInputStream(InputStream i) throws IOException {
|
|
|
|
StringBuilder responseBody = new StringBuilder();
|
|
|
|
responseBody.setLength(0);
|
|
|
|
if (i != null) {
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(i, "UTF-8"), 256); //$NON-NLS-1$
|
|
|
|
String s;
|
|
|
|
boolean f = true;
|
|
|
|
while ((s = in.readLine()) != null) {
|
|
|
|
if (!f) {
|
|
|
|
responseBody.append("\n"); //$NON-NLS-1$
|
|
|
|
} else {
|
|
|
|
f = false;
|
|
|
|
}
|
|
|
|
responseBody.append(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return responseBody;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static boolean removeAllFiles(File f) {
|
|
|
|
if (f == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (f.isDirectory()) {
|
|
|
|
File[] fs = f.listFiles();
|
2016-05-16 19:20:24 +02:00
|
|
|
if (fs != null) {
|
|
|
|
for (File c : fs) {
|
|
|
|
removeAllFiles(c);
|
|
|
|
}
|
2013-04-18 23:35:02 +02:00
|
|
|
}
|
|
|
|
return f.delete();
|
|
|
|
} else {
|
|
|
|
return f.delete();
|
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static long parseLongFromBytes(byte[] bytes, int offset) {
|
2016-05-16 19:20:24 +02:00
|
|
|
long o = 0xff & bytes[offset + 7];
|
2013-04-18 23:35:02 +02:00
|
|
|
o = o << 8 | (0xff & bytes[offset + 6]);
|
|
|
|
o = o << 8 | (0xff & bytes[offset + 5]);
|
|
|
|
o = o << 8 | (0xff & bytes[offset + 4]);
|
|
|
|
o = o << 8 | (0xff & bytes[offset + 3]);
|
|
|
|
o = o << 8 | (0xff & bytes[offset + 2]);
|
|
|
|
o = o << 8 | (0xff & bytes[offset + 1]);
|
2013-07-31 20:14:11 +02:00
|
|
|
o = o << 8 | (0xff & bytes[offset]);
|
2013-04-18 23:35:02 +02:00
|
|
|
return o;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void putLongToBytes(byte[] bytes, int offset, long l) {
|
2013-04-18 23:35:02 +02:00
|
|
|
bytes[offset] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 1] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 2] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 3] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 4] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 5] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 6] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 7] = (byte) (l & 0xff);
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static int parseIntFromBytes(byte[] bytes, int offset) {
|
|
|
|
int o = (0xff & bytes[offset + 3]) << 24;
|
|
|
|
o |= (0xff & bytes[offset + 2]) << 16;
|
|
|
|
o |= (0xff & bytes[offset + 1]) << 8;
|
2013-07-31 20:14:11 +02:00
|
|
|
o |= (0xff & bytes[offset]);
|
2013-04-18 23:35:02 +02:00
|
|
|
return o;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void putIntToBytes(byte[] bytes, int offset, int l) {
|
2013-04-18 23:35:02 +02:00
|
|
|
bytes[offset] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 1] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 2] = (byte) (l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
bytes[offset + 3] = (byte) (l & 0xff);
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static void writeLongInt(OutputStream stream, long l) throws IOException {
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
l >>= 8;
|
|
|
|
stream.write((int) (l & 0xff));
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static void writeInt(OutputStream stream, int l) throws IOException {
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
|
|
|
|
public static void writeSmallInt(OutputStream stream, int l) throws IOException {
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
l >>= 8;
|
|
|
|
stream.write(l & 0xff);
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
public static int parseSmallIntFromBytes(byte[] bytes, int offset) {
|
|
|
|
int s = (0xff & bytes[offset + 1]) << 8;
|
2013-07-31 20:14:11 +02:00
|
|
|
s |= (0xff & bytes[offset]);
|
2013-04-18 23:35:02 +02:00
|
|
|
return s;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static void putSmallIntBytes(byte[] bytes, int offset, int s) {
|
2013-04-18 23:35:02 +02:00
|
|
|
bytes[offset] = (byte) (s & 0xff);
|
|
|
|
s >>= 8;
|
|
|
|
bytes[offset + 1] = (byte) (s & 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean containsDigit(String name) {
|
|
|
|
for (int i = 0; i < name.length(); i++) {
|
|
|
|
if (Character.isDigit(name.charAt(i))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2013-04-18 23:35:02 +02:00
|
|
|
|
2014-04-03 04:12:20 +02:00
|
|
|
public static String formatDuration(int seconds, boolean fullForm) {
|
2013-04-18 23:35:02 +02:00
|
|
|
String sec;
|
|
|
|
if (seconds % 60 < 10) {
|
|
|
|
sec = "0" + (seconds % 60);
|
|
|
|
} else {
|
|
|
|
sec = (seconds % 60) + "";
|
|
|
|
}
|
|
|
|
int minutes = seconds / 60;
|
2014-04-03 04:12:20 +02:00
|
|
|
if ((!fullForm) && (minutes < 60)) {
|
2013-04-18 23:35:02 +02:00
|
|
|
return minutes + ":" + sec;
|
|
|
|
} else {
|
|
|
|
String min;
|
|
|
|
if (minutes % 60 < 10) {
|
|
|
|
min = "0" + (minutes % 60);
|
|
|
|
} else {
|
|
|
|
min = (minutes % 60) + "";
|
|
|
|
}
|
|
|
|
int hours = minutes / 60;
|
|
|
|
return hours + ":" + min + ":" + sec;
|
|
|
|
}
|
|
|
|
}
|
2015-10-01 17:10:46 +02:00
|
|
|
|
|
|
|
public static String formatMinutesDuration(int minutes) {
|
|
|
|
if (minutes < 60) {
|
|
|
|
return String.valueOf(minutes);
|
|
|
|
} else {
|
2015-12-29 16:11:55 +01:00
|
|
|
int min = minutes % 60;
|
2015-10-01 17:10:46 +02:00
|
|
|
int hours = minutes / 60;
|
2016-05-16 19:20:24 +02:00
|
|
|
return String.format(Locale.UK, "%02d:%02d", hours, min);
|
2015-10-01 17:10:46 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-16 19:13:09 +02:00
|
|
|
|
2016-05-16 19:20:24 +02:00
|
|
|
public static <T extends Enum<T>> T parseEnumValue(T[] cl, String val, T defaultValue) {
|
|
|
|
for (T aCl : cl) {
|
|
|
|
if (aCl.name().equalsIgnoreCase(val)) {
|
|
|
|
return aCl;
|
2013-04-18 23:35:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2014-06-08 21:01:46 +02:00
|
|
|
|
|
|
|
public static String colorToString(int color) {
|
|
|
|
if ((0xFF000000 & color) == 0xFF000000) {
|
2015-06-12 01:28:41 +02:00
|
|
|
return "#" + format(6, Integer.toHexString(color & 0x00FFFFFF)); //$NON-NLS-1$
|
2014-06-08 21:01:46 +02:00
|
|
|
} else {
|
2015-06-12 01:28:41 +02:00
|
|
|
return "#" + format(8, Integer.toHexString(color)); //$NON-NLS-1$
|
2014-06-08 21:01:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-12 01:28:41 +02:00
|
|
|
|
|
|
|
private static String format(int i, String hexString) {
|
2016-05-16 19:20:24 +02:00
|
|
|
while (hexString.length() < i) {
|
2015-06-12 01:28:41 +02:00
|
|
|
hexString = "0" + hexString;
|
|
|
|
}
|
|
|
|
return hexString;
|
|
|
|
}
|
2016-03-29 16:47:04 +02:00
|
|
|
|
|
|
|
public static int getRainbowColor(double percent) {
|
|
|
|
|
|
|
|
// Given an input percentage (0.0-1.0) this will produce a colour from a "wide rainbow"
|
|
|
|
// from purple (low) to red(high). This is useful for producing value-based colourations (e.g., altitude)
|
|
|
|
|
|
|
|
double a = (1. - percent) * 5.;
|
2016-05-16 19:20:24 +02:00
|
|
|
int X = (int) Math.floor(a);
|
|
|
|
int Y = (int) (Math.floor(255 * (a - X)));
|
2016-03-29 16:47:04 +02:00
|
|
|
switch (X) {
|
2016-05-16 19:20:24 +02:00
|
|
|
case 0: return 0xFFFF0000 + (Y << 8);
|
|
|
|
case 1: return 0xFF00FF00 + ((255 - Y) << 16);
|
2016-03-29 16:47:04 +02:00
|
|
|
case 2: return 0xFF00FF00 + Y;
|
2016-05-16 19:20:24 +02:00
|
|
|
case 3: return 0xFF0000FF + ((255 - Y) << 8);
|
2016-03-29 16:47:04 +02:00
|
|
|
case 4: return 0xFF0000FF + (Y << 16);
|
|
|
|
}
|
|
|
|
return 0xFFFF00FF;
|
|
|
|
}
|
|
|
|
|
2016-05-30 22:09:20 +02:00
|
|
|
}
|