aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/cgBase.java
diff options
context:
space:
mode:
authorBananeweizen <Bananeweizen@gmx.de>2011-08-21 03:29:53 -0700
committerBananeweizen <Bananeweizen@gmx.de>2011-08-21 03:29:53 -0700
commitc6517f17006066361acfc6fb2b9473bf8a5de798 (patch)
tree4fe346d406865022f4094b19c615e4a7edcabb94 /src/cgeo/geocaching/cgBase.java
parentaa5686edf4d29077170293ba7e1150df73837b27 (diff)
parentd6ceb750b58bed8355ce568d91730ae32e3f8ee0 (diff)
downloadcgeo-c6517f17006066361acfc6fb2b9473bf8a5de798.zip
cgeo-c6517f17006066361acfc6fb2b9473bf8a5de798.tar.gz
cgeo-c6517f17006066361acfc6fb2b9473bf8a5de798.tar.bz2
Merge pull request #255 from MichielK/master
Refactoring: remove dup. code, use enhanced for-each loop & use Integer.valueOf
Diffstat (limited to 'src/cgeo/geocaching/cgBase.java')
-rw-r--r--src/cgeo/geocaching/cgBase.java175
1 files changed, 55 insertions, 120 deletions
diff --git a/src/cgeo/geocaching/cgBase.java b/src/cgeo/geocaching/cgBase.java
index ab17c04..b12366f 100644
--- a/src/cgeo/geocaching/cgBase.java
+++ b/src/cgeo/geocaching/cgBase.java
@@ -869,7 +869,7 @@ public class cgBase {
while (matcherTotalCnt.find()) {
if (matcherTotalCnt.groupCount() > 0) {
if (matcherTotalCnt.group(1) != null) {
- caches.totalCnt = new Integer(matcherTotalCnt.group(1));
+ caches.totalCnt = Integer.valueOf(matcherTotalCnt.group(1));
}
}
}
@@ -2581,12 +2581,12 @@ public class cgBase {
continue;
}
if (trackableMatcher.group(3) != null) {
- trackable.ctl = new Integer(trackableMatcher.group(3));
+ trackable.ctl = Integer.valueOf(trackableMatcher.group(3));
} else {
continue;
}
if (trackableMatcher.group(5) != null) {
- trackable.id = new Integer(trackableMatcher.group(5));
+ trackable.id = Integer.valueOf(trackableMatcher.group(5));
} else {
continue;
}
@@ -4289,63 +4289,7 @@ public class cgBase {
scheme = "https://";
}
- // prepare cookies
- String cookiesDone = null;
- if (cookies == null || cookies.isEmpty()) {
- if (cookies == null) {
- cookies = new HashMap<String, String>();
- }
-
- final Map<String, ?> prefsAll = prefs.getAll();
- final Set<? extends Map.Entry<String, ?>> entrySet = prefsAll.entrySet();
-
- for(Map.Entry<String, ?> entry : entrySet){
- String key = entry.getKey();
- if (key.matches("cookie_.+")) {
- final String cookieKey = key.substring(7);
- final String cookieValue = (String) entry.getValue();
-
- cookies.put(cookieKey, cookieValue);
- }
- }
- }
-
- if (cookies != null) {
- final Set<Map.Entry<String, String>> entrySet = cookies.entrySet();
- final ArrayList<String> cookiesEncoded = new ArrayList<String>();
-
- for(Map.Entry<String, String> entry : entrySet){
- cookiesEncoded.add(entry.getKey() + "=" + entry.getValue());
- }
-
- if (cookiesEncoded.size() > 0) {
- cookiesDone = implode("; ", cookiesEncoded.toArray());
- }
- }
-
- if (cookiesDone == null) {
- Map<String, ?> prefsValues = prefs.getAll();
-
- if (prefsValues != null && prefsValues.size() > 0 && prefsValues.keySet().size() > 0) {
- final Set<? extends Map.Entry<String, ?>> entrySet = prefsValues.entrySet();
- final ArrayList<String> cookiesEncoded = new ArrayList<String>();
-
- for(Map.Entry<String, ?> entry : entrySet){
- String key = entry.getKey();
- if (key.length() > 7 && key.substring(0, 7).equals("cookie_")) {
- cookiesEncoded.add(key + "=" + entry.getValue());
- }
- }
-
- if (cookiesEncoded.size() > 0) {
- cookiesDone = implode("; ", cookiesEncoded.toArray());
- }
- }
- }
-
- if (cookiesDone == null) {
- cookiesDone = "";
- }
+ String cookiesDone = getCookiesAsString();
URLConnection uc = null;
HttpURLConnection connection = null;
@@ -4447,16 +4391,7 @@ public class cgBase {
}
prefsEditor.commit();
- final String encoding = connection.getContentEncoding();
- InputStream ins;
-
- if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
- ins = new GZIPInputStream(connection.getInputStream());
- } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
- ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
- } else {
- ins = connection.getInputStream();
- }
+ InputStream ins = getInputstreamFromConnection(connection);
final InputStreamReader inr = new InputStreamReader(ins);
final BufferedReader br = new BufferedReader(inr, 16 * 1024);
@@ -4525,32 +4460,7 @@ public class cgBase {
return response;
}
- private static void replaceWhitespace(final StringBuffer buffer) {
- final int length = buffer.length();
- final char[] chars = new char[length];
- buffer.getChars(0, length, chars, 0);
- int resultSize = 0;
- boolean lastWasWhitespace = false;
- for (int i = 0; i < length; i++) {
- char c = chars[i];
- if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
- if (!lastWasWhitespace) {
- chars[resultSize++] =' ';
- }
- lastWasWhitespace = true;
- } else {
- chars[resultSize++] = c;
- lastWasWhitespace = false;
- }
- }
- buffer.setLength(0);
- buffer.append(chars);
- }
-
- public String requestJSONgc(String host, String path, String params) {
- int httpCode = -1;
- String httpLocation = null;
-
+ private String getCookiesAsString() {
// prepare cookies
String cookiesDone = null;
if (cookies == null || cookies.isEmpty()) {
@@ -4608,6 +4518,35 @@ public class cgBase {
if (cookiesDone == null) {
cookiesDone = "";
}
+ return cookiesDone;
+ }
+
+ private static void replaceWhitespace(final StringBuffer buffer) {
+ final int length = buffer.length();
+ final char[] chars = new char[length];
+ buffer.getChars(0, length, chars, 0);
+ int resultSize = 0;
+ boolean lastWasWhitespace = false;
+ for (char c : chars) {
+ if (c == ' ' || c == '\n' || c == '\r' || c == '\t') {
+ if (!lastWasWhitespace) {
+ chars[resultSize++] =' ';
+ }
+ lastWasWhitespace = true;
+ } else {
+ chars[resultSize++] = c;
+ lastWasWhitespace = false;
+ }
+ }
+ buffer.setLength(0);
+ buffer.append(chars);
+ }
+
+ public String requestJSONgc(String host, String path, String params) {
+ int httpCode = -1;
+ String httpLocation = null;
+
+ String cookiesDone = getCookiesAsString();
URLConnection uc = null;
HttpURLConnection connection = null;
@@ -4679,16 +4618,7 @@ public class cgBase {
}
prefsEditor.commit();
- final String encoding = connection.getContentEncoding();
- InputStream ins;
-
- if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
- ins = new GZIPInputStream(connection.getInputStream());
- } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
- ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
- } else {
- ins = connection.getInputStream();
- }
+ InputStream ins = getInputstreamFromConnection(connection);
final InputStreamReader inr = new InputStreamReader(ins);
final BufferedReader br = new BufferedReader(inr);
@@ -4735,6 +4665,20 @@ public class cgBase {
}
}
+ private static InputStream getInputstreamFromConnection(HttpURLConnection connection) throws IOException {
+ final String encoding = connection.getContentEncoding();
+ InputStream ins;
+
+ if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
+ ins = new GZIPInputStream(connection.getInputStream());
+ } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
+ ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
+ } else {
+ ins = connection.getInputStream();
+ }
+ return ins;
+ }
+
public static String requestJSON(String host, String path, String params) {
return requestJSON("http://", host, path, "GET", params);
}
@@ -4814,16 +4758,7 @@ public class cgBase {
}
- final String encoding = connection.getContentEncoding();
- InputStream ins;
-
- if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
- ins = new GZIPInputStream(connection.getInputStream());
- } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) {
- ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true));
- } else {
- ins = connection.getInputStream();
- }
+ InputStream ins = getInputstreamFromConnection(connection);
final InputStreamReader inr = new InputStreamReader(ins);
final BufferedReader br = new BufferedReader(inr, 1024);
@@ -4949,11 +4884,11 @@ public class cgBase {
if (path.exists()) {
File[] files = path.listFiles();
- for (int i = 0; i < files.length; i++) {
- if (files[i].isDirectory()) {
- deleteDirectory(files[i]);
+ for (File file : files) {
+ if (file.isDirectory()) {
+ deleteDirectory(file);
} else {
- files[i].delete();
+ file.delete();
}
}
}