Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d33bcc3

Browse files
Use reflection to call Base64 class
1 parent 6057ea4 commit d33bcc3

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

‎src/main/java/dev/gustavoavila/websocketclient/WebSocketClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.LinkedList;
2222
import java.util.Map;
2323
import java.util.Random;
24-
import java.util.Base64;
2524

2625
import javax.net.SocketFactory;
2726
import javax.net.ssl.SSLSocketFactory;
@@ -682,7 +681,7 @@ private void startConnection() throws IOException {
682681
byte[] key = new byte[16];
683682
Random random = new Random();
684683
random.nextBytes(key);
685-
String base64Key = Base64.getEncoder().encodeToString(key);
684+
String base64Key = Utils.encodeToBase64String(key);
686685

687686
byte[] handshake = createHandshake(base64Key);
688687
bos.write(handshake);
@@ -849,12 +848,12 @@ private void verifyServerHandshake(InputStream inputStream, String secWebSocketK
849848
MessageDigest md = MessageDigest.getInstance("SHA-1");
850849
md.update(keyConcatenation.getBytes(Charset.forName("ASCII")));
851850
byte[] sha1 = md.digest();
852-
String secWebSocketAccept = Base64.getEncoder().encodeToString(sha1);
851+
String secWebSocketAccept = Utils.encodeToBase64String(sha1);
853852
if (!secWebSocketAcceptValue.equals(secWebSocketAccept)) {
854853
throw new InvalidServerHandshakeException("Invalid value for header Sec-WebSocket-Accept. Expected: " + secWebSocketAccept + ", received: " + secWebSocketAcceptValue);
855854
}
856855
} catch (NoSuchAlgorithmException e) {
857-
throw new InvalidServerHandshakeException("Your platform does not support the SHA-1 algorithm");
856+
throw new RuntimeException("Your platform does not support the SHA-1 algorithm");
858857
}
859858
}
860859

‎src/main/java/dev/gustavoavila/websocketclient/common/Utils.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package dev.gustavoavila.websocketclient.common;
22

3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
36
/**
47
* Utility class
58
*
@@ -39,4 +42,35 @@ public static byte[] to8ByteArray(int value) {
3942
public static int fromByteArray(byte[] bytes) {
4043
return bytes[0] << 24 | (bytes[1] & 0xFF) << 16 | (bytes[2] & 0xFF) << 8 | (bytes[3] & 0xFF);
4144
}
45+
46+
/**
47+
* Encode data to base 64.
48+
* It checks if the VM is Dalvik (Android) and uses reflection to call the right classes
49+
*
50+
* @param data Data to be encoded
51+
* @return The encoded data
52+
*/
53+
public static String encodeToBase64String(byte[] data) {
54+
String vmName = System.getProperties().getProperty("java.vm.name");
55+
56+
try {
57+
if (vmName.equals("Dalvik")) {
58+
Method encodeToString = Class.forName("android.util.Base64").getMethod("encodeToString", byte[].class, int.class);;
59+
return (String) encodeToString.invoke(null, data, 2);
60+
} else {
61+
Method encoderMethod = Class.forName("java.util.Base64").getMethod("getEncoder");
62+
Object encoder = encoderMethod.invoke(null);
63+
Method encodeToString = encoder.getClass().getMethod("encodeToString", byte[].class);
64+
return (String) encodeToString.invoke(encoder, data);
65+
}
66+
} catch (ClassNotFoundException e) {
67+
throw new RuntimeException("Base64 class not found");
68+
} catch (NoSuchMethodException e) {
69+
throw new RuntimeException("Base64 class not found");
70+
} catch (IllegalAccessException e) {
71+
throw new RuntimeException("Base64 class not found");
72+
} catch (InvocationTargetException e) {
73+
throw new RuntimeException("Base64 class not found");
74+
}
75+
}
4276
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /