src/main/java/com/github/hashicraft/projector/downloader/FileDownloader.java
private void downloadFile(String location) {
    System.out.println("Starting download for:" + location);

    // is already in process?
    if (this.cache.get(location) != null) {
      return;
    }

    // add the url to the cache, we can update this once download is complete
    this.cache.put(location, new PictureData(location));

    // run in the background
    service.submit(() -> {
      // retry the download in case the file is not currently avaialble
      RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withBackoff(3, 60, ChronoUnit.SECONDS).withMaxAttempts(200)
          .withMaxDuration(Duration.ofMinutes(10)).handle(RejectedExecutionException.class)
          .onRetry(e -> System.out.println("Unable to download, retrying: " + e.getLastFailure()))
          .onRetriesExceeded(e -> System.out.println("Unable to download, aborting " + e.getFailure()));

      Failsafe.with(retryPolicy).run(() -> {
        try {
          boolean isURL = false;

          final File tmpFile = File.createTempFile("image", "");
          tmpFile.deleteOnExit();

          File file = null;

          // Are we dealing with a local file or URL?
          try {
            final URL url = new URL(location);
            if (url.getProtocol().equals("http") || url.getProtocol().equals("https")) {
              isURL = true;
            }

            // We need to ensure that download threads do not overwhelm the system so we
            // limit
            // downloads to 10 concurrent threads
            Future<?> future = downloadService.submit(() -> {
              try {
                BufferedInputStream in = new BufferedInputStream(url.openStream());
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));

                byte dataBuffer[] = new byte[1024];
                int bytesRead;
                while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                  out.write(dataBuffer, 0, bytesRead);
                }

                out.close();
                in.close();
              } catch (Exception ex) {
              }
            });

            synchronized (future) {
              // wait 60s for the download to complete before quiting
              future.get(60L, TimeUnit.SECONDS);
            }
          } catch (MalformedURLException me) {
            isURL = false;
          } catch (TimeoutException e) {
            throw new FileNotFoundException("Timeout attempting to download file at url: " + e);
          }

          // not a URL check if is a local file
          if (isURL) {
            file = tmpFile;
          } else {
            file = new File(location);
            if (!file.exists()) {
              System.out.println("Unable to load file: " + location);
              throw new FileNotFoundException("File not found: " + location);
            }
          }

          // convert to a png
          BufferedImage bufferedImage = ImageIO.read(file);

          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          ImageIO.write(bufferedImage, "png", byteArrayOut);

          // byte[] resultingBytes = byteArrayOut.toByteArray();
          // ImageIO.write(bufferedImage, "png", new File(outputFile));
          InputStream targetStream = new ByteArrayInputStream(byteArrayOut.toByteArray());

          NativeImage nativeImage = NativeImage.read(targetStream);
          NativeImageBackedTexture nativeTexture = new NativeImageBackedTexture(nativeImage);

          Identifier id = MinecraftClient.getInstance().getTextureManager().registerDynamicTexture("image/pictures",
              nativeTexture);

          // update the cache
          synchronized (mutex) {
            PictureData data = this.cache.get(location);
            data.identifier = id;
            data.height = bufferedImage.getHeight();
            data.width = bufferedImage.getWidth();
          }

          System.out.println("Downloaded url: " + location);
        } catch (Exception ex) {
          throw new RejectedExecutionException("Unable to download file " + ex);
        }
      });
    });
  }
// downloads the file as a background process

downloads the file as a background process
downloads the file as a background process
private void downloadFile(String location) {
    System.out.println("Starting download for:" + location);
    if (this.cache.get(location) != null) {
      return;
    }
    this.cache.put(location, new PictureData(location));
    service.submit(() -> {
      RetryPolicy<Object> retryPolicy = new RetryPolicy<>().withBackoff(3, 60, ChronoUnit.SECONDS).withMaxAttempts(200)
          .withMaxDuration(Duration.ofMinutes(10)).handle(RejectedExecutionException.class)
          .onRetry(e -> System.out.println("Unable to download, retrying: " + e.getLastFailure()))
          .onRetriesExceeded(e -> System.out.println("Unable to download, aborting " + e.getFailure()));
      Failsafe.with(retryPolicy).run(() -> {
        try {
          boolean isURL = false;
          final File tmpFile = File.createTempFile("image", "");
          tmpFile.deleteOnExit();
          File file = null;
          try {
            final URL url = new URL(location);
            if (url.getProtocol().equals("http") || url.getProtocol().equals("https")) {
              isURL = true;
            }
            Future<?> future = downloadService.submit(() -> {
              try {
                BufferedInputStream in = new BufferedInputStream(url.openStream());
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmpFile));
                byte dataBuffer[] = new byte[1024];
                int bytesRead;
                while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                  out.write(dataBuffer, 0, bytesRead);
                }
                out.close();
                in.close();
              } catch (Exception ex) {
              }
            });
            synchronized (future) {
              future.get(60L, TimeUnit.SECONDS);
            }
          } catch (MalformedURLException me) {
            isURL = false;
          } catch (TimeoutException e) {
            throw new FileNotFoundException("Timeout attempting to download file at url: " + e);
          }
          if (isURL) {
            file = tmpFile;
          } else {
            file = new File(location);
            if (!file.exists()) {
              System.out.println("Unable to load file: " + location);
              throw new FileNotFoundException("File not found: " + location);
            }
          }
          BufferedImage bufferedImage = ImageIO.read(file);
          ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
          ImageIO.write(bufferedImage, "png", byteArrayOut);
          InputStream targetStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
          NativeImage nativeImage = NativeImage.read(targetStream);
          NativeImageBackedTexture nativeTexture = new NativeImageBackedTexture(nativeImage);
          Identifier id = MinecraftClient.getInstance().getTextureManager().registerDynamicTexture("image/pictures",
              nativeTexture);
          synchronized (mutex) {
            PictureData data = this.cache.get(location);
            data.identifier = id;
            data.height = bufferedImage.getHeight();
            data.width = bufferedImage.getWidth();
          }
          System.out.println("Downloaded url: " + location);
        } catch (Exception ex) {
          throw new RejectedExecutionException("Unable to download file " + ex);
        }
      });
    });
  }
