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 System.out.println("Error reading image: " + file);
49 throw new ImageFormatError("File: " + file
50 + " is not a valid image.");
54 return lastLoadedBufferedImage;
57 public static File getLayoutIndexFile(final Layout layout,
58 final File directoryToIndex) {
60 final String indexFilePath = directoryToIndex.getAbsolutePath()
61 + "/index" + layout.getFileNameSuffix() + ".html";
63 return new File(indexFilePath);
66 public static HashSet<Layout> getLayouts() {
67 final HashSet<Layout> layouts = new HashSet<Layout>();
68 layouts.add(new MixedLayout());
72 public static String getStringCrcAsHex(final String input) {
74 // create a new CRC-calculating object
75 final CRC32 crc = new CRC32();
77 // loop, calculating CRC for each byte of the string
78 // There is no CRC16.update(byte[]) method.
79 for (final byte b : input.getBytes())
82 // note use crc.value, not crc.getValue()
83 final String hex = Integer.toHexString((int) crc.getValue())
86 // System.out.println("Input string: " + input);
87 // System.out.println("Result: " + hex);
92 public static File getThumbnailsDirectory(final File directoryToIndex) {
93 return new File(getThumbnailsDirectoryPath(directoryToIndex));
96 public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
97 return directoryToIndex.getAbsolutePath() + "/"
98 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
101 public static boolean isMevizGeneratedIndexFile(final File indexFile)
102 throws FileNotFoundException, IOException {
104 boolean isMevizFile = false;
106 final FileReader fileReader = new FileReader(indexFile);
107 final BufferedReader reader = new BufferedReader(fileReader);
111 final String line = reader.readLine();
116 if (line.contains(Constants.HTML_MAGIC_STRING)) {