handle files in alphabetical order
[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, 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 java.io.ByteArrayOutputStream;
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18
19 import org.apache.log4j.Logger;
20
21 import eu.svjatoslav.commons.file.IOHelper;
22
23 public class MetadadaHelper {
24
25         public static final Logger logger = Logger.getLogger(MetadadaHelper.class);
26
27         /**
28          * Load previously generated or initialize new directory metadata.
29          */
30         public static DirectoryMetadata initDirectoryMetadata(
31                         final File metadataFile) {
32
33                 if (!metadataFile.exists())
34                         return new DirectoryMetadata();
35                 else
36                         try {
37
38                                 final ObjectInputStream in = new ObjectInputStream(
39                                                 new FileInputStream(metadataFile));
40                                 final DirectoryMetadata directory = (DirectoryMetadata) in
41                                                 .readObject();
42                                 in.close();
43
44                                 return directory;
45
46                         } catch (final Exception exception) {
47                                 logger.error("Cannot load existing directory metadata.",
48                                                 exception);
49
50                                 return new DirectoryMetadata();
51                         }
52         }
53
54         public static void saveDirectoryMetadata(final File persistanceFile,
55                         final DirectoryMetadata directory) throws IOException {
56                 // serialize directory metadata into byte array
57                 final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
58
59                 final ObjectOutputStream out = new ObjectOutputStream(
60                                 byteArrayOutputStream);
61                 out.writeObject(directory);
62                 out.close();
63
64                 // store byte array into file
65                 IOHelper.saveToFile(persistanceFile,
66                                 byteArrayOutputStream.toByteArray());
67         }
68 }