private void downloadFile ( String location ) { System . out . println ( "Starting download for:" + location ) ; if ( this . cache . get ( location ) != null ) { return ; } this . cache . put ( location , new PictureData ( location ) ) ; service . submit ( ( ) -> { RetryPolicy < Object > retryPolicy = new RetryPolicy < > ( ) . withBackoff ( 3 , 60 , ChronoUnit . SECONDS ) . withMaxAttempts ( 200 ) . withMaxDuration ( Duration . ofMinutes ( 10 ) ) . handle ( RejectedExecutionException . class ) . onRetry ( e -> System . out . println ( "Unable to download, retrying: " + e . getLastFailure ( ) ) ) . onRetriesExceeded ( e -> System . out . println ( "Unable to download, aborting " + e . getFailure ( ) ) ) ; Failsafe . with ( retryPolicy ) . run ( ( ) -> { try { boolean isURL = false ; final File tmpFile = File . createTempFile ( "image" , "" ) ; tmpFile . deleteOnExit ( ) ; File file = null ; try { final URL url = new URL ( location ) ; if ( url . getProtocol ( ) . equals ( "http" ) || url . getProtocol ( ) . equals ( "https" ) ) { isURL = true ; } Future < ? > future = downloadService . submit ( ( ) -> { try { BufferedInputStream in = new BufferedInputStream ( url . openStream ( ) ) ; BufferedOutputStream out = new BufferedOutputStream ( new FileOutputStream ( tmpFile ) ) ; byte dataBuffer [ ] = new byte [ 1024 ] ; int bytesRead ; while ( ( bytesRead = in . read ( dataBuffer , 0 , 1024 ) ) != - 1 ) { out . write ( dataBuffer , 0 , bytesRead ) ; } out . close ( ) ; in . close ( ) ; } catch ( Exception ex ) { } } ) ; synchronized ( future ) { future . get ( 60L , TimeUnit . SECONDS ) ; } } catch ( MalformedURLException me ) { isURL = false ; } catch ( TimeoutException e ) { throw new FileNotFoundException ( "Timeout attempting to download file at url: " + e ) ; } if ( isURL ) { file = tmpFile ; } else { file = new File ( location ) ; if ( ! file . exists ( ) ) { System . out . println ( "Unable to load file: " + location ) ; throw new FileNotFoundException ( "File not found: " + location ) ; } } BufferedImage bufferedImage = ImageIO . read ( file ) ; ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream ( ) ; ImageIO . write ( bufferedImage , "png" , byteArrayOut ) ; InputStream targetStream = new ByteArrayInputStream ( byteArrayOut . toByteArray ( ) ) ; NativeImage nativeImage = NativeImage . read ( targetStream ) ; NativeImageBackedTexture nativeTexture = new NativeImageBackedTexture ( nativeImage ) ; Identifier id = MinecraftClient . getInstance ( ) . getTextureManager ( ) . registerDynamicTexture ( "image/pictures" , nativeTexture ) ; synchronized ( mutex ) { PictureData data = this . cache . get ( location ) ; data . identifier = id ; data . height = bufferedImage . getHeight ( ) ; data . width = bufferedImage . getWidth ( ) ; } System . out . println ( "Downloaded url: " + location ) ; } catch ( Exception ex ) { throw new RejectedExecutionException ( "Unable to download file " + ex ) ; } } ) ; } ) ; }
downloads the file as a background process
downloads the file as a background process
// is already in process?
 // add the url to the cache, we can update this once download is complete
 // run in the background
 // retry the download in case the file is not currently avaialble
 // Are we dealing with a local file or URL?
 // We need to ensure that download threads do not overwhelm the system so we
 // limit
 // downloads to 10 concurrent threads
 // wait 60s for the download to complete before quiting
 // not a URL check if is a local file
 // convert to a png
 // byte[] resultingBytes = byteArrayOut.toByteArray();
 // ImageIO.write(bufferedImage, "png", new File(outputFile));
 // update the cache
