mirror of
https://github.com/f-droid/fdroidclient.git
synced 2026-01-31 02:01:35 -05:00
I moved the Utils.hasApi to the protected method in Compatibility. It was misleading having it in Utils, because it is not as simple as putting a "if(Utils.hasApi(11)) callApi_11_method()" there. The problem isn't when the method is executed during runtime, it is when the class is loaded into the VM using the classloader. At that point, it tries to verify that indeed every method you call from your class exists, so the conditional check doesn't work, hence VerifyError's and breakage. The appropriate way to do it is the same way as the Android support library does it. The goal is to have an interface which only one implementation is every loaded at runtime. Any implementations for versions that your device doesn't support will never get loaded, so no VerifyErrors. If you have the support library installed in your Android SDK, check out extras/android/support/v4/src/java/android/support/v4/view/MenuItemCompat.java to see how the pattern works.
122 lines
3.6 KiB
Java
122 lines
3.6 KiB
Java
/*
|
|
* Copyright (C) 2010-12 Ciaran Gultnieks, ciaran@ciarang.com
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 3
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
package org.fdroid.fdroid;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.Closeable;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.InputStream;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
|
|
public final class Utils {
|
|
|
|
public static final int BUFFER_SIZE = 4096;
|
|
|
|
private static final String[] FRIENDLY_SIZE_FORMAT = {
|
|
"%.0f B", "%.0f KiB", "%.1f MiB", "%.2f GiB" };
|
|
|
|
|
|
public static void copy(InputStream input, OutputStream output)
|
|
throws IOException {
|
|
copy(input, output, null, null);
|
|
}
|
|
|
|
public static void copy(InputStream input, OutputStream output,
|
|
ProgressListener progressListener,
|
|
ProgressListener.Event templateProgressEvent)
|
|
throws IOException {
|
|
byte[] buffer = new byte[BUFFER_SIZE];
|
|
int bytesRead = 0;
|
|
while (true) {
|
|
int count = input.read(buffer);
|
|
if (count == -1) {
|
|
break;
|
|
}
|
|
if (progressListener != null) {
|
|
bytesRead += count;
|
|
templateProgressEvent.progress = bytesRead;
|
|
progressListener.onProgress(templateProgressEvent);
|
|
}
|
|
output.write(buffer, 0, count);
|
|
}
|
|
output.flush();
|
|
}
|
|
|
|
public static void closeQuietly(Closeable closeable) {
|
|
if (closeable == null) {
|
|
return;
|
|
}
|
|
try {
|
|
closeable.close();
|
|
} catch (IOException ioe) {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
public static String getFriendlySize(int size) {
|
|
double s = size;
|
|
int i = 0;
|
|
while (i < FRIENDLY_SIZE_FORMAT.length - 1 && s >= 1024) {
|
|
s = (100 * s / 1024) / 100.0;
|
|
i++;
|
|
}
|
|
return String.format(FRIENDLY_SIZE_FORMAT[i], s);
|
|
}
|
|
|
|
public static int countSubstringOccurrence(File file, String substring) throws IOException {
|
|
int count = 0;
|
|
BufferedReader reader = null;
|
|
try {
|
|
|
|
reader = new BufferedReader(new FileReader(file));
|
|
while(true) {
|
|
String line = reader.readLine();
|
|
if (line == null) {
|
|
break;
|
|
}
|
|
count += countSubstringOccurrence(line, substring);
|
|
}
|
|
|
|
} 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;
|
|
}
|
|
count ++;
|
|
index += substring.length();
|
|
}
|
|
return count;
|
|
}
|
|
|
|
}
|