ImageUtils { @Nullable public static ImageType getImageType(@Nullable final byte[] imageBytes) { if ((imageBytes != null) && (imageBytes.length > 0)) { try { final ImageInputStream iis = ImageIO.createImageInputStream(new ByteArrayInputStream(imageBytes)); final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(iis); if (imageReaders != null) { while (imageReaders.hasNext()) { final ImageReader reader = imageReaders.next(); final ImageType imageType = ImageType.findByFormatName(reader.getFormatName()); if (imageType != null) { return imageType; } } } } catch (IOException e) { LOG.error("IOException while trying to read the image type, returning null"); } } return null; } private  ImageUtils(); static boolean isSupportedImage(@Nullable final byte[] imageBytes); @Nullable static Image createJpegThumbnail(@Nullable final byte[] imageBytes, final int lengthOfLongestSideInPixels); @Nullable static Geolocation getGeolocation(@Nullable final byte[] imageBytes); @Nullable static ImageOrientation getOrientation(@Nullable final byte[] imageBytes); @Nullable static BufferedImage convertToBufferedImage(@Nullable final byte[] imageBytes); @Nullable static byte[] convertToJpegByteArray(@Nullable final BufferedImage image); @Nullable static ImageType getImageType(@Nullable final byte[] imageBytes);  }
@Test public void testGetImageType() throws IOException { Assert.assertEquals(ImageType.JPEG, ImageUtils.getImageType(IMAGE_1)); Assert.assertEquals(ImageType.JPEG, ImageUtils.getImageType(IMAGE_2)); Assert.assertEquals(ImageType.PNG, ImageUtils.getImageType(IMAGE_3)); Assert.assertEquals(ImageType.GIF, ImageUtils.getImageType(IMAGE_4)); Assert.assertNull(ImageUtils.getImageType(IMAGE_5)); Assert.assertEquals(ImageType.JPEG, ImageUtils.getImageType(IMAGE_6)); Assert.assertNull(ImageUtils.getImageType(NOT_AN_IMAGE)); Assert.assertNull(ImageUtils.getImageType((byte[])null)); Assert.assertNull(ImageUtils.getImageType(new byte[]{})); }