Fixed bug where 0 byte pictures were treated incorrectly.
[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         /**
28          * Load image into {@link BufferedImage} and return it. Caches last loaded
29          * image to speed up subsequent loading attempts.
30          *
31          * @throws ImageFormatError
32          * @throws IOException
33          */
34         public static BufferedImage getBufferedImage(final File file)
35                         throws ImageFormatError, IOException {
36                 if (file.equals(lastLoadedFile))
37                         return lastLoadedBufferedImage;
38
39                 System.out.println("Loading image: " + file.getPath());
40                 lastLoadedBufferedImage = ImageIO.read(file);
41                 lastLoadedFile = file;
42
43                 if (lastLoadedBufferedImage == null) {
44                         System.out.println("Error reading image: " + file);
45                         throw new ImageFormatError("File: " + file
46                                         + " is not a valid image.");
47
48                 }
49
50                 return lastLoadedBufferedImage;
51         }
52
53         public static File getLayoutIndexFile(final Layout layout,
54                         final File directoryToIndex) {
55
56                 final String indexFilePath = directoryToIndex.getAbsolutePath()
57                                 + "/index" + layout.getFileNameSuffix() + ".html";
58
59                 return new File(indexFilePath);
60         }
61
62         public static HashSet<Layout> getLayouts() {
63                 final HashSet<Layout> layouts = new HashSet<Layout>();
64                 layouts.add(new MixedLayout());
65                 return layouts;
66         }
67
68         public static String getStringCrcAsHex(final String input) {
69
70                 // create a new CRC-calculating object
71                 final CRC32 crc = new CRC32();
72
73                 // loop, calculating CRC for each byte of the string
74                 // There is no CRC16.update(byte[]) method.
75                 for (final byte b : input.getBytes())
76                         crc.update(b);
77
78                 // note use crc.value, not crc.getValue()
79                 final String hex = Integer.toHexString((int) crc.getValue())
80                                 .toUpperCase();
81
82                 // System.out.println("Input string: " + input);
83                 // System.out.println("Result: " + hex);
84
85                 return hex;
86         }
87
88         public static File getThumbnailsDirectory(final File directoryToIndex) {
89                 return new File(getThumbnailsDirectoryPath(directoryToIndex));
90         }
91
92         public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
93                 return directoryToIndex.getAbsolutePath() + "/"
94                                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
95         }
96
97         public static boolean isMevizGeneratedIndexFile(final File indexFile)
98                         throws FileNotFoundException, IOException {
99
100                 boolean isMevizFile = false;
101
102                 final FileReader fileReader = new FileReader(indexFile);
103                 final BufferedReader reader = new BufferedReader(fileReader);
104
105                 parseFile: {
106                         while (true) {
107                                 final String line = reader.readLine();
108
109                                 if (line == null)
110                                         break parseFile;
111
112                                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
113                                         isMevizFile = true;
114                                         break parseFile;
115                                 }
116                         }
117                 }
118
119                 reader.close();
120                 fileReader.close();
121                 return isMevizFile;
122         }
123
124         private static File lastLoadedFile;
125
126         private static BufferedImage lastLoadedBufferedImage;
127
128 }