mirror of
https://github.com/zhigang1992/AndroidVideoCache.git
synced 2026-03-30 17:03:37 +08:00
fix too long file name for cache. Md5FileNameGenerator tests
This commit is contained in:
@@ -3,8 +3,6 @@ package com.danikula.videocache;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static com.danikula.videocache.Preconditions.checkNotNull;
|
||||
|
||||
@@ -15,6 +13,7 @@ import static com.danikula.videocache.Preconditions.checkNotNull;
|
||||
*/
|
||||
public class Md5FileNameGenerator implements FileNameGenerator {
|
||||
|
||||
private static final int MAX_EXTENSION_LENGTH = 4;
|
||||
private final File cacheDirectory;
|
||||
|
||||
public Md5FileNameGenerator(File cacheDirectory) {
|
||||
@@ -25,32 +24,15 @@ public class Md5FileNameGenerator implements FileNameGenerator {
|
||||
public File generate(String url) {
|
||||
checkNotNull(url);
|
||||
String extension = getExtension(url);
|
||||
String name = computeMD5(url);
|
||||
String name = ProxyCacheUtils.computeMD5(url);
|
||||
name = TextUtils.isEmpty(extension) ? name : name + "." + extension;
|
||||
return new File(cacheDirectory, name);
|
||||
}
|
||||
|
||||
private String getExtension(String url) {
|
||||
int dotIndex = url.lastIndexOf('.');
|
||||
int slashIndex = url.lastIndexOf(File.separator);
|
||||
return dotIndex != -1 && dotIndex > slashIndex ? url.substring(dotIndex + 1, url.length()) : "";
|
||||
}
|
||||
|
||||
private String computeMD5(String string) {
|
||||
try {
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||
byte[] digestBytes = messageDigest.digest(string.getBytes());
|
||||
return bytesToHexString(digestBytes);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String bytesToHexString(byte[] bytes) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
int slashIndex = url.lastIndexOf('/');
|
||||
return dotIndex != -1 && dotIndex > slashIndex && dotIndex + 2 + MAX_EXTENSION_LENGTH > url.length() ?
|
||||
url.substring(dotIndex + 1, url.length()) : "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static com.danikula.videocache.Preconditions.checkArgument;
|
||||
@@ -86,4 +88,23 @@ class ProxyCacheUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static String computeMD5(String string) {
|
||||
try {
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
|
||||
byte[] digestBytes = messageDigest.digest(string.getBytes());
|
||||
return bytesToHexString(digestBytes);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
static String bytesToHexString(byte[] bytes) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user