View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.util;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.nio.charset.Charset;
22  import java.util.Properties;
23  
24  import org.apache.commons.vfs2.FileContent;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileUtil;
28  import org.apache.commons.vfs2.impl.DecoratedFileObject;
29  import org.apache.commons.vfs2.provider.AbstractFileObject;
30  
31  /**
32   * Utility methods for {@link FileObject}.
33   */
34  public final class FileObjectUtils {
35  
36      /**
37       * Null-safe call to {@link FileObject#exists()}.
38       *
39       * @param fileObject the file object to test, may be null.
40       * @return false if {@code fileObject} is null, otherwise, see {@link FileObject#exists()}.
41       * @throws FileSystemException On error determining if this file exists.
42       * @since 2.4
43       */
44      public static boolean exists(final FileObject fileObject) throws FileSystemException {
45          return fileObject != null && fileObject.exists();
46      }
47  
48      /**
49       * Gets access to the base object even if decorated.
50       *
51       * @param fileObject The FileObject.
52       * @return The decorated FileObject or null.
53       * @throws FileSystemException if an error occurs.
54       */
55      public static AbstractFileObject getAbstractFileObject(final FileObject fileObject) throws FileSystemException {
56          Object searchObject = fileObject;
57          while (searchObject instanceof DecoratedFileObject) {
58              searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
59          }
60          if (searchObject instanceof AbstractFileObject) {
61              return (AbstractFileObject) searchObject;
62          }
63          if (searchObject == null) {
64              return null;
65          }
66  
67          throw new FileSystemException("vfs.util/find-abstract-file-object.error",
68                  fileObject == null ? "null" : fileObject.getClass().getName());
69      }
70  
71      /**
72       * Returns the content of a file as a String.
73       *
74       * @param file The file to get the content of.
75       * @param charset The file character set, may be null.
76       * @return The content as a byte array.
77       * @throws IOException if the file content cannot be accessed.
78       * @since 2.4
79       */
80      public static String getContentAsString(final FileObject file, final Charset charset) throws IOException {
81          try (final FileContent content = file.getContent()) {
82              return content.getString(charset);
83          }
84      }
85  
86      /**
87       * Returns the content of a file as a String.
88       *
89       * @param file The file to get the content of.
90       * @param charset The file character set, may be null.
91       * @return The content as a byte array.
92       * @throws IOException if the file content cannot be accessed.
93       * @since 2.4
94       */
95      public static String getContentAsString(final FileObject file, final String charset) throws IOException {
96          try (final FileContent content = file.getContent()) {
97              return content.getString(charset);
98          }
99      }
100 
101     /**
102      * Checks if the given FileObject is instance of given class argument.
103      *
104      * @param fileObject  The FileObject.
105      * @param wantedClass The Class to check.
106      * @return true if fileObject is an instance of the specified Class.
107      * @throws FileSystemException if an error occurs.
108      */
109     public static boolean isInstanceOf(final FileObject fileObject, final Class<?> wantedClass)
110             throws FileSystemException {
111         Object searchObject = fileObject;
112         while (searchObject instanceof DecoratedFileObject) {
113             if (wantedClass.isInstance(searchObject)) {
114                 return true;
115             }
116 
117             searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
118         }
119 
120         if (wantedClass.isInstance(searchObject)) {
121             return true;
122         }
123 
124         return false;
125     }
126 
127     /**
128      * Reads the given file into a new {@link Properties}.
129      *
130      * @param fileObject the file to read
131      * @return a new {@link Properties}.
132      * @throws IOException On error getting this file's content.
133      * @throws FileSystemException On error getting this file's content.
134      * @throws IOException On error getting this file's content.
135      * @since 2.4
136      */
137     public static Properties readProperties(final FileObject fileObject) throws FileSystemException, IOException {
138         return readProperties(fileObject, new Properties());
139     }
140 
141     /**
142      * Reads the given file into a new given {@link Properties}.
143      *
144      * @param fileObject the file to read
145      * @param properties the destination
146      * @return a new {@link Properties}.
147      * @throws FileSystemException On error getting this file's content.
148      * @throws IOException On error getting this file's content.
149      * @since 2.4
150      */
151     public static Properties readProperties(final FileObject fileObject, final Properties properties)
152             throws FileSystemException, IOException {
153         if (fileObject == null) {
154             return properties;
155         }
156         try (InputStream inputStream = fileObject.getContent().getInputStream()) {
157             properties.load(inputStream);
158         }
159         return properties;
160     }
161 
162     private FileObjectUtils() {
163         // noop
164     }
165 }