Moved to Java 8. Code cleanup.
[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 eu.svjatoslav.meviz.htmlindexer.layouts.MixedLayout;
13
14 import javax.imageio.ImageIO;
15 import java.awt.image.BufferedImage;
16 import java.io.*;
17 import java.net.URLEncoder;
18 import java.util.HashSet;
19 import java.util.zip.CRC32;
20
21 public class Utils {
22
23     private static final String UTF_8 = "UTF-8";
24     private static File lastLoadedFile;
25     private static BufferedImage lastLoadedBufferedImage;
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<>();
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
80         return Integer.toHexString((int) crc.getValue())
81                 .toUpperCase();
82     }
83
84     public static File getThumbnailsDirectory(final File directoryToIndex) {
85         return new File(getThumbnailsDirectoryPath(directoryToIndex));
86     }
87
88     public static String getThumbnailsDirectoryPath(final File directoryToIndex) {
89         return directoryToIndex.getAbsolutePath() + "/"
90                 + Constants.THUMBNAILS_DIRECTORY_NAME + "/";
91     }
92
93     public static boolean isMevizGeneratedIndexFile(final File indexFile)
94             throws IOException {
95
96         boolean isMevizFile = false;
97
98         final FileReader fileReader = new FileReader(indexFile);
99         final BufferedReader reader = new BufferedReader(fileReader);
100
101         parseFile:
102         {
103             while (true) {
104                 final String line = reader.readLine();
105
106                 if (line == null)
107                     break parseFile;
108
109                 if (line.contains(Constants.HTML_MAGIC_STRING)) {
110                     isMevizFile = true;
111                     break parseFile;
112                 }
113             }
114         }
115
116         reader.close();
117         fileReader.close();
118         return isMevizFile;
119     }
120
121     public static String urlEncode(String string) {
122         if (string.startsWith("./"))
123             string = string.substring(2);
124
125         try {
126             return URLEncoder.encode(string, UTF_8).replace("+", "%20");
127         } catch (UnsupportedEncodingException e) {
128             throw new RuntimeException(e);
129         }
130     }
131 }