@Test public void buildImageDataUriTest() throws Exception { String dataUri = URLUtils.buildDataURIFromStream("email.gif", loadStream(IMAGE_PATH)); assertEquals(IMAGE_DATA_URI, dataUri); }
public static String buildDataURIFromStream(final String fileName, final InputStream inputStream) throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final String contentType = guessContentType(fileName, inputStream); if (null != contentType) { final byte[] chunk = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(chunk)) > 0) { os.write(chunk, 0, bytesRead); } os.flush(); inputStream.close(); return "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray()); } else { throw new UnsupportedOperationException("Content type is undefined."); } }
URLUtils { public static String buildDataURIFromStream(final String fileName, final InputStream inputStream) throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final String contentType = guessContentType(fileName, inputStream); if (null != contentType) { final byte[] chunk = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(chunk)) > 0) { os.write(chunk, 0, bytesRead); } os.flush(); inputStream.close(); return "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray()); } else { throw new UnsupportedOperationException("Content type is undefined."); } } }
URLUtils { public static String buildDataURIFromStream(final String fileName, final InputStream inputStream) throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final String contentType = guessContentType(fileName, inputStream); if (null != contentType) { final byte[] chunk = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(chunk)) > 0) { os.write(chunk, 0, bytesRead); } os.flush(); inputStream.close(); return "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray()); } else { throw new UnsupportedOperationException("Content type is undefined."); } }  }
URLUtils { public static String buildDataURIFromStream(final String fileName, final InputStream inputStream) throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final String contentType = guessContentType(fileName, inputStream); if (null != contentType) { final byte[] chunk = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(chunk)) > 0) { os.write(chunk, 0, bytesRead); } os.flush(); inputStream.close(); return "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray()); } else { throw new UnsupportedOperationException("Content type is undefined."); } }  static byte[] readBytesFromURL(final String uri); static byte[] readBytes(final InputStream is); static String readFromURL(final String uri); static String readFromURL(final URL url); static String buildDataURIFromURL(final String url); static String buildDataURIFromURL(final URL url); static String buildDataURIFromStream(final String fileName,
                                                final InputStream inputStream); static String guessContentType(final String fileName,
                                          final InputStream stream); }
URLUtils { public static String buildDataURIFromStream(final String fileName, final InputStream inputStream) throws Exception { final ByteArrayOutputStream os = new ByteArrayOutputStream(); final String contentType = guessContentType(fileName, inputStream); if (null != contentType) { final byte[] chunk = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(chunk)) > 0) { os.write(chunk, 0, bytesRead); } os.flush(); inputStream.close(); return "data:" + contentType + ";base64," + Base64.getEncoder().encodeToString(os.toByteArray()); } else { throw new UnsupportedOperationException("Content type is undefined."); } }  static byte[] readBytesFromURL(final String uri); static byte[] readBytes(final InputStream is); static String readFromURL(final String uri); static String readFromURL(final URL url); static String buildDataURIFromURL(final String url); static String buildDataURIFromURL(final URL url); static String buildDataURIFromStream(final String fileName,
                                                final InputStream inputStream); static String guessContentType(final String fileName,
                                          final InputStream stream);  }