1818import  java .security .InvalidParameterException ;
1919
2020/** 
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. 
3127 * 
3228 * 
3329 * @author Mouseviator 
3430 */ 
3531public  class  StringRequest  extends  DataRequest  implements  IDataRequest <String > {
3632
3733    private  Charset  charset  = Charset .defaultCharset ();
34+     private  ValueRetrieveMethod  valueRetrieveMethod  = ValueRetrieveMethod .WHOLE_BUFFER ;
3835
3936    /** 
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. 
4540     * 
4641     * @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. 
4943     */ 
5044    public  StringRequest (int  offset , int  size ) throws  InvalidParameterException  {
5145        if  (offset  >= MIN_OFFSET_VALUE  && offset  <= MAX_OFFSET_VALUE  && size  > 0 ) {
@@ -57,10 +51,9 @@ public StringRequest(int offset, int size) throws InvalidParameterException {
5751    }
5852
5953    /** 
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. 
6457     * 
6558     * @param offset An offset to associate this data request with. 
6659     * @param value A string to set for this data request value. 
@@ -77,13 +70,10 @@ public StringRequest(int offset, String value) throws InvalidParameterException
7770    }
7871
7972    /** 
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. 
8777     * 
8878     * @param offset An offset to associate this data request with. 
8979     * @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
10292    }
10393
10494    /** 
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. 
10998     * 
11099     * @param offset An offset to associate this data request with. 
111100     * @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
115104        if  (offset  >= MIN_OFFSET_VALUE  && offset  <= MAX_OFFSET_VALUE ) {
116105            //convert to internal byte array 
117106            convertStringToByteArray (value , 0 , charset );
118-              
107+ 
119108            this .charset  = charset ;
120109            this .offset  = offset ;
121110            this .type  = RequestType .WRITE ;
@@ -125,12 +114,10 @@ public StringRequest(int offset, String value, Charset charset) throws InvalidPa
125114    }
126115
127116    /** 
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. 
134121     * 
135122     * @param offset An offset to associate this data request with. 
136123     * @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
151138    }
152139
153140    /** 
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. 
157143     * 
158144     * @param size The required size of the byte data buffer. 
159145     */ 
@@ -164,19 +150,40 @@ public void allocate(int size) {
164150    }
165151
166152    @ 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+      */ 
167159    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+         }
169176    }
170177
171178    @ Override 
172179    public  void  setValue (String  value ) {
173180        convertStringToByteArray (value , 0 , charset );
174181    }
175-      
182+ 
176183    /** 
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+      * 
180187     * @param value A string value to set. 
181188     * @param max_size The maximum length of the byte buffer of the converted string. 
182189     */ 
@@ -195,14 +202,46 @@ public void setCharset(Charset charset) {
195202            this .charset  = charset ;
196203        }
197204    }
198-      
205+ 
199206    /** 
200207     * Return the charset that the {@link #getValue() } and {@link #setValue(java.lang.String) 
201208     * } functions will use to convert to/from string/byte data buffer. 
202-      *   
209+      * 
203210     * @return The charset. 
204211     */ 
205212    public  Charset  getCharset () {
206213        return  this .charset ;
207214    }
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+     }
208247}
0 commit comments