### Question:
ImageEncoder { public InputStream encodeImage(byte[] imageBytes) { String formatName = determineImageFormat(imageBytes); InputStream inputStream = new ByteArrayInputStream(imageBytes); if (StringUtils.isNotEmpty(formatName)) { try { InputStream ddsStream = Image.getDDSImageStream(inputStream, formatName, imageBytes.length, null); inputStream.close(); inputStream = ddsStream; } catch (ImageFormatUnknownException | IOException e) { LOGGER.error(e, e); inputStream = new ByteArrayInputStream(imageBytes); } } return inputStream; }  InputStream encodeImage(byte[] imageBytes);  }### Answer:
@Test @Ignore public void testEncodeJpg() throws IOException, ImageFormatUnknownException { BufferedImage image = ImageUtil.BROKEN_IMAGE; ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(image, "jpg", output); byte[] imageBytes = output.toByteArray(); ImageEncoder encoder = new ImageEncoder(); InputStream ddsImage = encoder.encodeImage(imageBytes); InputStream expectedStream = Image.getDDSImageStream(new ByteArrayInputStream(imageBytes), "jpg", imageBytes.length, null); StreamReader reader = new StreamReader(ddsImage); ByteArrayOutputStream actual = new ByteArrayOutputStream(); reader.copyStream(actual); reader = new StreamReader(expectedStream); ByteArrayOutputStream expected = new ByteArrayOutputStream(); reader.copyStream(expected); assertTrue(expected.size() > 0); assertArrayEquals(expected.toByteArray(), actual.toByteArray()); }
@Test @Ignore public void testEncodePng() throws IOException, ImageFormatUnknownException { BufferedImage image = ImageUtil.BROKEN_IMAGE; ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(image, "png", output); byte[] imageBytes = output.toByteArray(); ImageEncoder encoder = new ImageEncoder(); InputStream ddsImage = encoder.encodeImage(imageBytes); InputStream expectedStream = Image.getDDSImageStream(new ByteArrayInputStream(imageBytes), "png", imageBytes.length, null); StreamReader reader = new StreamReader(ddsImage); ByteArrayOutputStream actual = new ByteArrayOutputStream(); reader.copyStream(actual); reader = new StreamReader(expectedStream); ByteArrayOutputStream expected = new ByteArrayOutputStream(); reader.copyStream(expected); assertTrue(expected.size() > 0); assertArrayEquals(expected.toByteArray(), actual.toByteArray()); }
@Test public void testEncodeUnrecognized() throws IOException { byte[] imageBytes = new byte[] { 1, 2, 3, 4 }; ImageEncoder encoder = new ImageEncoder(); InputStream ddsImage = encoder.encodeImage(imageBytes); StreamReader reader = new StreamReader(ddsImage); ByteArrayOutputStream actual = new ByteArrayOutputStream(); reader.copyStream(actual); assertArrayEquals(imageBytes, actual.toByteArray()); }