mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-30 02:50:39 +00:00
Extract util methods to separate classes
This commit is contained in:
parent
4ab904b88e
commit
7443225d96
6 changed files with 168 additions and 131 deletions
21
src/main/java/org/asamk/signal/util/DateUtils.java
Normal file
21
src/main/java/org/asamk/signal/util/DateUtils.java
Normal file
|
@ -0,0 +1,21 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
public class DateUtils {
|
||||
|
||||
private static final TimeZone tzUTC = TimeZone.getTimeZone("UTC");
|
||||
|
||||
private DateUtils() {
|
||||
}
|
||||
|
||||
public static String formatTimestamp(long timestamp) {
|
||||
Date date = new Date(timestamp);
|
||||
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Quoted "Z" to indicate UTC, no timezone offset
|
||||
df.setTimeZone(tzUTC);
|
||||
return timestamp + " (" + df.format(date) + ")";
|
||||
}
|
||||
}
|
|
@ -6,6 +6,9 @@ public class Hex {
|
|||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
||||
};
|
||||
|
||||
private Hex() {
|
||||
}
|
||||
|
||||
public static String toStringCondensed(byte[] bytes) {
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
|
|
55
src/main/java/org/asamk/signal/util/IOUtils.java
Normal file
55
src/main/java/org/asamk/signal/util/IOUtils.java
Normal file
|
@ -0,0 +1,55 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.attribute.PosixFilePermission;
|
||||
import java.nio.file.attribute.PosixFilePermissions;
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.nio.file.attribute.PosixFilePermission.*;
|
||||
|
||||
public class IOUtils {
|
||||
|
||||
private IOUtils() {
|
||||
}
|
||||
|
||||
public static File createTempFile() throws IOException {
|
||||
return File.createTempFile("signal_tmp_", ".tmp");
|
||||
}
|
||||
|
||||
public static String readAll(InputStream in, Charset charset) throws IOException {
|
||||
StringWriter output = new StringWriter();
|
||||
byte[] buffer = new byte[4096];
|
||||
int n;
|
||||
while (-1 != (n = in.read(buffer))) {
|
||||
output.write(new String(buffer, 0, n, charset));
|
||||
}
|
||||
return output.toString();
|
||||
}
|
||||
|
||||
public static void createPrivateDirectories(String path) throws IOException {
|
||||
final Path file = new File(path).toPath();
|
||||
try {
|
||||
Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE);
|
||||
Files.createDirectories(file, PosixFilePermissions.asFileAttribute(perms));
|
||||
} catch (UnsupportedOperationException e) {
|
||||
Files.createDirectories(file);
|
||||
}
|
||||
}
|
||||
|
||||
public static void createPrivateFile(String path) throws IOException {
|
||||
final Path file = new File(path).toPath();
|
||||
try {
|
||||
Set<PosixFilePermission> perms = EnumSet.of(OWNER_READ, OWNER_WRITE);
|
||||
Files.createFile(file, PosixFilePermissions.asFileAttribute(perms));
|
||||
} catch (UnsupportedOperationException e) {
|
||||
Files.createFile(file);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,56 @@
|
|||
package org.asamk.signal.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class Util {
|
||||
public static File createTempFile() throws IOException {
|
||||
return File.createTempFile("signal_tmp_", ".tmp");
|
||||
|
||||
private Util() {
|
||||
}
|
||||
|
||||
public static String formatSafetyNumber(String digits) {
|
||||
final int partCount = 12;
|
||||
int partSize = digits.length() / partCount;
|
||||
StringBuilder f = new StringBuilder(digits.length() + partCount);
|
||||
for (int i = 0; i < partCount; i++) {
|
||||
f.append(digits, i * partSize, (i * partSize) + partSize).append(" ");
|
||||
}
|
||||
return f.toString();
|
||||
}
|
||||
|
||||
public static Map<String, String> getQueryMap(String query) {
|
||||
String[] params = query.split("&");
|
||||
Map<String, String> map = new HashMap<>();
|
||||
for (String param : params) {
|
||||
String name = null;
|
||||
final String[] paramParts = param.split("=");
|
||||
try {
|
||||
name = URLDecoder.decode(paramParts[0], "utf-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Impossible
|
||||
}
|
||||
String value = null;
|
||||
try {
|
||||
value = URLDecoder.decode(paramParts[1], "utf-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
// Impossible
|
||||
}
|
||||
map.put(name, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public static String join(CharSequence separator, Iterable<? extends CharSequence> list) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (CharSequence str : list) {
|
||||
if (buf.length() > 0) {
|
||||
buf.append(separator);
|
||||
}
|
||||
buf.append(str);
|
||||
}
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue