]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/manager/Utils.java
Move storage package to manager
[signal-cli] / src / main / java / org / asamk / signal / manager / Utils.java
index 05fcfb5ed84f7069f592d7e3c08b321b1e3d1a10..0a815ea9ad3971df098134f69478ca702954d089 100644 (file)
@@ -27,11 +27,11 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.net.URLConnection;
 import java.net.URLDecoder;
 import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -81,7 +81,21 @@ class Utils {
         Optional<String> caption = Optional.absent();
         Optional<String> blurHash = Optional.absent();
         final Optional<ResumableUploadSpec> resumableUploadSpec = Optional.absent();
-        return new SignalServiceAttachmentStream(attachmentStream, mime, attachmentSize, Optional.of(attachmentFile.getName()), false, false, preview, 0, 0, uploadTimestamp, caption, blurHash, null, null, resumableUploadSpec);
+        return new SignalServiceAttachmentStream(attachmentStream,
+                mime,
+                attachmentSize,
+                Optional.of(attachmentFile.getName()),
+                false,
+                false,
+                preview,
+                0,
+                0,
+                uploadTimestamp,
+                caption,
+                blurHash,
+                null,
+                null,
+                resumableUploadSpec);
     }
 
     static StreamDetails createStreamDetailsFromFile(File file) throws IOException {
@@ -96,7 +110,8 @@ class Utils {
 
     static CertificateValidator getCertificateValidator() {
         try {
-            ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(ServiceConfig.UNIDENTIFIED_SENDER_TRUST_ROOT), 0);
+            ECPublicKey unidentifiedSenderTrustRoot = Curve.decodePoint(Base64.decode(ServiceConfig.UNIDENTIFIED_SENDER_TRUST_ROOT),
+                    0);
             return new CertificateValidator(unidentifiedSenderTrustRoot);
         } catch (InvalidKeyException | IOException e) {
             throw new AssertionError(e);
@@ -107,31 +122,20 @@ class Utils {
         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
-            }
+            String name = URLDecoder.decode(paramParts[0], StandardCharsets.UTF_8);
+            String value = URLDecoder.decode(paramParts[1], StandardCharsets.UTF_8);
             map.put(name, value);
         }
         return map;
     }
 
     static String createDeviceLinkUri(DeviceLinkInfo info) {
-        try {
-            return "tsdevice:/?uuid=" + URLEncoder.encode(info.deviceIdentifier, "utf-8") + "&pub_key=" + URLEncoder.encode(Base64.encodeBytesWithoutPadding(info.deviceKey.serialize()), "utf-8");
-        } catch (UnsupportedEncodingException e) {
-            // Shouldn't happen
-            return null;
-        }
+        return "tsdevice:/?uuid="
+                + URLEncoder.encode(info.deviceIdentifier, StandardCharsets.UTF_8)
+                + "&pub_key="
+                + URLEncoder.encode(Base64.encodeBytesWithoutPadding(info.deviceKey.serialize()),
+                StandardCharsets.UTF_8);
     }
 
     static DeviceLinkInfo parseDeviceLinkUri(URI linkUri) throws IOException, InvalidKeyException {
@@ -195,7 +199,15 @@ class Utils {
             Optional<SignalServiceAddress> addressOptional = sourceUuid == null && source.isEmpty()
                     ? Optional.absent()
                     : Optional.of(new SignalServiceAddress(sourceUuid, source));
-            return new SignalServiceEnvelope(type, addressOptional, sourceDevice, timestamp, legacyMessage, content, serverReceivedTimestamp, serverDeliveredTimestamp, uuid);
+            return new SignalServiceEnvelope(type,
+                    addressOptional,
+                    sourceDevice,
+                    timestamp,
+                    legacyMessage,
+                    content,
+                    serverReceivedTimestamp,
+                    serverDeliveredTimestamp,
+                    uuid);
         }
     }
 
@@ -245,13 +257,18 @@ class Utils {
         return outputFile;
     }
 
-    static String computeSafetyNumber(SignalServiceAddress ownAddress, IdentityKey ownIdentityKey, SignalServiceAddress theirAddress, IdentityKey theirIdentityKey) {
+    static String computeSafetyNumber(
+            SignalServiceAddress ownAddress,
+            IdentityKey ownIdentityKey,
+            SignalServiceAddress theirAddress,
+            IdentityKey theirIdentityKey
+    ) {
         int version;
         byte[] ownId;
         byte[] theirId;
 
-        if (ServiceConfig.capabilities.isUuid()
-                && ownAddress.getUuid().isPresent() && theirAddress.getUuid().isPresent()) {
+        if (ServiceConfig.capabilities.isUuid() && ownAddress.getUuid().isPresent() && theirAddress.getUuid()
+                .isPresent()) {
             // Version 2: UUID user
             version = 2;
             ownId = UuidUtil.toByteArray(ownAddress.getUuid().get());
@@ -266,7 +283,11 @@ class Utils {
             theirId = theirAddress.getNumber().get().getBytes();
         }
 
-        Fingerprint fingerprint = new NumericFingerprintGenerator(5200).createFor(version, ownId, ownIdentityKey, theirId, theirIdentityKey);
+        Fingerprint fingerprint = new NumericFingerprintGenerator(5200).createFor(version,
+                ownId,
+                ownIdentityKey,
+                theirId,
+                theirIdentityKey);
         return fingerprint.getDisplayableFingerprint().getDisplayText();
     }