@@ -41,41 +41,91 @@ public class GuacamoleInstruction {
41
41
private final List <String > args ;
42
42
43
43
/**
44
- * The cached result of converting this GuacamoleInstruction to the format
45
- * used by the Guacamole protocol.
44
+ * The cached String result of converting this GuacamoleInstruction to the
45
+ * format used by the Guacamole protocol.
46
+ *
47
+ * @see #toString()
46
48
*/
47
- private String protocolForm = null ;
49
+ private String rawString = null ;
48
50
49
51
/**
50
- * Creates a new GuacamoleInstruction having the given Operation and
51
- * list of arguments values .
52
+ * The cached char[] result of converting this GuacamoleInstruction to the
53
+ * format used by the Guacamole protocol .
52
54
*
53
- * @param opcode The opcode of the instruction to create.
54
- * @param args The list of argument values to provide in the new
55
- * instruction if any.
55
+ * @see #toCharArray()
56
+ */
57
+ private char [] rawChars = null ;
58
+
59
+ /**
60
+ * Creates a new GuacamoleInstruction having the given opcode and list of
61
+ * argument values.
62
+ *
63
+ * @param opcode
64
+ * The opcode of the instruction to create.
65
+ *
66
+ * @param args
67
+ * The list of argument values to provide in the new instruction, if
68
+ * any.
56
69
*/
57
70
public GuacamoleInstruction (String opcode , String ... args ) {
58
71
this .opcode = opcode ;
59
72
this .args = Collections .unmodifiableList (Arrays .asList (args ));
60
73
}
61
74
62
75
/**
63
- * Creates a new GuacamoleInstruction having the given Operation and
64
- * list of arguments values. The list given will be used to back the
65
- * internal list of arguments and the list returned by getArgs().
76
+ * Creates a new GuacamoleInstruction having the given opcode and list of
77
+ * argument values. The list given will be used to back the internal list of
78
+ * arguments and the list returned by {@link #getArgs()}.
79
+ * <p>
80
+ * The provided argument list may not be modified in any way after being
81
+ * provided to this constructor. Doing otherwise will result in undefined
82
+ * behavior.
83
+ *
84
+ * @param opcode
85
+ * The opcode of the instruction to create.
66
86
*
67
- * @param opcode The opcode of the instruction to create.
68
- * @param args The list of argument values to provide in the new
69
- * instruction if any.
87
+ * @param args
88
+ * The list of argument values to provide in the new instruction, if
89
+ * any.
70
90
*/
71
91
public GuacamoleInstruction (String opcode , List <String > args ) {
72
92
this .opcode = opcode ;
73
93
this .args = Collections .unmodifiableList (args );
74
94
}
75
95
96
+ /**
97
+ * Creates a new GuacamoleInstruction having the given opcode, list of
98
+ * argument values, and underlying protocol representation. The list given
99
+ * will be used to back the internal list of arguments and the list
100
+ * returned by {@link #getArgs()}. The provided protocol representation
101
+ * will be used to back the internal protocol representation and values
102
+ * returned by {@link #toCharArray()} and {@link #toString()}.
103
+ * <p>
104
+ * Neither the provided argument list nor the provided protocol
105
+ * representation may be modified in any way after being provided to this
106
+ * constructor. Doing otherwise will result in undefined behavior.
107
+ *
108
+ * @param opcode
109
+ * The opcode of the instruction to create.
110
+ *
111
+ * @param args
112
+ * The list of argument values to provide in the new instruction, if
113
+ * any.
114
+ *
115
+ * @param raw
116
+ * The underlying representation of this instruction as would be sent
117
+ * over the network via the Guacamole protocol.
118
+ */
119
+ public GuacamoleInstruction (String opcode , List <String > args , char [] raw ) {
120
+ this (opcode , args );
121
+ this .rawChars = raw ;
122
+ }
123
+
76
124
/**
77
125
* Returns the opcode associated with this GuacamoleInstruction.
78
- * @return The opcode associated with this GuacamoleInstruction.
126
+ *
127
+ * @return
128
+ * The opcode associated with this GuacamoleInstruction.
79
129
*/
80
130
public String getOpcode () {
81
131
return opcode ;
@@ -86,8 +136,9 @@ public String getOpcode() {
86
136
* GuacamoleInstruction. Note that the List returned is immutable.
87
137
* Attempts to modify the list will result in exceptions.
88
138
*
89
- * @return A List of all argument values specified for this
90
- * GuacamoleInstruction.
139
+ * @return
140
+ * An unmodifiable List of all argument values specified for this
141
+ * GuacamoleInstruction.
91
142
*/
92
143
public List <String > getArgs () {
93
144
return args ;
@@ -122,28 +173,58 @@ public String toString() {
122
173
123
174
// Avoid rebuilding Guacamole protocol form of instruction if already
124
175
// known
125
- if (protocolForm == null ) {
176
+ if (rawString == null ) {
126
177
127
- StringBuilder buff = new StringBuilder ();
178
+ // Prefer to construct String from existing char array, rather than
179
+ // reconstruct protocol from scratch
180
+ if (rawChars != null )
181
+ rawString = new String (rawChars );
128
182
129
- // Write opcode
130
- appendElement ( buff , opcode );
183
+ // Reconstruct protocol details only if truly necessary
184
+ else {
131
185
132
- // Write argument values
133
- for (String value : args ) {
134
- buff .append (',' );
135
- appendElement (buff , value );
136
- }
186
+ StringBuilder buff = new StringBuilder ();
137
187
138
- // Write terminator
139
- buff . append ( ';' );
188
+ // Write opcode
189
+ appendElement ( buff , opcode );
140
190
141
- // Cache result for future calls
142
- protocolForm = buff .toString ();
191
+ // Write argument values
192
+ for (String value : args ) {
193
+ buff .append (',' );
194
+ appendElement (buff , value );
195
+ }
196
+
197
+ // Write terminator
198
+ buff .append (';' );
199
+
200
+ // Cache result for future calls
201
+ rawString = buff .toString ();
202
+
203
+ }
143
204
144
205
}
145
206
146
- return protocolForm ;
207
+ return rawString ;
208
+
209
+ }
210
+
211
+ /**
212
+ * Returns this GuacamoleInstruction in the form it would be sent over the
213
+ * Guacamole protocol. The returned char[] MUST NOT be modified. If the
214
+ * returned char[] is modified, the results of doing so are undefined.
215
+ *
216
+ * @return
217
+ * This GuacamoleInstruction in the form it would be sent over the
218
+ * Guacamole protocol. The returned char[] MUST NOT be modified.
219
+ */
220
+ public char [] toCharArray () {
221
+
222
+ // Avoid rebuilding Guacamole protocol form of instruction if already
223
+ // known
224
+ if (rawChars == null )
225
+ rawChars = toString ().toCharArray ();
226
+
227
+ return rawChars ;
147
228
148
229
}
149
230
0 commit comments