aboutsummaryrefslogtreecommitdiff
path: root/subsonic-main/src/main/java/net/sourceforge/subsonic/util/FileUtil.java
blob: e91758ef96048fe91f26c633def86da3b5af251b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package net.sourceforge.subsonic.util;

import java.io.Closeable;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Arrays;

import net.sourceforge.subsonic.Logger;

/**
 * Miscellaneous file utility methods.
 *
 * @author Sindre Mehus
 */
public final class FileUtil {

    private static final Logger LOG = Logger.getLogger(FileUtil.class);

    /**
     * Disallow external instantiation.
     */
    private FileUtil() {
    }

    public static boolean isFile(final File file) {
        return timed(new FileTask<Boolean>("isFile", file) {
            @Override
            public Boolean execute() {
                return file.isFile();
            }
        });
    }

    public static boolean isDirectory(final File file) {
        return timed(new FileTask<Boolean>("isDirectory", file) {
            @Override
            public Boolean execute() {
                return file.isDirectory();
            }
        });
    }

    public static boolean exists(final File file) {
        return timed(new FileTask<Boolean>("exists", file) {
            @Override
            public Boolean execute() {
                return file.exists();
            }
        });
    }

    public static long lastModified(final File file) {
        return timed(new FileTask<Long>("lastModified", file) {
            @Override
            public Long execute() {
                return file.lastModified();
            }
        });
    }

    public static long length(final File file) {
        return timed(new FileTask<Long>("length", file) {
            @Override
            public Long execute() {
                return file.length();
            }
        });
    }

    /**
     * Similar to {@link File#listFiles()}, but never returns null.
     * Instead a warning is logged, and an empty array is returned.
     */
    public static File[] listFiles(final File dir) {
        File[] files = timed(new FileTask<File[]>("listFiles", dir) {
            @Override
            public File[] execute() {
                return dir.listFiles();
            }
        });

        if (files == null) {
            LOG.warn("Failed to list children for " + dir.getPath());
            return new File[0];
        }
        return files;
    }

    /**
     * Similar to {@link File#listFiles(FilenameFilter)}, but never returns null.
     * Instead a warning is logged, and an empty array is returned.
     */
    public static File[] listFiles(final File dir, final FilenameFilter filter, boolean sort) {
        File[] files = timed(new FileTask<File[]>("listFiles2", dir) {
            @Override
            public File[] execute() {
                return dir.listFiles(filter);
            }
        });
        if (files == null) {
            LOG.warn("Failed to list children for " + dir.getPath());
            return new File[0];
        }
        if (sort) {
            Arrays.sort(files);
        }
        return files;
    }

    /**
     * Returns a short path for the given file.  The path consists of the name
     * of the parent directory and the given file.
     */
    public static String getShortPath(File file) {
        if (file == null) {
            return null;
        }
        File parent = file.getParentFile();
        if (parent == null) {
            return file.getName();
        }
        return parent.getName() + File.separator + file.getName();
    }

    /**
     * Closes the "closable", ignoring any excepetions.
     *
     * @param closeable The Closable to close, may be {@code null}.
     */
    public static void closeQuietly(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException e) {
                // Ignored
            }
        }
    }

    private static <T> T timed(FileTask<T> task) {
//        long t0 = System.nanoTime();
//        try {
            return task.execute();
//        } finally {
//            long t1 = System.nanoTime();
//            LOG.debug((t1 - t0) / 1000L + " microsec, " + task);
//        }
    }

    private abstract static class FileTask<T> {

        private final String name;
        private final File file;

        public FileTask(String name, File file) {
            this.name = name;
            this.file = file;
        }

        public abstract T execute();

        @Override
        public String toString() {
            return name + ", " + file;
        }
    }
}