|
1 |
| -package com.paulsen.io; |
2 |
| - |
3 |
| -public class CustomProtocol { |
4 |
| - |
5 |
| - private String IDENTIFIER = "CP"; |
6 |
| - |
7 |
| - private char BLOCK_START = '[', BLOCK_END = ']'; |
8 |
| - /** |
9 |
| - * Replacement if start- or end-character is included in data (1)=> store data |
10 |
| - * with replaced characters (2)=> read data with replacements back replaced to |
11 |
| - * start or end |
12 |
| - */ |
13 |
| - private String START_REPLACEMENT = "^<<^", END_REPLACEMENT = "^>>^"; |
14 |
| - |
15 |
| - public CustomProtocol() { |
16 |
| - } |
17 |
| - |
18 |
| - public CustomProtocol(String identifier, char blockStart, char blockEnd, String startReplacement, |
19 |
| - String endRemplacement) { |
20 |
| - IDENTIFIER = identifier; |
21 |
| - BLOCK_START = blockStart; |
22 |
| - BLOCK_END = blockEnd; |
23 |
| - START_REPLACEMENT = startReplacement; |
24 |
| - END_REPLACEMENT = endRemplacement; |
25 |
| - } |
26 |
| - |
27 |
| - public boolean isPartOfProtocol(String s) { |
28 |
| - s = trimEnd(trimStart(s)); |
29 |
| - // Message must start immediatley with IDENTIFIER and BLOCK_START |
30 |
| - if (s.startsWith(IDENTIFIER + BLOCK_START) && s.endsWith(String.valueOf(BLOCK_END))) { |
31 |
| - String message = removeStart(s, IDENTIFIER.length()); |
32 |
| - // Message can only contain BLOCK_START && BLOCK_END once |
33 |
| - if (count(message, BLOCK_START) != 1 || count(message, BLOCK_END) != 1) { |
34 |
| - return false; |
35 |
| - } |
36 |
| - return true; |
37 |
| - } |
38 |
| - return false; |
39 |
| - } |
40 |
| - |
41 |
| - /** |
42 |
| - * @param Protocol-Output |
43 |
| - * @return Message |
44 |
| - */ |
45 |
| - public String getMessage(String input) { |
46 |
| - if (isPartOfProtocol(input)) { |
47 |
| - String message = removeStart(trimEnd(trimStart(input)), IDENTIFIER.length()); |
48 |
| - |
49 |
| - // remove block-character and fill replcements |
50 |
| - String mN = ""; |
51 |
| - for (int i = 1; i < message.length() - 1; i++) |
52 |
| - mN += message.charAt(i); |
53 |
| - message = mN.replace(START_REPLACEMENT, String.valueOf(BLOCK_START)).replace(END_REPLACEMENT, |
54 |
| - String.valueOf(BLOCK_END)); |
55 |
| - |
56 |
| - return message; |
57 |
| - } |
58 |
| - return null; |
59 |
| - } |
60 |
| - |
61 |
| - /** |
62 |
| - * @param Message that gets convertet into the protocol-format |
63 |
| - * @return Protocol-Output |
64 |
| - */ |
65 |
| - public String getProtocolOutput(String message) { |
66 |
| - return IDENTIFIER + BLOCK_START + message.replace(String.valueOf(BLOCK_START), START_REPLACEMENT) |
67 |
| - .replace(String.valueOf(BLOCK_END), END_REPLACEMENT) + BLOCK_END; |
68 |
| - } |
69 |
| - |
70 |
| - private static String trimStart(String sIn) { |
71 |
| - String s = ""; |
72 |
| - boolean hasBeenStart = false; |
73 |
| - for (int i = 0; i < sIn.length(); i++) { |
74 |
| - if (sIn.charAt(i) != ' ') |
75 |
| - hasBeenStart = true; |
76 |
| - if (hasBeenStart) |
77 |
| - s += sIn.charAt(i); |
78 |
| - } |
79 |
| - return s; |
80 |
| - } |
81 |
| - |
82 |
| - private static String removeStart(String in, int length) { |
83 |
| - String s = ""; |
84 |
| - for (int i = length; i < in.length(); i++) |
85 |
| - s += in.charAt(i); |
86 |
| - return s; |
87 |
| - } |
88 |
| - |
89 |
| - private static String trimEnd(String sIn) { |
90 |
| - String s = ""; |
91 |
| - boolean hasBeenEnd = false; |
92 |
| - for (int i = sIn.length() - 1; i >= 0; i--) { |
93 |
| - if (sIn.charAt(i) != ' ') |
94 |
| - hasBeenEnd = true; |
95 |
| - if (hasBeenEnd) |
96 |
| - s = sIn.charAt(i) + s; |
97 |
| - } |
98 |
| - return s; |
99 |
| - } |
100 |
| - |
101 |
| - private static int count(String s, char c) { |
102 |
| - int count = 0; |
103 |
| - for (int i = 0; i < s.length(); i++) { |
104 |
| - if (s.charAt(i) == c) |
105 |
| - count++; |
106 |
| - } |
107 |
| - return count; |
108 |
| - } |
109 |
| -} |
| 1 | +package com.paulsen.io; |
| 2 | + |
| 3 | +public class PCustomProtocol { |
| 4 | + |
| 5 | + private String IDENTIFIER = "CP"; |
| 6 | + |
| 7 | + private char BLOCK_START = '[', BLOCK_END = ']'; |
| 8 | + /** |
| 9 | + * Replacement if start- or end-character is included in data (1)=> store data |
| 10 | + * with replaced characters (2)=> read data with replacements back replaced to |
| 11 | + * start or end |
| 12 | + */ |
| 13 | + private String START_REPLACEMENT = "^<<^", END_REPLACEMENT = "^>>^"; |
| 14 | + |
| 15 | + public PCustomProtocol() { |
| 16 | + } |
| 17 | + |
| 18 | + public PCustomProtocol(String identifier, char blockStart, char blockEnd, String startReplacement, |
| 19 | + String endRemplacement) { |
| 20 | + IDENTIFIER = identifier; |
| 21 | + BLOCK_START = blockStart; |
| 22 | + BLOCK_END = blockEnd; |
| 23 | + START_REPLACEMENT = startReplacement; |
| 24 | + END_REPLACEMENT = endRemplacement; |
| 25 | + } |
| 26 | + |
| 27 | + public boolean isPartOfProtocol(String s) { |
| 28 | + s = trimEnd(trimStart(s)); |
| 29 | + // Message must start immediatley with IDENTIFIER and BLOCK_START |
| 30 | + if (s.startsWith(IDENTIFIER + BLOCK_START) && s.endsWith(String.valueOf(BLOCK_END))) { |
| 31 | + String message = removeStart(s, IDENTIFIER.length()); |
| 32 | + // Message can only contain BLOCK_START && BLOCK_END once |
| 33 | + if (count(message, BLOCK_START) != 1 || count(message, BLOCK_END) != 1) { |
| 34 | + return false; |
| 35 | + } |
| 36 | + return true; |
| 37 | + } |
| 38 | + return false; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param Protocol-Output |
| 43 | + * @return Message |
| 44 | + */ |
| 45 | + public String getMessage(String input) { |
| 46 | + if (isPartOfProtocol(input)) { |
| 47 | + String message = removeStart(trimEnd(trimStart(input)), IDENTIFIER.length()); |
| 48 | + |
| 49 | + // remove block-character and fill replcements |
| 50 | + String mN = ""; |
| 51 | + for (int i = 1; i < message.length() - 1; i++) |
| 52 | + mN += message.charAt(i); |
| 53 | + message = mN.replace(START_REPLACEMENT, String.valueOf(BLOCK_START)).replace(END_REPLACEMENT, |
| 54 | + String.valueOf(BLOCK_END)); |
| 55 | + |
| 56 | + return message; |
| 57 | + } |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param Message that gets convertet into the protocol-format |
| 63 | + * @return Protocol-Output |
| 64 | + */ |
| 65 | + public String getProtocolOutput(String message) { |
| 66 | + return IDENTIFIER + BLOCK_START + message.replace(String.valueOf(BLOCK_START), START_REPLACEMENT) |
| 67 | + .replace(String.valueOf(BLOCK_END), END_REPLACEMENT) + BLOCK_END; |
| 68 | + } |
| 69 | + |
| 70 | + private static String trimStart(String sIn) { |
| 71 | + String s = ""; |
| 72 | + boolean hasBeenStart = false; |
| 73 | + for (int i = 0; i < sIn.length(); i++) { |
| 74 | + if (sIn.charAt(i) != ' ') |
| 75 | + hasBeenStart = true; |
| 76 | + if (hasBeenStart) |
| 77 | + s += sIn.charAt(i); |
| 78 | + } |
| 79 | + return s; |
| 80 | + } |
| 81 | + |
| 82 | + private static String removeStart(String in, int length) { |
| 83 | + String s = ""; |
| 84 | + for (int i = length; i < in.length(); i++) |
| 85 | + s += in.charAt(i); |
| 86 | + return s; |
| 87 | + } |
| 88 | + |
| 89 | + private static String trimEnd(String sIn) { |
| 90 | + String s = ""; |
| 91 | + boolean hasBeenEnd = false; |
| 92 | + for (int i = sIn.length() - 1; i >= 0; i--) { |
| 93 | + if (sIn.charAt(i) != ' ') |
| 94 | + hasBeenEnd = true; |
| 95 | + if (hasBeenEnd) |
| 96 | + s = sIn.charAt(i) + s; |
| 97 | + } |
| 98 | + return s; |
| 99 | + } |
| 100 | + |
| 101 | + private static int count(String s, char c) { |
| 102 | + int count = 0; |
| 103 | + for (int i = 0; i < s.length(); i++) { |
| 104 | + if (s.charAt(i) == c) |
| 105 | + count++; |
| 106 | + } |
| 107 | + return count; |
| 108 | + } |
| 109 | +} |
0 commit comments