Android Code Snippet Test if a file is a Java Class File

Similar threads

B4A Code Snippet [B4X] Bytes To File
B4A Code Snippet [B4X] Use WebView to show large images
B4A Code Snippet Upload files with WebView
B4A Library Printing and Pdf creation
B4A Code Snippet [B4X] DownloadAndSave
B4A Code Snippet [B4X] Convert bitmap to BMP format


Licensed User
Sub Name: Is Class File

Description: Determines whether a file looks like it could be a Java Class file (compiled Java code).

Sub IsClassFile(Path As String, FileName As String) As Boolean
    If Not(File.Exists(Path,FileName)) OR File.IsDirectory(Path,FileName) Then Return False
    If File.Size(Path,FileName) < 4 Then Return False
    Dim RAF As RandomAccessFile
    Dim result As Boolean = RAF.ReadInt(0) = 0xCAFEBABE
    Return result
End Sub
Depends On: RandomAccesFile
Tags: Class File, Java, File Testing, Magic Numbers

Note: This also works on B4J.