-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathContentInfo.java
More file actions
110 lines (96 loc) · 3.11 KB
/
ContentInfo.java
File metadata and controls
110 lines (96 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.j256.simplemagic;
import java.io.Serializable;
/**
* Information associated with some content, returned by the magic matching code in
* {@link ContentInfoUtil#findMatch(String)} and other methods.
*
* @author graywatson
*/
public class ContentInfo implements Serializable {
private static final long serialVersionUID = 1342819252130963539L;
public static final ContentInfo EMPTY_INFO = new ContentInfo(ContentType.EMPTY);
private final ContentType contentType;
private final String name;
private final String message;
private final String mimeType;
private final String[] fileExtensions;
private final boolean partial;
public ContentInfo(String name, String mimeType, String message, boolean partial) {
this.contentType = ContentType.fromMimeType(mimeType);
if (this.contentType == ContentType.OTHER) {
this.name = name;
this.fileExtensions = null;
} else {
this.name = this.contentType.getSimpleName();
this.fileExtensions = this.contentType.getFileExtensions();
}
this.mimeType = mimeType;
this.message = message;
this.partial = partial;
}
public ContentInfo(ContentType contentType) {
this.contentType = contentType;
this.name = contentType.getSimpleName();
this.mimeType = contentType.getMimeType();
this.message = null;
this.fileExtensions = contentType.getFileExtensions();
this.partial = false;
}
/**
* Returns the internal enumerated type associated with the content or {@link ContentType#OTHER} if not known.
*/
public ContentType getContentType() {
return contentType;
}
/**
* Returns the short name of the content either from the content-type or extracted from the message. If the
* content-type is known then this is a specific name string. Otherwise this is usually the first word of the
* message generated by the magic file.
*/
public String getName() {
return name;
}
/**
* Returns the mime-type or null if none.
*/
public String getMimeType() {
return mimeType;
}
/**
* Returns the full message as generated by the magic matching code or null if none. This should be similar to the
* output from the Unix file(1) command.
*/
public String getMessage() {
return message;
}
/**
* Returns an array of associated file-extensions or null if none.
*/
public String[] getFileExtensions() {
return fileExtensions;
}
/**
* Whether or not this was a partial match. For some of the types, there is a main matching pattern and then more
* specific patterns which detect additional features of the type. A partial match means that none of the more
* specific patterns fully matched the content. It's probably still of the type but just not a variant that the
* entries from the magic file(s) know about.
*/
public boolean isPartial() {
return partial;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(name);
if (contentType != null) {
sb.append(", type ").append(contentType);
}
if (mimeType != null) {
sb.append(", mime '").append(mimeType).append('\'');
}
if (message != null) {
sb.append(", msg '").append(message).append('\'');
}
return sb.toString();
}
}