Author: Addo
Version: 1.0
Requires: Java 8+
Library Type: Java-based wrapper for B4J
MemTypeChecker is a 100% reliable image validation library for B4J. Unlike simple file extension checks, this library uses three levels of verification to ensure an uploaded file is a real image:
Magic Number Check – Detects images based on their actual file signature.
MIME Type Check – Uses Java's built-in Files.probeContentType().
Image Decoding Test – Ensures the file is not corrupted by attempting to decode it.
This ensures hackers cannot bypass validation by renaming files like malware.exe → image.jpg!
✔ Supports JPEG, PNG, GIF, BMP, and WebP images.
✔ 100% built-in Java solution – no external dependencies.
✔ Works on Java 8 and above.
✔ Fast and efficient image verification.
Usage
Get MIME Type of a File
Check Only the Magic Number
Check If an Image Can Be Decoded
Magic Number Check – Reads the first few bytes of the file and compares with known image signatures.
MIME Type Check – Uses Files.probeContentType() to verify the OS-reported MIME type.
Decode Image – Uses Java’s ImageIO.read() to confirm the file is truly an image.
v1.0 (Initial Release)
Version: 1.0
Requires: Java 8+
Library Type: Java-based wrapper for B4J
MemTypeChecker is a 100% reliable image validation library for B4J. Unlike simple file extension checks, this library uses three levels of verification to ensure an uploaded file is a real image:



This ensures hackers cannot bypass validation by renaming files like malware.exe → image.jpg!

Features
✔ Supports JPEG, PNG, GIF, BMP, and WebP images.✔ 100% built-in Java solution – no external dependencies.
✔ Works on Java 8 and above.
✔ Fast and efficient image verification.
Usage
B4X:
' simply declare it
Dim checker As MemTypeChecker
' then use it
Log(checker.isValidImageMagicNumber(File.DirApp & "/tempUpload/2.gif"))
Get MIME Type of a File
B4X:
Dim mime As String = checker.getMimeType(filePath)
Log("MIME Type: " & mime)
Check Only the Magic Number
B4X:
If checker.IsValidImageMagicNumber(filePath) Then
Log("File has a valid image signature!")
Else
Log("Invalid image signature!")
End If
Check If an Image Can Be Decoded
B4X:
If checker.CanDecodeImage(filePath) Then
Log("Image is readable!")
Else
Log("Image is corrupted or not an image!")
End If
How It Works Internally



Changelog
v1.0 (Initial Release)- Added magic number verification for JPEG, PNG, GIF, BMP, WEBP.
- Integrated Java MIME type detection.
- Added image decoding validation using ImageIO.read().