forked from robaho/httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
128 lines (104 loc) · 3.59 KB
/
Utils.java
File metadata and controls
128 lines (104 loc) · 3.59 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package robaho.net.httpserver.http2;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Deque;
import java.util.List;
public class Utils {
public static int convertToInt(byte[] bytes, int off) throws Exception {
return convertToInt(bytes, off, 4);
}
public static int convertToInt(byte[] bytes, int off, int length) throws Exception {
int result = 0;
if (off + length <= bytes.length) {
int counter = 0;
for (int i = off + length-1 ; i >=off ; i--) {
result |= (bytes[i] & 0x00ff) << (8 * counter);
counter++;
}
} else {
throw new Exception(String.format("cannot read %s bytes from offset, goes beyond array boundaries", length));
}
return result;
}
public static long convertToLong(byte[] bytes, int off, int length) throws Exception {
long result = 0;
if (off + length <= bytes.length) {
int counter = 0;
for (int i = off + length-1 ; i >=off ; i--) {
result |= Long.valueOf(bytes[i] & 0xff) << (8 * counter);
counter++;
}
} else {
throw new Exception(String.format("cannot read %s bytes from offset, goes beyond array boundries", length));
}
return result;
}
public static void convertToBinary(byte[] buffer, int pos, int input) {
convertToBinary(buffer, pos, input, 4);
}
public static void convertToBinary(byte[] buffer, int pos, int input, int length) {
for (int i = 0; i < length; i++) {
buffer[i+pos] = (byte) ((input >> (8 * (length-1-i))) & 255);
}
}
public static void writeBinary(OutputStream os,int input) throws IOException {
writeBinary(os, input, 4);
}
public static void writeBinary(OutputStream os,int input,int length) throws IOException {
for (int i = length-1; i>=0; i--) {
os.write((byte) ((input >> (8 * i)) & 0xFF));
}
}
private static final byte[] EMPTY = new byte[0];
public static byte[] combineByteArrays(byte[]... blocks) {
if(blocks.length==0) return EMPTY;
if(blocks.length==1) return blocks[0];
int totalLength = 0;
for (byte[] block : blocks) {
totalLength += block.length;
}
byte[] combined = new byte[totalLength];
int offset = 0;
for (byte[] block : blocks) {
System.arraycopy(block, 0, combined, offset, block.length);
offset += block.length;
}
return combined;
}
public static byte[] combineByteArrays(List<byte[]> blocks) {
if(blocks.isEmpty()) return EMPTY;
if(blocks.size()==1) return blocks.get(0);
int totalLength = 0;
for (byte[] block : blocks) {
totalLength += block.length;
}
byte[] combined = new byte[totalLength];
int offset = 0;
for (byte[] block : blocks) {
System.arraycopy(block, 0, combined, offset, block.length);
offset += block.length;
}
return combined;
}
public static byte[] combineByteArrays(List<byte[]> array1,List<byte[]> array2) {
int totalLength = 0;
for (byte[] block : array1) {
totalLength += block.length;
}
for (byte[] block : array2) {
totalLength += block.length;
}
if(totalLength==0) return EMPTY;
byte[] combined = new byte[totalLength];
int offset = 0;
for (byte[] block : array1) {
System.arraycopy(block, 0, combined, offset, block.length);
offset += block.length;
}
for (byte[] block : array2) {
System.arraycopy(block, 0, combined, offset, block.length);
offset += block.length;
}
return combined;
}
}