18
18
import java .security .InvalidParameterException ;
19
19
20
20
/**
21
- * This class implements string data request for use with
22
- * {@link com.mouseviator.fsuipc.FSUIPC} class. Well, FSUIPC will read/write
23
- * string values as series of bytes, usually terminated as classic C string, by
24
- * 0 byte. But with Strings we always have this problem with encoding... so to
25
- * keep the scalability, this data request allows you to set encoding that will
26
- * be used to convert the internal byte data buffer holding the string to the
27
- * actual Java string. You can set this encoding using various constructors or
28
- * by the {@link #setCharset(java.nio.charset.Charset) } method. The charset for
29
- * encoding/decoding the string will be initialized to the system default,
30
- * which, mostly, will be UTF-8.
21
+ * This class implements string data request for use with {@link com.mouseviator.fsuipc.FSUIPC} class. Well, FSUIPC will
22
+ * read/write string values as series of bytes, usually terminated as classic C string, by 0 byte. But with Strings we
23
+ * always have this problem with encoding... so to keep the scalability, this data request allows you to set encoding
24
+ * that will be used to convert the internal byte data buffer holding the string to the actual Java string. You can set
25
+ * this encoding using various constructors or by the {@link #setCharset(java.nio.charset.Charset) } method. The charset
26
+ * for encoding/decoding the string will be initialized to the system default, which, mostly, will be UTF-8.
31
27
*
32
28
*
33
29
* @author Mouseviator
34
30
*/
35
31
public class StringRequest extends DataRequest implements IDataRequest <String > {
36
32
37
33
private Charset charset = Charset .defaultCharset ();
34
+ private ValueRetrieveMethod valueRetrieveMethod = ValueRetrieveMethod .WHOLE_BUFFER ;
38
35
39
36
/**
40
- * Creates a new string data request associated with given offset and the
41
- * byte data buffer initialized to given size. Note that this is only useful
42
- * for READ type request. If you use the {@link #setValue(java.lang.String)
43
- * } method later on this object, the byte array created by this constructor
44
- * will be discarded.
37
+ * Creates a new string data request associated with given offset and the byte data buffer initialized to given
38
+ * size. Note that this is only useful for READ type request. If you use the {@link #setValue(java.lang.String)
39
+ * } method later on this object, the byte array created by this constructor will be discarded.
45
40
*
46
41
* @param offset An offset to associate this data request with.
47
- * @param size The size of the byte data buffer to hold the string
48
- * characters.
42
+ * @param size The size of the byte data buffer to hold the string characters.
49
43
*/
50
44
public StringRequest (int offset , int size ) throws InvalidParameterException {
51
45
if (offset >= MIN_OFFSET_VALUE && offset <= MAX_OFFSET_VALUE && size > 0 ) {
@@ -57,10 +51,9 @@ public StringRequest(int offset, int size) throws InvalidParameterException {
57
51
}
58
52
59
53
/**
60
- * Creates a new string data request associated with given offset and
61
- * initialized to given string value. The conversion of the string to byte
62
- * data buffer will happen using the system default charset. The data
63
- * request type will be set to WRITE.
54
+ * Creates a new string data request associated with given offset and initialized to given string value. The
55
+ * conversion of the string to byte data buffer will happen using the system default charset. The data request type
56
+ * will be set to WRITE.
64
57
*
65
58
* @param offset An offset to associate this data request with.
66
59
* @param value A string to set for this data request value.
@@ -77,13 +70,10 @@ public StringRequest(int offset, String value) throws InvalidParameterException
77
70
}
78
71
79
72
/**
80
- * Creates a new string data request associated with given offset and
81
- * initialized to given string value. If the byte data buffer, that will be
82
- * result of converting given string value to byte array, is longer than
83
- * max_size, it will be trimmed to the (max_size - 1) - the last byte will
84
- * be set to 0. The conversion of the string to byte data buffer will happen
85
- * using the system default charset. The data request type will be set to
86
- * WRITE.
73
+ * Creates a new string data request associated with given offset and initialized to given string value. If the byte
74
+ * data buffer, that will be result of converting given string value to byte array, is longer than max_size, it will
75
+ * be trimmed to the (max_size - 1) - the last byte will be set to 0. The conversion of the string to byte data
76
+ * buffer will happen using the system default charset. The data request type will be set to WRITE.
87
77
*
88
78
* @param offset An offset to associate this data request with.
89
79
* @param max_size Maximum size of the resulting byte data buffer.
@@ -102,10 +92,9 @@ public StringRequest(int offset, int max_size, String value) throws InvalidParam
102
92
}
103
93
104
94
/**
105
- * Creates a new string data request associated with given offset and
106
- * initialized to given string value.The conversion of the string to byte
107
- * data buffer will happen using the provided charset. The data request type
108
- * will be set to WRITE.
95
+ * Creates a new string data request associated with given offset and initialized to given string value.The
96
+ * conversion of the string to byte data buffer will happen using the provided charset. The data request type will
97
+ * be set to WRITE.
109
98
*
110
99
* @param offset An offset to associate this data request with.
111
100
* @param value A string to set for this data request value.
@@ -115,7 +104,7 @@ public StringRequest(int offset, String value, Charset charset) throws InvalidPa
115
104
if (offset >= MIN_OFFSET_VALUE && offset <= MAX_OFFSET_VALUE ) {
116
105
//convert to internal byte array
117
106
convertStringToByteArray (value , 0 , charset );
118
-
107
+
119
108
this .charset = charset ;
120
109
this .offset = offset ;
121
110
this .type = RequestType .WRITE ;
@@ -125,12 +114,10 @@ public StringRequest(int offset, String value, Charset charset) throws InvalidPa
125
114
}
126
115
127
116
/**
128
- * Creates a new string data request associated with given offset and
129
- * initialized to given string value.If the byte data buffer, that will be
130
- * result of converting given string value to byte array, is longer than
131
- * max_size, it will be trimmed to the (max_size - 1) - the last byte will
132
- * be set to 0. The conversion of the string to byte data buffer will happen
133
- * using the provided charset. The data request type will be set to WRITE.
117
+ * Creates a new string data request associated with given offset and initialized to given string value.If the byte
118
+ * data buffer, that will be result of converting given string value to byte array, is longer than max_size, it will
119
+ * be trimmed to the (max_size - 1) - the last byte will be set to 0. The conversion of the string to byte data
120
+ * buffer will happen using the provided charset. The data request type will be set to WRITE.
134
121
*
135
122
* @param offset An offset to associate this data request with.
136
123
* @param max_size Maximum size of the resulting byte data buffer.
@@ -151,9 +138,8 @@ public StringRequest(int offset, int max_size, String value, Charset charset) th
151
138
}
152
139
153
140
/**
154
- * This function will allocate the internat byte data buffer to specified
155
- * size, creating new buffer, thus, throwing away any data that this data
156
- * request held before.
141
+ * This function will allocate the internat byte data buffer to specified size, creating new buffer, thus, throwing
142
+ * away any data that this data request held before.
157
143
*
158
144
* @param size The required size of the byte data buffer.
159
145
*/
@@ -164,19 +150,40 @@ public void allocate(int size) {
164
150
}
165
151
166
152
@ Override
153
+ /**
154
+ * This function will return underlying byte data buffer as String. How the value will be retrieved depends on what value is set
155
+ * {@link #getValueRetrieveMethod()}.
156
+ *
157
+ * @return String representation of the underlying byte data buffer.
158
+ */
167
159
public String getValue () {
168
- return new String (dataBuffer , charset ).trim ();
160
+ return getValue (valueRetrieveMethod );
161
+ }
162
+
163
+ /**
164
+ * This function will return underlying byte data buffer as String. You have to specify how the value will be retrieved
165
+ * by <code>valueRetrieveMethod</code> parameter.
166
+ *
167
+ * @param valueRetrieveMethod The method to retrieve the value.
168
+ * @return String representation of the underlying byte data buffer.
169
+ */
170
+ public String getValue (ValueRetrieveMethod valueRetrieveMethod ) {
171
+ if (valueRetrieveMethod == ValueRetrieveMethod .WHOLE_BUFFER ) {
172
+ return new String (dataBuffer , charset ).trim ();
173
+ } else {
174
+ return getZeroTerminatedString (charset );
175
+ }
169
176
}
170
177
171
178
@ Override
172
179
public void setValue (String value ) {
173
180
convertStringToByteArray (value , 0 , charset );
174
181
}
175
-
182
+
176
183
/**
177
- * This function will set value of this StringRequest to given string, but the resulting length
178
- * of the byte array of the converted string will be max_size.
179
- *
184
+ * This function will set value of this StringRequest to given string, but the resulting length of the byte array of
185
+ * the converted string will be max_size.
186
+ *
180
187
* @param value A string value to set.
181
188
* @param max_size The maximum length of the byte buffer of the converted string.
182
189
*/
@@ -195,14 +202,46 @@ public void setCharset(Charset charset) {
195
202
this .charset = charset ;
196
203
}
197
204
}
198
-
205
+
199
206
/**
200
207
* Return the charset that the {@link #getValue() } and {@link #setValue(java.lang.String)
201
208
* } functions will use to convert to/from string/byte data buffer.
202
- *
209
+ *
203
210
* @return The charset.
204
211
*/
205
212
public Charset getCharset () {
206
213
return this .charset ;
207
214
}
215
+
216
+ /**
217
+ * Return the value retrieve method used by this String data request.
218
+ *
219
+ * @return Value retrieve method.
220
+ */
221
+ public ValueRetrieveMethod getValueRetrieveMethod () {
222
+ return valueRetrieveMethod ;
223
+ }
224
+
225
+ /**
226
+ * Sets the value retrieve method for this String data request.
227
+ *
228
+ * @param valueRetrieveMethod Value retrieve method.
229
+ */
230
+ public void setValueRetrieveMethod (ValueRetrieveMethod valueRetrieveMethod ) {
231
+ this .valueRetrieveMethod = valueRetrieveMethod ;
232
+ }
233
+
234
+ /**
235
+ * This enumeration defines values retrieve method that will be used by {@link #getValue() } method.
236
+ */
237
+ public enum ValueRetrieveMethod {
238
+ /**
239
+ * Retrieve the content of whole underlying byte buffer when converting it to String.
240
+ */
241
+ WHOLE_BUFFER ,
242
+ /**
243
+ * Retrieve the content of underlying byte buffer up to first zero byte when converting it to String.
244
+ */
245
+ TO_FIRST_ZERO_BYTE
246
+ }
208
247
}
0 commit comments