View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.niofs;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.nio.file.FileSystem;
24  import java.nio.file.FileVisitResult;
25  import java.nio.file.Files;
26  import java.nio.file.LinkOption;
27  import java.nio.file.NoSuchFileException;
28  import java.nio.file.Path;
29  import java.nio.file.SimpleFileVisitor;
30  import java.nio.file.attribute.BasicFileAttributes;
31  import java.util.Arrays;
32  import java.util.concurrent.atomic.AtomicLong;
33  
34  import org.apache.logging.log4j.LogManager;
35  
36  /**
37   * @author Thomas Scheffler (yagee)
38   *
39   */
40  public abstract class MCRPathUtils {
41  
42      private MCRPathUtils() {
43  
44      }
45  
46      /**
47       * Returns requested {@link BasicFileAttributes} or null if file does not exist.
48       * 
49       * Same as {@link Files#readAttributes(Path, Class, LinkOption...)} without throwing {@link IOException}.
50       * 
51       * @param   path
52       *          the path to the file
53       * @param   type
54       *          the {@code Class} of the file attributes required
55       *          to read
56       * @param   options
57       *          options indicating how symbolic links are handled
58       *
59       * @return  the file attributes
60       */
61      public static <A extends BasicFileAttributes> A getAttributes(Path path, Class<A> type, LinkOption... options) {
62          try {
63              return Files.readAttributes(path, type, options);
64          } catch (NoSuchFileException | FileNotFoundException e) {
65              //we expect that file may not exist
66          } catch (IOException e) {
67              //any other IOException is catched
68              LogManager.getLogger(MCRPathUtils.class).info("Error while retrieving attributes of file: {}", path, e);
69          }
70          return null;
71      }
72  
73      public static Path getPath(FileSystem targetFS, String fileName) {
74          String[] nameComps = fileName.replace('\\', '/').split("/");
75          if (nameComps.length == 1) {
76              return targetFS.getPath(nameComps[0]);
77          } else {
78              return targetFS.getPath(nameComps[0], Arrays.copyOfRange(nameComps, 1, nameComps.length));
79          }
80  
81      }
82  
83      /**
84       * Returns the size of the path.
85       *
86       * If the path is a directory the size returned is the sum of all files found recursivly
87       * in this directory.
88       * @param p path of a file or directory
89       * @return the size of p in bytes
90       * @throws IOException underlaying IOException
91       */
92      public static long getSize(Path p) throws IOException {
93          AtomicLong size = new AtomicLong();
94          Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
95              @Override
96              public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
97                  size.addAndGet(attrs.size());
98                  return super.visitFile(file, attrs);
99              }
100         });
101         return size.get();
102     }
103 
104 }