public CompletableFuture < byte [ ] > asByteArray ( DiscordApi api ) { CompletableFuture < byte [ ] > future = new CompletableFuture <> ( ) ; try { if ( fileAsByteArray != null ) { future . complete ( fileAsByteArray ) ; return future ; } if ( fileAsBufferedImage != null || fileAsFile != null || fileAsIcon != null || fileAsUrl != null || fileAsInputStream != null ) { asInputStream ( api ) . thenApply ( stream -> { try ( InputStream in = new BufferedInputStream ( stream ) ; ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ) { byte [ ] buf = new byte [ 1024 ] ; int n ; while ( - 1 != ( n = in . read ( buf ) ) ) { out . write ( buf , 0 , n ) ; } return out . toByteArray ( ) ; } catch ( IOException e ) { throw new CompletionException ( e ) ; } } ) . whenComplete ( ( bytes , throwable ) -> { if ( throwable != null ) { future . completeExceptionally ( throwable ) ; } else { future . complete ( bytes ) ; } } ) ; return future ; } future . completeExceptionally ( new IllegalStateException ( "No file variant is set" ) ) ; } catch ( Throwable t ) { future . completeExceptionally ( t ) ; } return future ; }
Gets the byte array for the file .