21
21
import android .content .Context ;
22
22
import android .content .SharedPreferences ;
23
23
import android .text .TextUtils ;
24
+ import android .util .Log ;
24
25
25
26
import org .apache .http .client .CookieStore ;
26
27
import org .apache .http .cookie .Cookie ;
42
43
* regular old apache HttpClient/HttpContext if you prefer.
43
44
*/
44
45
public class PersistentCookieStore implements CookieStore {
46
+ private static final String LOG_TAG = "PersistentCookieStore" ;
45
47
private static final String COOKIE_PREFS = "CookiePrefsFile" ;
46
48
private static final String COOKIE_NAME_STORE = "names" ;
47
49
private static final String COOKIE_NAME_PREFIX = "cookie_" ;
@@ -143,12 +145,15 @@ public List<Cookie> getCookies() {
143
145
return new ArrayList <Cookie >(cookies .values ());
144
146
}
145
147
146
-
147
- //
148
- // Cookie serialization/deserialization
149
- //
150
-
148
+ /**
149
+ * Serializes Cookie object into String
150
+ *
151
+ * @param cookie cookie to be encoded, can be null
152
+ * @return cookie encoded as String
153
+ */
151
154
protected String encodeCookie (SerializableCookie cookie ) {
155
+ if (cookie == null )
156
+ return null ;
152
157
ByteArrayOutputStream os = new ByteArrayOutputStream ();
153
158
try {
154
159
ObjectOutputStream outputStream = new ObjectOutputStream (os );
@@ -160,27 +165,36 @@ protected String encodeCookie(SerializableCookie cookie) {
160
165
return byteArrayToHexString (os .toByteArray ());
161
166
}
162
167
163
- protected Cookie decodeCookie (String cookieStr ) {
164
- byte [] bytes = hexStringToByteArray (cookieStr );
165
- ByteArrayInputStream is = new ByteArrayInputStream (bytes );
168
+ /**
169
+ * Returns cookie decoded from cookie string
170
+ *
171
+ * @param cookieString string of cookie as returned from http request
172
+ * @return decoded cookie or null if exception occured
173
+ */
174
+ protected Cookie decodeCookie (String cookieString ) {
175
+ byte [] bytes = hexStringToByteArray (cookieString );
176
+ ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream (bytes );
166
177
Cookie cookie = null ;
167
178
try {
168
- ObjectInputStream ois = new ObjectInputStream (is );
169
- cookie = ((SerializableCookie ) ois .readObject ()).getCookie ();
170
- } catch (Exception e ) {
171
- e . printStackTrace ( );
179
+ ObjectInputStream objectInputStream = new ObjectInputStream (byteArrayInputStream );
180
+ cookie = ((SerializableCookie ) objectInputStream .readObject ()).getCookie ();
181
+ } catch (Exception exception ) {
182
+ Log . d ( LOG_TAG , "decodeCookie failed" , exception );
172
183
}
173
184
174
185
return cookie ;
175
186
}
176
187
177
188
/**
178
- * Using some super basic byte array <-> hex conversions so we don't have to rely on any large
179
- * Base64 libraries. Can be overridden if you like!
189
+ * Using some super basic byte array <-> hex conversions so we don't have to rely on any
190
+ * large Base64 libraries. Can be overridden if you like!
191
+ *
192
+ * @param bytes byte array to be converted
193
+ * @return string containing hex values
180
194
*/
181
- protected String byteArrayToHexString (byte [] b ) {
182
- StringBuilder sb = new StringBuilder (b .length * 2 );
183
- for (byte element : b ) {
195
+ protected String byteArrayToHexString (byte [] bytes ) {
196
+ StringBuilder sb = new StringBuilder (bytes .length * 2 );
197
+ for (byte element : bytes ) {
184
198
int v = element & 0xff ;
185
199
if (v < 16 ) {
186
200
sb .append ('0' );
@@ -190,11 +204,17 @@ protected String byteArrayToHexString(byte[] b) {
190
204
return sb .toString ().toUpperCase ();
191
205
}
192
206
193
- protected byte [] hexStringToByteArray (String s ) {
194
- int len = s .length ();
207
+ /**
208
+ * Converts hex values from strings to byte arra
209
+ *
210
+ * @param hexString string of hex-encoded values
211
+ * @return decoded byte array
212
+ */
213
+ protected byte [] hexStringToByteArray (String hexString ) {
214
+ int len = hexString .length ();
195
215
byte [] data = new byte [len / 2 ];
196
216
for (int i = 0 ; i < len ; i += 2 ) {
197
- data [i / 2 ] = (byte ) ((Character .digit (s .charAt (i ), 16 ) << 4 ) + Character .digit (s .charAt (i + 1 ), 16 ));
217
+ data [i / 2 ] = (byte ) ((Character .digit (hexString .charAt (i ), 16 ) << 4 ) + Character .digit (hexString .charAt (i + 1 ), 16 ));
198
218
}
199
219
return data ;
200
220
}
0 commit comments