Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
<!-- build: add PMD Maven plugin for static code analysis -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.21.2</version>
<configuration>
<linkXRef>false</linkXRef>
<minimumTokens>100</minimumTokens>
<targetJdk>1.8</targetJdk> </configuration>
</plugin>
<!-- build: add PMD Maven plugin for static code analysis -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int read() throws IOException {
}

@Override
public int read(byte b[], int off, int len) throws IOException {
public int read(byte[] b, int off, int len) throws IOException {
int numRead = delegate.read(b, off, len);
int left = firstBytes.length - byteCount;
if (left > numRead) {
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/com/j256/simplemagic/ContentInfoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class ContentInfoUtil {
public final static int DEFAULT_READ_SIZE = 10 * 1024;

/** internal entries loaded once if the {@link ContentInfoUtil#MagicUtil()} constructor is used. */
private static MagicEntries internalMagicEntries;
private static volatile MagicEntries internalMagicEntries;

private final MagicEntries magicEntries;
private int fileReadSize = DEFAULT_READ_SIZE;
Expand All @@ -76,14 +76,19 @@ public ContentInfoUtil() {
*/
public ContentInfoUtil(ErrorCallBack errorCallBack) {
if (internalMagicEntries == null) {
try {
internalMagicEntries = readEntriesFromResource(INTERNAL_MAGIC_FILE, errorCallBack);
} catch (IOException e) {
throw new IllegalStateException(
"Could not load entries from internal magic file: " + INTERNAL_MAGIC_FILE, e);
}
if (internalMagicEntries == null) {
throw new IllegalStateException("Internal magic file not found in class-path: " + INTERNAL_MAGIC_FILE);
synchronized (ContentInfoUtil.class) {
if (internalMagicEntries == null) {
try {
internalMagicEntries = readEntriesFromResource(INTERNAL_MAGIC_FILE, errorCallBack);
} catch (IOException e) {
throw new IllegalStateException(
"Could not load entries from internal magic file: " + INTERNAL_MAGIC_FILE, e);
}
if (internalMagicEntries == null) {
throw new IllegalStateException(
"Internal magic file not found in class-path: " + INTERNAL_MAGIC_FILE);
}
}
}
}
this.magicEntries = internalMagicEntries;
Expand Down Expand Up @@ -328,7 +333,7 @@ public void setErrorCallBack(ErrorCallBack errorCallBack) {
}

private MagicEntries readEntriesFromFile(File fileOrDirectory, ErrorCallBack errorCallBack)
throws FileNotFoundException, IOException {
throws IOException {
if (fileOrDirectory.isFile()) {
FileReader reader = new FileReader(fileOrDirectory);
try {
Expand All @@ -337,13 +342,19 @@ private MagicEntries readEntriesFromFile(File fileOrDirectory, ErrorCallBack err
closeQuietly(reader);
}
} else if (fileOrDirectory.isDirectory()) {
MagicEntries entries = new MagicEntries();
for (File subFile : fileOrDirectory.listFiles()) {
MagicEntries entries = new MagicEntries();
File[] subFiles = fileOrDirectory.listFiles();
if (subFiles == null) {
return null;
}
for (File subFile : subFiles) {
FileReader fr = new FileReader(subFile);
try {
readEntries(entries, fr, errorCallBack);
} catch (IOException e) {
// ignore the file
if (errorCallBack != null) {
errorCallBack.error(subFile.getPath(), "could not read magic file: " + e.getMessage(), e);
}
} finally {
closeQuietly(fr);
}
Expand Down Expand Up @@ -418,6 +429,6 @@ public interface ErrorCallBack {
* @param e
* Exception that was thrown trying to parse the line or null if none.
*/
public void error(String line, String details, Exception e);
void error(String line, String details, Exception e);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/j256/simplemagic/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ public enum ContentType {
private final String[] fileExtensions;
private final IanaEntry ianaEntry;

private ContentType(String mimeType, String simpleName, String... fileExtensions) {
ContentType(String mimeType, String simpleName, String... fileExtensions) {
this.mimeType = mimeType;
this.simpleName = simpleName;
this.fileExtensions = fileExtensions;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/j256/simplemagic/endian/EndianType.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public enum EndianType {
// end
;

private EndianConverter converter;
private final EndianConverter converter;

private EndianType(EndianConverter converter) {
this.converter = converter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public byte[] convertToByteArray(long value, int size) {
if (size == 4) {
// BADC again
return new byte[] { (byte) ((value >> 16) & 0XFF), (byte) ((value >> 24) & 0XFF),
(byte) ((value >> 0) & 0XFF), (byte) ((value >> 8) & 0XFF) };
(byte) ((value) & 0XFF), (byte) ((value >> 8) & 0XFF) };
} else {
return null;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/j256/simplemagic/entries/IanaEntries.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ private void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// ignored
} catch (IOException ignored) {
// Intentionally ignored because this method is used only for cleanup.
// A failure while closing should not affect the main operation.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void readEntries(BufferedReader lineReader, ErrorCallBack errorCallBack)
break;
}
// skip blanks and comments
if (line.length() == 0 || line.charAt(0) == '#') {
if (line.isEmpty() || line.charAt(0) == '#') {
continue;
}

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/j256/simplemagic/entries/MagicEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class MagicEntry {
*/
ContentInfo matchBytes(byte[] bytes) {
ContentData data = matchBytes(bytes, 0, 0, null);
if (data == null || data.name == MagicEntryParser.UNKNOWN_NAME) {
if (data == null || MagicEntryParser.UNKNOWN_NAME.equals(data.name)) {
return null;
} else {
return new ContentInfo(data.name, data.mimeType, data.sb.toString(), data.partial);
Expand Down Expand Up @@ -197,7 +197,7 @@ private ContentData matchBytes(byte[] bytes, int prevOffset, int level, ContentD
* NOTE: the children will have the first opportunity to set this which makes sense since they are the most
* specific.
*/
if (name != MagicEntryParser.UNKNOWN_NAME && contentData.name == MagicEntryParser.UNKNOWN_NAME) {
if (!MagicEntryParser.UNKNOWN_NAME.equals(name) && MagicEntryParser.UNKNOWN_NAME.equals(contentData.name)) {
contentData.name = name;
}
/*
Expand All @@ -216,12 +216,22 @@ private ContentData matchBytes(byte[] bytes, int prevOffset, int level, ContentD
* Internal processing data about the content.
*/
static class ContentData {
String name;
boolean partial;
String mimeType;
int mimeTypeLevel;
private String name;
private boolean partial;
private String mimeType;
private int mimeTypeLevel;
final StringBuilder sb = new StringBuilder();

// add package-private getters/setters used within the entries package
String getName() { return name; }
void setName(String name) { this.name = name; }
boolean isPartial() { return partial; }
void setPartial(boolean partial) { this.partial = partial; }
String getMimeType() { return mimeType; }
void setMimeType(String mimeType) { this.mimeType = mimeType; }
int getMimeTypeLevel() { return mimeTypeLevel; }
void setMimeTypeLevel(int level) { this.mimeTypeLevel = level; }

private ContentData(String name, String mimeType, int mimeTypeLevel) {
this.name = name;
this.mimeType = mimeType;
Expand Down
Loading