1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package es.accv.arangi.base.document;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.IOException;
27 import java.io.InputStream;
28
29 import org.apache.log4j.Logger;
30
31 import es.accv.arangi.base.algorithm.HashingAlgorithm;
32 import es.accv.arangi.base.exception.document.HashingException;
33 import es.accv.arangi.base.exception.document.InitDocumentException;
34
35
36
37
38
39
40
41 public class FileDocument extends Document {
42
43
44
45
46 Logger logger = Logger.getLogger(FileDocument.class);
47
48
49
50
51 private File file;
52
53
54
55
56
57
58
59 public FileDocument (File file) throws InitDocumentException {
60 logger.debug("[FileDocument]::Entrada::" + file);
61 if (file == null) {
62 logger.info("[FileDocument]::El fichero es nulo");
63 throw new InitDocumentException ("El fichero es nulo");
64 }
65 if (!file.exists()) {
66 logger.info("[FileDocument]::El fichero no existe en el sistema de archivos");
67 throw new InitDocumentException ("El fichero no existe en el sistema de archivos");
68 }
69
70 this.file = file;
71 }
72
73
74
75
76
77 public byte[] getHash() throws HashingException {
78 return getHash (HashingAlgorithm.getDefault());
79 }
80
81
82
83
84
85 public byte[] getHash(String hashingAlgorithm) throws HashingException {
86
87 logger.debug("[FileDocument.getHash]::Entrada::" + hashingAlgorithm);
88
89
90 InputStream is = null;
91 try {
92 is = new FileInputStream (this.file);
93 InputStreamDocument isd = new InputStreamDocument (is);
94 byte[] hash = isd.getHash(hashingAlgorithm);
95 logger.debug ("[FileDocument.getHash]::FIN::Devolviendo: " + hash);
96
97 return hash;
98
99 } catch (FileNotFoundException e) {
100 logger.info("[FileDocument.getHash]::El fichero no existe", e);
101 throw new HashingException ("El fichero no existe", e);
102 } finally {
103 if (is != null) {
104 try {
105 is.close();
106 } catch (IOException e) {
107 logger.info("[FileDocument.getHash]::El fichero no existe", e);
108 throw new HashingException ("No es posible cerrar el stream de lectura", e);
109 }
110 }
111 }
112 }
113
114
115
116
117
118 public InputStream getInputStream() {
119 try {
120 return new FileInputStream (file);
121 } catch (FileNotFoundException e) {
122 logger.info("[FileDocument.getInputStream]::No es posible obtener un stream de lectura al fichero", e);
123 return null;
124 }
125 }
126
127 }