Minor housekeeping.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / metadata / MetadadaHelper.java
1 /*
2  * Meviz - Various tools collection to work with multimedia. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5
6
7 package eu.svjatoslav.meviz.htmlindexer.metadata;
8
9 import eu.svjatoslav.commons.file.IOHelper;
10 import org.apache.log4j.Logger;
11
12 import java.io.*;
13
14 public class MetadadaHelper {
15
16     private static final Logger logger = Logger.getLogger(MetadadaHelper.class);
17
18     /**
19      * Load previously generated or initialize new directory metadata.
20      */
21     public static DirectoryMetadata initDirectoryMetadata(
22             final File metadataFile) {
23
24         if (!metadataFile.exists())
25             return new DirectoryMetadata();
26         else
27             try {
28
29                 final ObjectInputStream in = new ObjectInputStream(
30                         new FileInputStream(metadataFile));
31                 final DirectoryMetadata directory = (DirectoryMetadata) in
32                         .readObject();
33                 in.close();
34
35                 return directory;
36
37             } catch (final Exception exception) {
38                 logger.error("Cannot load existing directory metadata.",
39                         exception);
40
41                 return new DirectoryMetadata();
42             }
43     }
44
45     public static void saveDirectoryMetadata(final File persistanceFile,
46                                              final DirectoryMetadata directory) throws IOException {
47         // serialize directory metadata into byte array
48         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
49
50         final ObjectOutputStream out = new ObjectOutputStream(
51                 byteArrayOutputStream);
52         out.writeObject(directory);
53         out.close();
54
55         // store byte array into file
56         IOHelper.saveToFile(persistanceFile,
57                 byteArrayOutputStream.toByteArray());
58     }
59 }