parent
59b983eaec
commit
9d4fd2d3ac
@ -1,138 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.util; |
||||
|
||||
import android.text.TextUtils; |
||||
import android.util.Log; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.math.BigInteger; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
/** |
||||
* Created by lyy on 2017/3/21. |
||||
*/ |
||||
public class FileUtil { |
||||
|
||||
private static final String TAG = "FileUtil"; |
||||
|
||||
/** |
||||
* 通过流创建文件 |
||||
*/ |
||||
public static void createFileFormInputStream(InputStream is, String path) { |
||||
try { |
||||
FileOutputStream fos = new FileOutputStream(path); |
||||
byte[] buf = new byte[1376]; |
||||
while (is.read(buf) > 0) { |
||||
fos.write(buf, 0, buf.length); |
||||
} |
||||
is.close(); |
||||
fos.flush(); |
||||
fos.close(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 校验文件MD5码 |
||||
*/ |
||||
public static boolean checkMD5(String md5, File updateFile) { |
||||
if (TextUtils.isEmpty(md5) || updateFile == null) { |
||||
Log.e(TAG, "MD5 string empty or updateFile null"); |
||||
return false; |
||||
} |
||||
|
||||
String calculatedDigest = getFileMD5(updateFile); |
||||
if (calculatedDigest == null) { |
||||
Log.e(TAG, "calculatedDigest null"); |
||||
return false; |
||||
} |
||||
return calculatedDigest.equalsIgnoreCase(md5); |
||||
} |
||||
|
||||
/** |
||||
* 校验文件MD5码 |
||||
*/ |
||||
public static boolean checkMD5(String md5, InputStream is) { |
||||
if (TextUtils.isEmpty(md5) || is == null) { |
||||
Log.e(TAG, "MD5 string empty or updateFile null"); |
||||
return false; |
||||
} |
||||
|
||||
String calculatedDigest = getFileMD5(is); |
||||
if (calculatedDigest == null) { |
||||
Log.e(TAG, "calculatedDigest null"); |
||||
return false; |
||||
} |
||||
return calculatedDigest.equalsIgnoreCase(md5); |
||||
} |
||||
|
||||
/** |
||||
* 获取文件MD5码 |
||||
*/ |
||||
public static String getFileMD5(File updateFile) { |
||||
InputStream is; |
||||
try { |
||||
is = new FileInputStream(updateFile); |
||||
} catch (FileNotFoundException e) { |
||||
Log.e(TAG, "Exception while getting FileInputStream", e); |
||||
return null; |
||||
} |
||||
|
||||
return getFileMD5(is); |
||||
} |
||||
|
||||
/** |
||||
* 获取文件MD5码 |
||||
*/ |
||||
public static String getFileMD5(InputStream is) { |
||||
MessageDigest digest; |
||||
try { |
||||
digest = MessageDigest.getInstance("MD5"); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
Log.e(TAG, "Exception while getting digest", e); |
||||
return null; |
||||
} |
||||
|
||||
byte[] buffer = new byte[8192]; |
||||
int read; |
||||
try { |
||||
while ((read = is.read(buffer)) > 0) { |
||||
digest.update(buffer, 0, read); |
||||
} |
||||
byte[] md5sum = digest.digest(); |
||||
BigInteger bigInt = new BigInteger(1, md5sum); |
||||
String output = bigInt.toString(16); |
||||
// Fill to 32 chars
|
||||
output = String.format("%32s", output).replace(' ', '0'); |
||||
return output; |
||||
} catch (IOException e) { |
||||
throw new RuntimeException("Unable to process file for MD5", e); |
||||
} finally { |
||||
try { |
||||
is.close(); |
||||
} catch (IOException e) { |
||||
Log.e(TAG, "Exception on closing MD5 input stream", e); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -1,70 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package com.arialyy.aria.util; |
||||
|
||||
import android.os.Environment; |
||||
import java.security.MessageDigest; |
||||
import java.security.NoSuchAlgorithmException; |
||||
|
||||
/** |
||||
* Created by lyy on 2016/9/27. |
||||
* 下载路径帮助类 |
||||
*/ |
||||
public class PathUtil { |
||||
|
||||
/** |
||||
* 下载链接转换保存路径 |
||||
* |
||||
* @param downloadUrl 下载链接 |
||||
* @return 保存路径 |
||||
*/ |
||||
public static String urlconvertPath(String downloadUrl) { |
||||
return Environment.getDownloadCacheDirectory().getPath() + "/" + StringToHashKey(downloadUrl); |
||||
} |
||||
|
||||
/** |
||||
* 字符串转换为hash码 |
||||
*/ |
||||
public static String StringToHashKey(String str) { |
||||
String cacheKey; |
||||
try { |
||||
final MessageDigest mDigest = MessageDigest.getInstance("MD5"); |
||||
mDigest.update(str.getBytes()); |
||||
cacheKey = bytesToHexString(mDigest.digest()); |
||||
} catch (NoSuchAlgorithmException e) { |
||||
cacheKey = String.valueOf(str.hashCode()); |
||||
} |
||||
return cacheKey; |
||||
} |
||||
|
||||
/** |
||||
* 将普通字符串转换为16位进制字符串 |
||||
*/ |
||||
public static String bytesToHexString(byte[] src) { |
||||
StringBuilder stringBuilder = new StringBuilder("0x"); |
||||
if (src == null || src.length <= 0) { |
||||
return null; |
||||
} |
||||
char[] buffer = new char[2]; |
||||
for (byte aSrc : src) { |
||||
buffer[0] = Character.forDigit((aSrc >>> 4) & 0x0F, 16); |
||||
buffer[1] = Character.forDigit(aSrc & 0x0F, 16); |
||||
stringBuilder.append(buffer); |
||||
} |
||||
return stringBuilder.toString(); |
||||
} |
||||
} |
@ -1,113 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.util; |
||||
|
||||
import android.util.Log; |
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Created by lyy on 2015/7/30. |
||||
* 反射工具类 |
||||
*/ |
||||
public class ReflectionUtil { |
||||
private static final String TAG = "ReflectionUtil"; |
||||
|
||||
/** |
||||
* 获取类里面的所在字段 |
||||
*/ |
||||
public static Field[] getFields(Class clazz) { |
||||
Field[] fields = null; |
||||
fields = clazz.getDeclaredFields(); |
||||
if (fields == null || fields.length == 0) { |
||||
Class superClazz = clazz.getSuperclass(); |
||||
if (superClazz != null) { |
||||
fields = getFields(superClazz); |
||||
} |
||||
} |
||||
return fields; |
||||
} |
||||
|
||||
/** |
||||
* 获取所有字段,包括父类的字段 |
||||
*/ |
||||
public static List<Field> getAllFields(Class clazz) { |
||||
List<Field> fields = new ArrayList<>(); |
||||
Class personClazz = clazz.getSuperclass(); |
||||
if (personClazz != null) { |
||||
Collections.addAll(fields, personClazz.getDeclaredFields()); |
||||
} |
||||
Collections.addAll(fields, clazz.getDeclaredFields()); |
||||
return fields; |
||||
} |
||||
|
||||
/** |
||||
* 获取类里面的指定对象,如果该类没有则从父类查询 |
||||
*/ |
||||
public static Field getField(Class clazz, String name) { |
||||
Field field = null; |
||||
try { |
||||
field = clazz.getDeclaredField(name); |
||||
} catch (NoSuchFieldException e) { |
||||
try { |
||||
field = clazz.getField(name); |
||||
} catch (NoSuchFieldException e1) { |
||||
if (clazz.getSuperclass() == null) { |
||||
return field; |
||||
} else { |
||||
field = getField(clazz.getSuperclass(), name); |
||||
} |
||||
} |
||||
} |
||||
if (field != null) { |
||||
field.setAccessible(true); |
||||
} |
||||
return field; |
||||
} |
||||
|
||||
/** |
||||
* 利用递归找一个类的指定方法,如果找不到,去父亲里面找直到最上层Object对象为止。 |
||||
* |
||||
* @param clazz 目标类 |
||||
* @param methodName 方法名 |
||||
* @param params 方法参数类型数组 |
||||
* @return 方法对象 |
||||
*/ |
||||
public static Method getMethod(Class clazz, String methodName, final Class<?>... params) { |
||||
Method method = null; |
||||
try { |
||||
method = clazz.getDeclaredMethod(methodName, params); |
||||
} catch (NoSuchMethodException e) { |
||||
try { |
||||
method = clazz.getMethod(methodName, params); |
||||
} catch (NoSuchMethodException ex) { |
||||
if (clazz.getSuperclass() == null) { |
||||
Log.e(TAG, "无法找到" + methodName + "方法"); |
||||
return method; |
||||
} else { |
||||
method = getMethod(clazz.getSuperclass(), methodName, params); |
||||
} |
||||
} |
||||
} |
||||
if (method != null) { |
||||
method.setAccessible(true); |
||||
} |
||||
return method; |
||||
} |
||||
} |
@ -1,44 +0,0 @@ |
||||
/* |
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
package com.arialyy.aria.util; |
||||
|
||||
/** |
||||
* Created by lyy on 2017/3/6. |
||||
*/ |
||||
public enum Speed { |
||||
/** |
||||
* 最大速度为256kb |
||||
*/ |
||||
KB_256(64), /** |
||||
* 最大速度为512kb |
||||
*/ |
||||
KB_512(128), /** |
||||
* 最大速度为1mb |
||||
*/ |
||||
MB_1(256), /** |
||||
* 最大速度为2mb |
||||
*/ |
||||
MB_2(1024), /** |
||||
* 最大速度为10mb |
||||
*/ |
||||
MAX(8192); |
||||
int buf; |
||||
|
||||
Speed(int buf) { |
||||
this.buf = buf; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue