code: public static String getImageFormat(ImageInputStream input) {
          Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
          String format = "unknown";
          if (readers.hasNext()) {
              ImageReader reader = readers.next();
              try {
                  format = reader.getFormatName();
              } catch (IOException e) {
                  e.printStackTrace();
              } finally {
                  reader.dispose();
              }
          }
  
          return format;
      }
docs: this function will not close input, need caller to close it.