2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.htmlindexer;
12 import java.awt.image.BufferedImage;
13 import java.io.BufferedReader;
15 import java.io.FileNotFoundException;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.util.HashSet;
19 import java.util.zip.CRC32;
21 import javax.imageio.ImageIO;
23 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
27 private static File lastLoadedFile;
29 private static BufferedImage lastLoadedBufferedImage;
32 * Load image into {@link BufferedImage} and return it. Caches last loaded
33 * image to speed up subsequent loading attempts.
35 * @throws ImageFormatError
38 public static BufferedImage getBufferedImage(final File file)
39 throws ImageFormatError, IOException {
40 if (file.equals(lastLoadedFile))
41 return lastLoadedBufferedImage;
43 System.out.println("Loading image: " + file.getPath());
44 lastLoadedBufferedImage = ImageIO.read(file);
45 lastLoadedFile = file;
47 if (lastLoadedBufferedImage == null)
48 throw new ImageFormatError("File: " + file
49 + " is not a valid image.");
51 return lastLoadedBufferedImage;
54 public static File getLayoutIndexFile(final Layout layout,
55 final File directoryToIndex) {
57 final String indexFilePath = directoryToIndex.getAbsolutePath()
58 + "/index" + layout.getFileNameSuffix() + ".html";
60 return new File(indexFilePath);
63 public static HashSet<Layout> getLayouts() {
64 final HashSet<Layout> layouts = new HashSet<Layout>();
65 layouts.add(new MixedLayout());
69 public static String getStringCrcAsHex(final String input) {
71 // create a new CRC-calculating object
72 final CRC32 crc = new CRC32();
74 // loop, calculating CRC for each byte of the string
75 // There is no CRC16.update(byte[]) method.
76 for (final byte b : input.getBytes())
79 // note use crc.value, not crc.getValue()
80 final String hex = Integer.toHexString((int) crc.getValue())
83 // System.out.println("Input string: " + input);
84 // System.out.println("Result: " + hex);
89 public static File getThumbnailsDirectory(final File directoryToIndex) {
90 return new File(getThumbnailsDirectoryPath(directoryToIndex));
93 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
94 return directoryToIndex.getAbsolutePath() + "/"
95 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
98 public static boolean isMevizGeneratedIndexFile(final File indexFile)
99 throws FileNotFoundException, IOException {
101 boolean isMevizFile = false;
103 final FileReader fileReader = new FileReader(indexFile);
104 final BufferedReader reader = new BufferedReader(fileReader);
108 final String line = reader.readLine();
113 if (line.contains(Constants.HTML_MAGIC_STRING)) {