Class OpenMimeType

java.lang.Object
cloud.opencode.base.io.OpenMimeType

public final class OpenMimeType extends Object
MIME Type Detection Utility Class MIME 类型检测工具类

Provides MIME type detection from file extension and content (magic numbers).

提供基于文件扩展名和内容(魔数)的 MIME 类型检测。

Features | 主要功能:

  • Detect by file extension - 按扩展名检测
  • Detect by content (magic numbers) - 按内容检测(魔数)
  • Comprehensive MIME type mappings - 完整的 MIME 类型映射

Usage Examples | 使用示例:

// Detect by extension
String mime = OpenMimeType.fromExtension("pdf");  // "application/pdf"

// Detect from file (extension + content)
String mime = OpenMimeType.detect(Path.of("image.png"));

// Detect from content only
String mime = OpenMimeType.fromContent(bytes);

Security | 安全性:

  • Thread-safe: Yes (stateless utility with static maps) - 线程安全: 是(使用静态映射的无状态工具类)
  • Null-safe: Yes, null/unknown returns application/octet-stream - 空值安全: 是,null/未知返回application/octet-stream
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • APPLICATION_OCTET_STREAM

      public static final String APPLICATION_OCTET_STREAM
      Default MIME type for unknown content 未知内容的默认 MIME 类型
      See Also:
    • TEXT_PLAIN

      public static final String TEXT_PLAIN
      Common text MIME type 常见文本 MIME 类型
      See Also:
    • TEXT_HTML

      public static final String TEXT_HTML
      Common HTML MIME type 常见 HTML MIME 类型
      See Also:
    • APPLICATION_JSON

      public static final String APPLICATION_JSON
      Common JSON MIME type 常见 JSON MIME 类型
      See Also:
    • APPLICATION_XML

      public static final String APPLICATION_XML
      Common XML MIME type 常见 XML MIME 类型
      See Also:
    • APPLICATION_PDF

      public static final String APPLICATION_PDF
      Common PDF MIME type 常见 PDF MIME 类型
      See Also:
  • Method Details

    • fromExtension

      public static Optional<String> fromExtension(String extension)
      Get MIME type from file extension 从文件扩展名获取 MIME 类型
      Parameters:
      extension - the file extension (without dot) | 文件扩展名(不含点)
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • fromExtension

      public static String fromExtension(String extension, String defaultMime)
      Get MIME type from file extension with default 从文件扩展名获取 MIME 类型(带默认值)
      Parameters:
      extension - the file extension | 文件扩展名
      defaultMime - default MIME type | 默认 MIME 类型
      Returns:
      MIME type | MIME 类型
    • fromPath

      public static Optional<String> fromPath(Path path)
      Get MIME type from path extension 从路径扩展名获取 MIME 类型
      Parameters:
      path - the file path | 文件路径
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • fromFilename

      public static Optional<String> fromFilename(String filename)
      Get MIME type from filename 从文件名获取 MIME 类型
      Parameters:
      filename - the filename | 文件名
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • fromContent

      public static Optional<String> fromContent(Path path)
      Detect MIME type from file content (magic numbers) 从文件内容检测 MIME 类型(魔数)
      Parameters:
      path - the file path | 文件路径
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • fromContent

      public static Optional<String> fromContent(byte[] data)
      Detect MIME type from byte array content 从字节数组内容检测 MIME 类型
      Parameters:
      data - the content bytes | 内容字节
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • fromContent

      public static Optional<String> fromContent(InputStream input)
      Detect MIME type from input stream 从输入流检测 MIME 类型
      Parameters:
      input - the input stream (must support mark/reset) | 输入流(必须支持 mark/reset)
      Returns:
      MIME type or empty if unknown | MIME 类型,未知时返回空
    • detect

      public static String detect(Path path)
      Detect MIME type from file (combines extension and content detection) 从文件检测 MIME 类型(综合扩展名和内容检测)

      Priority: content detection > extension detection > system probe

      优先级:内容检测 > 扩展名检测 > 系统探测

      Parameters:
      path - the file path | 文件路径
      Returns:
      MIME type, defaults to application/octet-stream | MIME 类型,默认 application/octet-stream
    • detectByExtension

      public static String detectByExtension(Path path)
      Detect MIME type preferring extension over content 优先使用扩展名检测 MIME 类型
      Parameters:
      path - the file path | 文件路径
      Returns:
      MIME type, defaults to application/octet-stream | MIME 类型
    • isText

      public static boolean isText(String mimeType)
      Check if MIME type is text-based 检查 MIME 类型是否为文本类型
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      true if text-based | 如果是文本类型返回 true
    • isImage

      public static boolean isImage(String mimeType)
      Check if MIME type is image 检查 MIME 类型是否为图片
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      true if image | 如果是图片返回 true
    • isAudio

      public static boolean isAudio(String mimeType)
      Check if MIME type is audio 检查 MIME 类型是否为音频
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      true if audio | 如果是音频返回 true
    • isVideo

      public static boolean isVideo(String mimeType)
      Check if MIME type is video 检查 MIME 类型是否为视频
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      true if video | 如果是视频返回 true
    • isBinary

      public static boolean isBinary(String mimeType)
      Check if MIME type is binary 检查 MIME 类型是否为二进制
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      true if binary (not text) | 如果是二进制返回 true
    • getExtension

      public static Optional<String> getExtension(String mimeType)
      Get file extension for MIME type 获取 MIME 类型对应的文件扩展名
      Parameters:
      mimeType - the MIME type | MIME 类型
      Returns:
      extension or empty if unknown | 扩展名,未知时返回空