bugfix
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / Utils.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.meviz.htmlindexer;
11
12 import java.awt.image.BufferedImage;
13 import java.io.BufferedReader;
14 import java.io.File;
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;
20
21 import javax.imageio.ImageIO;
22
23 import eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
24
25 public class Utils {
26
27         private static File lastLoadedFile;
28
29         private static BufferedImage lastLoadedBufferedImage;
30
31         /**
32          * Load image into {@link BufferedImage} and return it. Caches last loaded
33          * image to speed up subsequent loading attempts.
34          * 
35          * @throws ImageFormatError
36          * @throws IOException
37          */
38         public static BufferedImage getBufferedImage(final File file)
39                         throws ImageFormatError, IOException {
40                 if (file.equals(lastLoadedFile))
41                         return lastLoadedBufferedImage;
42
43                 System.out.println("Loading image: " + file.getPath());
44                 lastLoadedBufferedImage = ImageIO.read(file);
45                 lastLoadedFile = file;
46
47                 if (lastLoadedBufferedImage == null)
48                         throw new ImageFormatError("File: " + file
49                                         + " is not a valid image.");
50
51                 return lastLoadedBufferedImage;
52         }
53
54         public static File getLayoutIndexFile(final Layout layout,
55                         final File directoryToIndex) {
56
57                 final String indexFilePath = directoryToIndex.getAbsolutePath()
58                                 + "/index" + layout.getFileNameSuffix() + ".html";
59
60                 return new File(indexFilePath);
61         }
62
63         public static HashSet<Layout> getLayouts() {
64                 final HashSet<Layout> layouts = new HashSet<Layout>();
65                 layouts.add(new MixedLayout());
66                 return layouts;
67         }
68
69         public static String getStringCrcAsHex(final String input) {
70
71                 // create a new CRC-calculating object
72                 final CRC32 crc = new CRC32();
73
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())
77                         crc.update(b);
78
79                 // note use crc.value, not crc.getValue()
80                 final String hex = Integer.toHexString((int) crc.getValue())
81                                 .toUpperCase();
82
83                 // System.out.println("Input string: " + input);
84                 // System.out.println("Result: " + hex);
85
86                 return hex;
87         }
88
89         public static File getThumbnailsDirectory(final File directoryToIndex) {
90                 return new File(getThumbnailsDirectoryPath(directoryToIndex));
91         }
92
93         public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
94                 return directoryToIndex.getAbsolutePath() + "/"
95                                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
96         }
97
98         public static boolean isMevizGeneratedIndexFile(final File indexFile)
99                         throws FileNotFoundException, IOException {
100
101                 boolean isMevizFile = false;
102
103                 final FileReader fileReader = new FileReader(indexFile);
104                 final BufferedReader reader = new BufferedReader(fileReader);
105
106                 parseFile: {
107                         while (true) {
108                                 final String line = reader.readLine();
109
110                                 if (line == null)
111                                         break parseFile;
112
113                                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
114                                         isMevizFile = true;
115                                         break parseFile;
116                                 }
117                         }
118                 }
119
120                 reader.close();
121                 fileReader.close();
122                 return isMevizFile;
123         }
124
125 }