forked from hap-java/HAP-Java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathByteUtils.java
43 lines (38 loc) · 1.13 KB
/
ByteUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package io.github.hapjava.server.impl.pairing;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.util.Arrays;
class ByteUtils {
public static byte[] joinBytes(byte[]... piece) {
int pos = 0;
int length = 0;
for (int i = 0; i < piece.length; i++) {
length += piece[i].length;
}
byte[] ret = new byte[length];
for (int i = 0; i < piece.length; i++) {
System.arraycopy(piece[i], 0, ret, pos, piece[i].length);
pos += piece[i].length;
}
return ret;
}
public static byte[] toUnsignedByteArray(BigInteger i) {
byte[] array = i.toByteArray();
if (array[0] == 0) {
array = Arrays.copyOfRange(array, 1, array.length);
}
return array;
}
public static void copyStream(InputStream input, OutputStream output, int length)
throws IOException {
byte[] buffer = new byte[length];
int remaining = length;
int bytesRead;
while ((bytesRead = input.read(buffer, 0, remaining)) != -1 && remaining > 0) {
output.write(buffer, 0, bytesRead);
remaining -= bytesRead;
}
}
}