2 * jSite - FileScanner.java - Copyright © 2006–2012 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.gui;
22 import java.io.FileFilter;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.security.DigestOutputStream;
28 import java.security.MessageDigest;
29 import java.security.NoSuchAlgorithmException;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.logging.Level;
34 import java.util.logging.Logger;
36 import de.todesbaum.jsite.application.Project;
37 import de.todesbaum.jsite.i18n.I18n;
38 import de.todesbaum.util.io.Closer;
39 import de.todesbaum.util.io.StreamCopier;
42 * Scans the local path of a project anychronously and returns the list of found
45 * @see Project#getLocalPath()
46 * @see FileScannerListener#fileScannerFinished(FileScanner)
47 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
49 public class FileScanner implements Runnable {
52 private final static Logger logger = Logger.getLogger(FileScanner.class.getName());
54 /** The list of listeners. */
55 private final List<FileScannerListener> fileScannerListeners = new ArrayList<FileScannerListener>();
57 /** The project to scan. */
58 private final Project project;
60 /** The list of found files. */
61 private List<ScannedFile> files;
63 /** Wether there was an error. */
64 private boolean error = false;
67 * Creates a new file scanner for the given project.
70 * The project whose files to scan
72 public FileScanner(Project project) {
73 this.project = project;
77 * Adds the given listener to the list of listeners.
79 * @param fileScannerListener
82 public void addFileScannerListener(FileScannerListener fileScannerListener) {
83 fileScannerListeners.add(fileScannerListener);
87 * Removes the given listener from the list of listeners.
89 * @param fileScannerListener
90 * The listener to remove
92 public void removeFileScannerListener(FileScannerListener fileScannerListener) {
93 fileScannerListeners.remove(fileScannerListener);
97 * Notifies all listeners that the file scan finished.
99 protected void fireFileScannerFinished() {
100 for (FileScannerListener fileScannerListener : new ArrayList<FileScannerListener>(fileScannerListeners)) {
101 fileScannerListener.fileScannerFinished(this);
108 * Scans all available files in the project’s local path and emits an event
111 * @see FileScannerListener#fileScannerFinished(FileScanner)
115 files = new ArrayList<ScannedFile>();
118 scanFiles(new File(project.getLocalPath()), files);
119 Collections.sort(files);
120 } catch (IOException ioe1) {
123 fireFileScannerFinished();
127 * Returns whether there was an error scanning for files.
129 * @return <code>true</code> if there was an error, <code>false</code>
132 public boolean isError() {
137 * Returns the list of found files.
139 * @return The list of found files
141 public List<ScannedFile> getFiles() {
146 * Recursively scans a directory and adds all found files to the given list.
149 * The directory to scan
151 * The list to which to add the found files
152 * @throws IOException
153 * if an I/O error occurs
155 private void scanFiles(File rootDir, List<ScannedFile> fileList) throws IOException {
156 File[] files = rootDir.listFiles(new FileFilter() {
159 @SuppressWarnings("synthetic-access")
160 public boolean accept(File file) {
161 return !project.isIgnoreHiddenFiles() || !file.isHidden();
165 throw new IOException(I18n.getMessage("jsite.file-scanner.can-not-read-directory"));
167 for (File file : files) {
168 if (file.isDirectory()) {
169 scanFiles(file, fileList);
172 String filename = project.shortenFilename(file).replace('\\', '/');
173 String hash = hashFile(project.getLocalPath(), filename);
174 fileList.add(new ScannedFile(filename, hash));
179 * Hashes the given file.
182 * The path of the project
184 * The name of the file, relative to the project path
185 * @return The hash of the file
187 @SuppressWarnings("synthetic-access")
188 private static String hashFile(String path, String filename) {
189 InputStream fileInputStream = null;
190 DigestOutputStream digestOutputStream = null;
191 File file = new File(path, filename);
193 fileInputStream = new FileInputStream(file);
194 digestOutputStream = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance("SHA-256"));
195 StreamCopier.copy(fileInputStream, digestOutputStream, file.length());
196 return toHex(digestOutputStream.getMessageDigest().digest());
197 } catch (NoSuchAlgorithmException nsae1) {
198 logger.log(Level.WARNING, "Could not get SHA-256 digest!", nsae1);
199 } catch (IOException ioe1) {
200 logger.log(Level.WARNING, "Could not read file!", ioe1);
202 Closer.close(digestOutputStream);
203 Closer.close(fileInputStream);
205 return toHex(new byte[32]);
209 * Converts the given byte array into a hexadecimal string.
212 * The array to convert
213 * @return The hexadecimal string
215 private static String toHex(byte[] array) {
216 StringBuilder hexString = new StringBuilder(array.length * 2);
217 for (byte b : array) {
218 hexString.append("0123456789abcdef".charAt((b >>> 4) & 0x0f)).append("0123456789abcdef".charAt(b & 0xf));
220 return hexString.toString();
224 * {@link OutputStream} that discards all written bytes.
226 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
228 private static class NullOutputStream extends OutputStream {
234 public void write(int b) {
242 public void write(byte[] b) {
250 public void write(byte[] b, int off, int len) {
257 * Container for a scanned file, consisting of the name of the file and its
260 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
262 public static class ScannedFile implements Comparable<ScannedFile> {
264 /** The name of the file. */
265 private final String filename;
267 /** The hash of the file. */
268 private final String hash;
271 * Creates a new scanned file.
274 * The name of the file
276 * The hash of the file
278 public ScannedFile(String filename, String hash) {
279 this.filename = filename;
288 * Returns the name of the file.
290 * @return The name of the file
292 public String getFilename() {
297 * Returns the hash of the file.
299 * @return The hash of the file
301 public String getHash() {
313 public int hashCode() {
314 return filename.hashCode();
321 public boolean equals(Object obj) {
322 return filename.equals(obj);
329 public String toString() {
334 // COMPARABLE METHODS
341 public int compareTo(ScannedFile scannedFile) {
342 return filename.compareTo(scannedFile.filename);