+ /**
+ * Load image into {@link BufferedImage} and return it. Caches last loaded
+ * image to speed up subsequent loading attempts.
+ *
+ * @throws ImageFormatError
+ * @throws IOException
+ */
+ public static BufferedImage getBufferedImage(final File file)
+ throws ImageFormatError, IOException {
+ if (file.equals(lastLoadedFile))
+ return lastLoadedBufferedImage;
+
+ System.out.println("Loading image: " + file.getPath());
+ lastLoadedBufferedImage = ImageIO.read(file);
+ lastLoadedFile = file;
+
+ if (lastLoadedBufferedImage == null) {
+ System.out.println("Error reading image: " + file);
+ throw new ImageFormatError("File: " + file
+ + " is not a valid image.");
+
+ }
+
+ return lastLoadedBufferedImage;
+ }
+
+ public static File getLayoutIndexFile(final Layout layout,
+ final File directoryToIndex) {
+
+ final String indexFilePath = directoryToIndex.getAbsolutePath()
+ + "/index" + layout.getFileNameSuffix() + ".html";
+
+ return new File(indexFilePath);
+ }
+
+ public static HashSet<Layout> getLayouts() {
+ final HashSet<Layout> layouts = new HashSet<Layout>();
+ layouts.add(new MixedLayout());
+ return layouts;
+ }
+
+ public static String getStringCrcAsHex(final String input) {
+
+ // create a new CRC-calculating object
+ final CRC32 crc = new CRC32();
+
+ // loop, calculating CRC for each byte of the string
+ // There is no CRC16.update(byte[]) method.
+ for (final byte b : input.getBytes())
+ crc.update(b);
+
+ // note use crc.value, not crc.getValue()
+ final String hex = Integer.toHexString((int) crc.getValue())
+ .toUpperCase();
+
+ // System.out.println("Input string: " + input);
+ // System.out.println("Result: " + hex);
+
+ return hex;
+ }
+
+ public static File getThumbnailsDirectory(final File directoryToIndex) {
+ return new File(getThumbnailsDirectoryPath(directoryToIndex));
+ }
+
+ public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
+ return directoryToIndex.getAbsolutePath() + "/"
+ + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
+ }
+
+ public static boolean isMevizGeneratedIndexFile(final File indexFile)
+ throws IOException {
+
+ boolean isMevizFile = false;
+
+ final FileReader fileReader = new FileReader(indexFile);
+ final BufferedReader reader = new BufferedReader(fileReader);
+
+ parseFile:
+ {
+ while (true) {
+ final String line = reader.readLine();
+
+ if (line == null)
+ break parseFile;
+
+ if (line.contains(Constants.HTML_MAGIC_STRING)) {
+ isMevizFile = true;
+ break parseFile;
+ }
+ }
+ }
+
+ reader.close();
+ fileReader.close();
+ return isMevizFile;
+ }
+
+ public static String urlEncode(String string) {
+ if (string.startsWith("./"))
+ string = string.substring(2);
+
+ try {
+ return URLEncoder.encode(string, UTF_8).replace("+", "%20");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ }
+ }