Class FileTypes
This class is security-sensitive: misclassifying a binary file as text could lead to binary data leaking into LLM prompts or other text-processing pipelines. Conversely, misclassifying a text file as binary may cause useful content to be skipped.
The classification relies on Apache Tika for MIME type detection based on file content (not extension), which provides resilience against extension-spoofing attacks.
Additionally, this class provides a method to detect hidden files
(those whose path contains a dot-prefixed component), which is
important for preventing accidental exposure of sensitive
configuration files like .git/config, .env, or
.ssh/id_rsa.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanDetermines whether the given file is binary by detecting its MIME type via Apache Tika and then classifying that MIME type.static booleanisBinaryMimeType(String rawMime) Classifies a MIME type string as binary or text-based.static booleanChecks whether any component of the given path starts with a dot (.), indicating a hidden file or directory.
-
Constructor Details
-
FileTypes
public FileTypes()
-
-
Method Details
-
isBinary
Determines whether the given file is binary by detecting its MIME type via Apache Tika and then classifying that MIME type.Non-regular files (directories, symlinks pointing to directories, device files, etc.) are treated as non-binary. If the MIME type cannot be detected due to an I/O error, the file is conservatively treated as non-binary and a warning is logged.
This method reads only the file header (magic bytes), not the entire file content, making it efficient for large files.
- Parameters:
path- the path to the file to inspect; must not be null- Returns:
trueif the file is a regular file and its detected MIME type is classified as binary,falseotherwise- See Also:
-
isBinaryMimeType
Classifies a MIME type string as binary or text-based.The classification rules are as follows:
null, blank, or malformed strings (no slash) — non-binary.- Any type under the
texttop-level type — non-binary. image,audio,video,font,model— binary.- For
application: subtypes ending with+xml,+json,+yaml, or+csvare non-binary. A set of well-known text-based subtypes (e.g.json,xml,javascript,x-sh,x-python,sql,yaml,toml,graphql, and others) are non-binary. All otherapplicationsubtypes are binary. - Any other top-level type — binary.
Comparison is case-insensitive. Leading and trailing whitespace is stripped.
Examples of binary MIME types:
application/octet-stream,application/pdf,application/zip,application/x-executable,image/png,audio/mpeg.Examples of non-binary MIME types:
text/plain,text/html,application/json,application/xml,application/javascript,application/x-sh.- Parameters:
rawMime- the MIME type string to classify; may be null- Returns:
trueif the MIME type represents a binary format,falseotherwise
-
isHidden
Checks whether any component of the given path starts with a dot (.), indicating a hidden file or directory.This is important for security: hidden files and directories often contain sensitive configuration data (e.g.
.git/config,.env,.ssh/id_rsa,.aws/credentials) that should not be exposed to text-processing pipelines or LLM prompts.The check iterates over all path components. Both
.(current directory) and..(parent directory) are considered hidden, as are files like.gitignoreand directories like.git.Note: this method inspects the path string itself. It does not resolve symlinks or normalize the path. Callers should consider whether normalization is needed before invoking this method.
- Parameters:
path- the path to inspect; must not be null- Returns:
trueif any component of the path starts with a dot,falseotherwise
-