-
Notifications
You must be signed in to change notification settings - Fork 5
/
CommandFunction.java
72 lines (58 loc) · 2.14 KB
/
CommandFunction.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
package com.teragrep.rlp_03.context.frame.function;
import com.teragrep.rlp_01.RelpCommand;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.function.BiFunction;
public class CommandFunction implements BiFunction<ByteBuffer, LinkedList<ByteBuffer>, Boolean> {
private static final int maximumCommandLength = 12; // serverclose + space
// private final Set<String> enabledCommands;
public CommandFunction() {
/*
this.enabledCommands = new HashSet<>();
this.enabledCommands.add("open");
this.enabledCommands.add("close");
this.enabledCommands.add("abort");
this.enabledCommands.add("serverclose");
this.enabledCommands.add("syslog");
this.enabledCommands.add("rsp");
int maximumLength = 0;
for (String command : enabledCommands) {
if (command.length() > maximumLength) {
maximumLength = command.length();
}
}
maximumCommandLength = maximumLength + 1; // space
*/
}
@Override
public Boolean apply(ByteBuffer input, LinkedList<ByteBuffer> bufferSliceList) {
ByteBuffer slice = input.slice();
int bytesRead = 0;
boolean rv = false;
while (input.hasRemaining()) {
byte b = input.get();
bytesRead++;
checkOverSize(bytesRead, bufferSliceList);
if (b == ' ') {
// remove ' ' from the input as it's complete
((ByteBuffer) slice).limit(bytesRead - 1);
rv = true;
break;
}
}
bufferSliceList.add(slice);
return rv;
}
private void checkOverSize(int bytesRead, LinkedList<ByteBuffer> bufferSliceList) {
long currentLength = 0;
for (ByteBuffer slice : bufferSliceList) {
currentLength = currentLength + ((ByteBuffer) slice).limit();
}
currentLength = currentLength + bytesRead;
if (currentLength > maximumCommandLength) {
throw new IllegalArgumentException("command too long");
}
}
}