【发布时间】:2013-10-22 20:09:53
【问题描述】:
我正在尝试确定通过流接收的文件类型(以便使用正确的文件扩展名对其进行命名)。我已经编写了determineFormat(String str) 方法,该方法由bytesToHex() 方法提供(字节来自缓冲区)。不幸的是,这并没有按预期工作。 determineFormat() 总是返回 .aac 扩展,即使 .mp3 正在被接收。
public String determineFormat(String str) {
Pattern aacPattern = Pattern.compile("FFF1|FFF9");
Pattern mp3Pattern = Pattern.compile("494433|FFFB");
Matcher matcher = aacPattern.matcher(str);
if(matcher.find()) {
return "aac";
}
matcher = mp3Pattern.matcher(str);
if(matcher.find()) {
return "mp3";
}
return "unknown";
}
我使用此方法提供我的 determineFormat() 方法:
public String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
【问题讨论】: