Skip to content

Commit 2dce35d

Browse files
committed
Fix integer parsing.
1 parent bf066e0 commit 2dce35d

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/com/codebutler/android_websockets/HybiParser.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import android.util.Log;
3434

3535
import java.io.*;
36-
import java.math.BigInteger;
3736
import java.util.Arrays;
3837
import java.util.List;
3938

@@ -327,7 +326,7 @@ private byte[] decode(String string) {
327326
}
328327

329328
private int getInteger(byte[] bytes) throws ProtocolError {
330-
long i = new BigInteger(bytes).longValue();
329+
long i = byteArrayToLong(bytes, 0, bytes.length);
331330
if (i < 0 || i > Integer.MAX_VALUE) {
332331
throw new ProtocolError("Bad integer: " + i);
333332
}
@@ -344,6 +343,18 @@ public ProtocolError(String detailMessage) {
344343
}
345344
}
346345

346+
private static long byteArrayToLong(byte[] b, int offset, int length) {
347+
if (b.length < length)
348+
throw new IllegalArgumentException("length must be less than or equal to b.length");
349+
350+
long value = 0;
351+
for (int i = 0; i < length; i++) {
352+
int shift = (length - 1 - i) * 8;
353+
value += (b[i + offset] & 0x000000FF) << shift;
354+
}
355+
return value;
356+
}
357+
347358
public static class HappyDataInputStream extends DataInputStream {
348359
public HappyDataInputStream(InputStream in) {
349360
super(in);

0 commit comments

Comments
 (0)