-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathParser.java
148 lines (118 loc) · 4.32 KB
/
Parser.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package io.socket.engineio.parser;
import java.util.HashMap;
import java.util.Map;
public class Parser {
public static final int PROTOCOL = 4;
private static final char SEPARATOR = '\u001e';
private static final Map<String, Integer> packets = new HashMap<String, Integer>() {{
put(Packet.OPEN, 0);
put(Packet.CLOSE, 1);
put(Packet.PING, 2);
put(Packet.PONG, 3);
put(Packet.MESSAGE, 4);
put(Packet.UPGRADE, 5);
put(Packet.NOOP, 6);
}};
private static final Map<Integer, String> packetslist = new HashMap<>();
static {
for (Map.Entry<String, Integer> entry : packets.entrySet()) {
packetslist.put(entry.getValue(), entry.getKey());
}
}
private static final Packet<String> err = new Packet<String>(Packet.ERROR, "parser error");
private Parser() {}
public static void encodePacket(Packet packet, EncodeCallback callback) {
if (packet.data instanceof byte[]) {
((EncodeCallback<byte[]>) callback).call(((Packet<byte[]>) packet).data);
} else {
String type = String.valueOf(packets.get(packet.type));
String content = packet.data != null ? String.valueOf(packet.data) : "";
((EncodeCallback<String>) callback).call(type + content);
}
}
private static void encodePacketAsBase64(Packet packet, EncodeCallback<String> callback) {
if (packet.data instanceof byte[]) {
byte[] data = ((Packet<byte[]>) packet).data;
String value = "b" + Base64.encodeToString(data, Base64.DEFAULT);
callback.call(value);
} else {
encodePacket(packet, callback);
}
}
public static Packet<String> decodePacket(String data) {
if (data == null) {
return err;
}
int type;
try {
type = Character.getNumericValue(data.charAt(0));
} catch (IndexOutOfBoundsException e) {
type = -1;
}
if (type < 0 || type >= packetslist.size()) {
return err;
}
if (data.length() > 1) {
return new Packet<String>(packetslist.get(type), data.substring(1));
} else {
return new Packet<String>(packetslist.get(type));
}
}
public static Packet decodeBase64Packet(String data) {
if (data == null) {
return err;
}
if (data.charAt(0) == 'b') {
return new Packet(Packet.MESSAGE, Base64.decode(data.substring(1), Base64.DEFAULT));
} else {
return decodePacket(data);
}
}
public static Packet<byte[]> decodePacket(byte[] data) {
return new Packet<>(Packet.MESSAGE, data);
}
public static void encodePayload(Packet[] packets, EncodeCallback<String> callback) {
if (packets.length == 0) {
callback.call("0:");
return;
}
final StringBuilder result = new StringBuilder();
for (int i = 0, l = packets.length; i < l; i++) {
final boolean isLast = i == l - 1;
encodePacketAsBase64(packets[i], new EncodeCallback<String>() {
@Override
public void call(String message) {
result.append(message);
if (!isLast) {
result.append(SEPARATOR);
}
}
});
}
callback.call(result.toString());
}
public static void decodePayload(String data, DecodePayloadCallback<String> callback) {
if (data == null || data.length() == 0) {
callback.call(err, 0, 1);
return;
}
String[] messages = data.split(String.valueOf(SEPARATOR));
for (int i = 0, l = messages.length; i < l; i++) {
Packet<String> packet = decodeBase64Packet(messages[i]);
if (err.type.equals(packet.type) && err.data.equals(packet.data)) {
callback.call(err, 0, 1);
return;
}
boolean ret = callback.call(packet, i, l);
if (!ret) {
return;
}
}
}
public interface EncodeCallback<T> {
void call(T data);
}
public interface DecodePayloadCallback<T> {
boolean call(Packet<T> packet, int index, int total);
}
}