class 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;
    }

}