JPGFileUtils { public static Dimensions getDimensions(final File outFile) throws FileNotFoundException, IOException { Dimensions dimensions = null; final FileInputStream is = new FileInputStream(outFile); try { if (is.read() == 0xFF && is.read() == 0xD8) { int height = -1; int width = -1; while (is.read() == 255) { final int marker = is.read(); final int len = readIntBigEndianOnTwoBytes(is); if (marker == 192 || marker == 193 || marker == 194) { is.skip(1); height = readIntBigEndianOnTwoBytes(is); width = readIntBigEndianOnTwoBytes(is); break; } is.skip(len - 2); } if (height != -1 && width != -1) { dimensions = new Dimensions(height, width); } } } finally { IOUtils.closeQuietly(is); } return dimensions; }  static Dimensions getDimensions(final File outFile);  }
@Test public void getDimensions_validJPGFile() throws Exception { Assertions.assertThat(JPGFileUtils.getDimensions(new File("src/test/resources/utils/jpg500x500.jpg"))) .isEqualTo(new Dimensions(500)); }
@Test public void getDimensions_invalidFile() throws Exception { Assertions.assertThat(JPGFileUtils.getDimensions(new File("src/test/resources/utils/png512x512.png"))).isNull(); }