Skip to content

Commit 30607d4

Browse files
committed
Completed javadoc for PersistentCookieStore
1 parent ea227cb commit 30607d4

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

library/src/main/java/com/loopj/android/http/PersistentCookieStore.java

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import android.content.Context;
2222
import android.content.SharedPreferences;
2323
import android.text.TextUtils;
24+
import android.util.Log;
2425

2526
import org.apache.http.client.CookieStore;
2627
import org.apache.http.cookie.Cookie;
@@ -42,6 +43,7 @@
4243
* regular old apache HttpClient/HttpContext if you prefer.
4344
*/
4445
public class PersistentCookieStore implements CookieStore {
46+
private static final String LOG_TAG = "PersistentCookieStore";
4547
private static final String COOKIE_PREFS = "CookiePrefsFile";
4648
private static final String COOKIE_NAME_STORE = "names";
4749
private static final String COOKIE_NAME_PREFIX = "cookie_";
@@ -143,12 +145,15 @@ public List<Cookie> getCookies() {
143145
return new ArrayList<Cookie>(cookies.values());
144146
}
145147

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+
*/
151154
protected String encodeCookie(SerializableCookie cookie) {
155+
if (cookie == null)
156+
return null;
152157
ByteArrayOutputStream os = new ByteArrayOutputStream();
153158
try {
154159
ObjectOutputStream outputStream = new ObjectOutputStream(os);
@@ -160,27 +165,36 @@ protected String encodeCookie(SerializableCookie cookie) {
160165
return byteArrayToHexString(os.toByteArray());
161166
}
162167

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);
166177
Cookie cookie = null;
167178
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);
172183
}
173184

174185
return cookie;
175186
}
176187

177188
/**
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 &lt;-&gt; 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
180194
*/
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) {
184198
int v = element & 0xff;
185199
if (v < 16) {
186200
sb.append('0');
@@ -190,11 +204,17 @@ protected String byteArrayToHexString(byte[] b) {
190204
return sb.toString().toUpperCase();
191205
}
192206

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();
195215
byte[] data = new byte[len / 2];
196216
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));
198218
}
199219
return data;
200220
}

0 commit comments

Comments
 (0)