HtmlReader.java 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // This was added by Jason MacDuffie in 2014
  2. import java.io.*;
  3. import java.nio.charset.*;
  4. import java.nio.file.*;
  5. import java.net.URL;
  6. import java.awt.*;
  7. import java.awt.image.BufferedImage;
  8. import javax.imageio.*;
  9. import javax.swing.*;
  10. public class HtmlReader {
  11. public static String readLocalFile(String filepath) {
  12. URL dirpath = HtmlReader.class.getProtectionDomain().getCodeSource().getLocation();
  13. String localPath = dirpath.toString().replace("file:", "").replace("%20", " ").replace("CipherBuddy.jar", "") + filepath;
  14. String content = null;
  15. try {
  16. content = readFile(localPath, Charset.defaultCharset());
  17. } catch (IOException e) { System.err.println(e); }
  18. return "<html><body style='width: 200 px'" + content + "</html>";
  19. }
  20. public static Image readLocalImage(String filepath) {
  21. URL dirpath = HtmlReader.class.getProtectionDomain().getCodeSource().getLocation();
  22. String localpath = dirpath.toString().replace("file:", "").replace("%20", " ").replace("CipherBuddy.jar", "") + filepath;
  23. BufferedImage bitmap = null;
  24. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  25. try {
  26. bitmap = ImageIO.read(new File(localpath));
  27. ImageIO.write(bitmap, "PNG", bytes);
  28. } catch (IOException e) { System.err.println(e); }
  29. return (new ImageIcon(bytes.toByteArray())).getImage();
  30. }
  31. public static String readFile(String path, Charset encoding) throws IOException {
  32. byte[] encoded = Files.readAllBytes(Paths.get(path));
  33. return new String(encoded, encoding);
  34. }
  35. }