Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.lib / src / main / java / org / gvsig / fmap / dal / store / simplereader / SimpleReaderUtils.java @ 47784

History | View | Annotate | Download (2.68 KB)

1 47636 fdiaz
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.simplereader;
7
8
import java.io.File;
9
import java.io.FileInputStream;
10
import java.io.FileNotFoundException;
11
import java.io.InputStream;
12
import java.io.InputStreamReader;
13
import java.nio.charset.Charset;
14 47665 fdiaz
import java.util.Objects;
15
import org.apache.commons.io.FileUtils;
16 47636 fdiaz
import org.apache.commons.io.input.BOMInputStream;
17
import org.apache.commons.lang3.StringUtils;
18
import org.slf4j.Logger;
19
import org.slf4j.LoggerFactory;
20
21
/**
22
 *
23
 * @author fdiaz
24
 */
25
public class SimpleReaderUtils {
26
27
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReaderUtils.class);
28
29
    public static InputStreamReader openFile(File f, String charsetName) throws FileNotFoundException {
30
        String fullFileName = f==null? "NULL":f.getAbsolutePath();
31
        Charset charset = Charset.defaultCharset();
32
        InputStream fis = new BOMInputStream(new FileInputStream(f));
33
        if (StringUtils.isNotBlank(charsetName)) {
34
            if (Charset.isSupported(charsetName)) {
35
                try {
36
                    charset = Charset.forName(charsetName);
37
                } catch (Throwable th) {
38
                    LOGGER.warn("Can't use charset '" + charsetName + "' for read csv '" + fullFileName + "'.", th);
39
                }
40
            } else {
41
                LOGGER.warn("charset '" + charsetName + "' not supported for read csv '" + fullFileName + "'.");
42
            }
43
        }
44
        InputStreamReader isr = new InputStreamReader(fis, charset);
45
        return isr;
46
    }
47
48 47665 fdiaz
    public static boolean isFileNewer(File file, File fileReference){
49
        if(file == null || fileReference == null){
50
            return false;
51
        }
52
        long lastModified = fileReference.lastModified();
53
        // Por si el archivo nos lo ha enviado el Doctor Who desde el futuro
54
        long now = System.currentTimeMillis();
55
        if(now < lastModified){
56
            LOGGER.warn("The file date is future " + Objects.toString(fileReference));
57
            lastModified = now;
58
        }
59
        return FileUtils.isFileNewer(file, lastModified);
60
    }
61 47636 fdiaz
62 47665 fdiaz
    public static boolean isFileNewer(File file, long reference){
63
        if(file == null){
64
            return false;
65
        }
66
        // Por si el archivo nos lo ha enviado el Doctor Who desde el futuro
67
        long now = System.currentTimeMillis();
68
        if(now < reference){
69
            LOGGER.warn("The reference file date is future " + Objects.toString(file));
70
            reference = now;
71
        }
72
        return FileUtils.isFileNewer(file, reference);
73
    }
74 47636 fdiaz
}