Updated copyright message.
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / metadata / MetadadaHelper.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012 -- 2018, 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.metadata;
11
12 import eu.svjatoslav.commons.file.IOHelper;
13 import org.apache.log4j.Logger;
14
15 import java.io.*;
16
17 public class MetadadaHelper {
18
19     private static final Logger logger = Logger.getLogger(MetadadaHelper.class);
20
21     /**
22      * Load previously generated or initialize new directory metadata.
23      */
24     public static DirectoryMetadata initDirectoryMetadata(
25             final File metadataFile) {
26
27         if (!metadataFile.exists())
28             return new DirectoryMetadata();
29         else
30             try {
31
32                 final ObjectInputStream in = new ObjectInputStream(
33                         new FileInputStream(metadataFile));
34                 final DirectoryMetadata directory = (DirectoryMetadata) in
35                         .readObject();
36                 in.close();
37
38                 return directory;
39
40             } catch (final Exception exception) {
41                 logger.error("Cannot load existing directory metadata.",
42                         exception);
43
44                 return new DirectoryMetadata();
45             }
46     }
47
48     public static void saveDirectoryMetadata(final File persistanceFile,
49                                              final DirectoryMetadata directory) throws IOException {
50         // serialize directory metadata into byte array
51         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
52
53         final ObjectOutputStream out = new ObjectOutputStream(
54                 byteArrayOutputStream);
55         out.writeObject(directory);
56         out.close();
57
58         // store byte array into file
59         IOHelper.saveToFile(persistanceFile,
60                 byteArrayOutputStream.toByteArray());
61     }
62 }