public CompletionStage < String > detectMimeTypeAsync ( String filename , Supplier < CompletionStage < byte [ ] > > getBytesAsync ) { Set < WeightedMimeType > weightedMimeTypes = filenameToWmts ( filename ) ; Set < String > globMimeTypes = findBestMimeTypes ( weightedMimeTypes ) ; if ( globMimeTypes . size ( ) == 1 ) { return CompletableFuture . completedFuture ( globMimeTypes . iterator ( ) . next ( ) ) ; } return getBytesAsync . get ( ) . thenApply ( bytes -> { for ( String magicMimeType : bytesToMimeTypes ( bytes ) ) { if ( globMimeTypes . isEmpty ( ) ) { return magicMimeType ; } else { for ( String globMimeType : globMimeTypes ) { if ( isMimeTypeEqualOrSubclass ( globMimeType , magicMimeType ) ) { return globMimeType ; } } } } if ( isText ( bytes ) ) { return "text/plain" ; } return "application/octet-stream" ; } ) . exceptionally ( ex -> { throw new CompletionException ( new GetBytesException ( ex ) ) ; } ) ; }
Detects a MIME type from a filename and bytes .