public CompletionStage < String > detectMimeTypeAsync ( final Path path ) { String filename = path . getFileName ( ) . toString ( ) ; Supplier < CompletionStage < byte [ ] > > supplier = ( ) -> { final CompletableFuture < byte [ ] > futureBytes = new CompletableFuture < byte [ ] > ( ) ; AsynchronousFileChannel channel ; try { channel = AsynchronousFileChannel . open ( path , StandardOpenOption . READ ) ; } catch ( IOException e ) { futureBytes . completeExceptionally ( new GetBytesException ( e ) ) ; return futureBytes ; } final ByteBuffer buf = ByteBuffer . allocate ( getMaxGetBytesLength ( ) ) ; channel . read ( buf , 0 , futureBytes , new CompletionHandler < Integer , CompletableFuture < byte [ ] > > ( ) { @ Override public void completed ( Integer nBytes , CompletableFuture < byte [ ] > f ) { if ( nBytes == - 1 ) nBytes = 0 ; // handle empty file byte [ ] bytes = new byte [ nBytes ] ; buf . rewind ( ) ; buf . get ( bytes , 0 , nBytes ) ; f . complete ( bytes ) ; } @ Override public void failed ( Throwable exc , CompletableFuture < byte [ ] > f ) { f . completeExceptionally ( new GetBytesException ( exc ) ) ; } } ) ; return futureBytes ; } ; return detectMimeTypeAsync ( filename , supplier ) ; }
Determines the MIME type of a file .