MCPcopy Create free account
hub / github.com/SeanDragon/protools / ToolFileType

Class ToolFileType

common/src/main/java/pro/tools/file/ToolFileType.java:15–102  ·  view source on GitHub ↗

文件类型判断类 @author SeanDragon

Source from the content-addressed store, hash-verified

13 * @author SeanDragon
14 */
15public final class ToolFileType {
16 /**
17 * Constructor
18 */
19 private ToolFileType() {
20 }
21
22 /**
23 * 得到文件头
24 *
25 * @param inputStream
26 * 文件路径
27 *
28 * @return 文件头
29 *
30 * @throws IOException
31 */
32 private static String getFileContent(InputStream inputStream) throws IOException {
33
34 byte[] b = new byte[28];
35
36 try {
37 inputStream.read(b, 0, 28);
38 } finally {
39 if (inputStream != null) {
40 try {
41 inputStream.close();
42 } catch (IOException e) {
43 e.printStackTrace();
44 throw e;
45 }
46 }
47 }
48 return ToolConvert.bytes2HexString(b);
49 }
50
51 /**
52 * 判断文件类型
53 *
54 * @param inputStream
55 * 文件流
56 *
57 * @return 文件类型
58 */
59 public static FileType getType(InputStream inputStream) throws IOException {
60
61 String fileHead = getFileContent(inputStream);
62
63 if (fileHead == null || fileHead.length() == 0) {
64 return null;
65 }
66
67 fileHead = fileHead.toUpperCase();
68
69 FileType[] fileTypes = FileType.values();
70
71 for (FileType type : fileTypes) {
72 if (fileHead.startsWith(type.getValue())) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected