Merge commit 'refs/merge-requests/52' of gitorious.org:f-droid/fdroidclient

This commit is contained in:
Daniel Martí 2014-01-29 23:29:27 +01:00
commit c8016e9af2

View File

@ -22,13 +22,7 @@ import android.content.Context;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Locale;
@ -117,37 +111,30 @@ public final class Utils {
public static int countSubstringOccurrence(File file, String substring) throws IOException {
int count = 0;
BufferedReader reader = null;
FileReader input = null;
try {
int currentSubstringIndex = 0;
char[] buffer = new char[4096];
reader = new BufferedReader(new FileReader(file));
while(true) {
String line = reader.readLine();
if (line == null) {
break;
}
count += countSubstringOccurrence(line, substring);
}
input = new FileReader(file);
int numRead = input.read(buffer);
while(numRead != -1) {
} finally {
closeQuietly(reader);
}
return count;
}
/**
* Thanks to http://stackoverflow.com/a/767910
*/
public static int countSubstringOccurrence(String toSearch, String substring) {
int count = 0;
int index = 0;
while (true) {
index = toSearch.indexOf(substring, index);
if (index == -1){
break;
}
for (char c : buffer) {
if (c == substring.charAt(currentSubstringIndex)) {
currentSubstringIndex ++;
if (currentSubstringIndex == substring.length()) {
count ++;
index += substring.length();
currentSubstringIndex = 0;
}
} else {
currentSubstringIndex = 0;
}
}
numRead = input.read(buffer);
}
} finally {
closeQuietly(input);
}
return count;
}