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.io.OutputStream;
22  import java.nio.charset.Charset;
23  import java.util.Properties;
24  
25  import org.apache.commons.vfs2.FileContent;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSystemException;
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       * Gets the content of a file object, as a byte array.
73       *
74       * @param file Gets the contents of this file object.
75       * @return The content as a byte array.
76       * @throws IOException if the file content cannot be accessed.
77       *
78       * @since 2.6.0
79       */
80      public static byte[] getContentAsByteArray(final FileObject file) throws IOException {
81          try (final FileContent content = file.getContent()) {
82              return content.getByteArray();
83          }
84      }
85  
86      /**
87       * Gets the content of a file as a String.
88       *
89       * @param file Gets the contents of this file object.
90       * @param charset The file character set, may be null.
91       * @return The content as a string.
92       * @throws IOException if the file content cannot be accessed.
93       * @since 2.4
94       */
95      public static String getContentAsString(final FileObject file, final Charset charset) throws IOException {
96          try (final FileContent content = file.getContent()) {
97              return content.getString(charset);
98          }
99      }
100 
101     /**
102      * Returns the content of a file as a String.
103      *
104      * @param file Gets the contents of this file object.
105      * @param charset The file character set, may be null.
106      * @return The content as a string.
107      * @throws IOException if the file content cannot be accessed.
108      * @since 2.4
109      */
110     public static String getContentAsString(final FileObject file, final String charset) throws IOException {
111         try (final FileContent content = file.getContent()) {
112             return content.getString(charset);
113         }
114     }
115 
116     /**
117      * Checks if the given FileObject is instance of given class argument.
118      *
119      * @param fileObject The FileObject.
120      * @param wantedClass The Class to check.
121      * @return true if fileObject is an instance of the specified Class.
122      * @throws FileSystemException if an error occurs.
123      */
124     public static boolean isInstanceOf(final FileObject fileObject, final Class<?> wantedClass)
125         throws FileSystemException {
126         Object searchObject = fileObject;
127         while (searchObject instanceof DecoratedFileObject) {
128             if (wantedClass.isInstance(searchObject)) {
129                 return true;
130             }
131 
132             searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
133         }
134 
135         return wantedClass.isInstance(searchObject);
136     }
137 
138     /**
139      * Reads the given file into a new {@link Properties}.
140      *
141      * @param fileObject the file to read
142      * @return a new {@link Properties}.
143      * @throws IOException On error getting this file's content.
144      * @throws FileSystemException On error getting this file's content.
145      * @throws IOException On error getting this file's content.
146      * @since 2.4
147      */
148     public static Properties readProperties(final FileObject fileObject) throws FileSystemException, IOException {
149         return readProperties(fileObject, new Properties());
150     }
151 
152     /**
153      * Reads the given file into a new given {@link Properties}.
154      *
155      * @param fileObject the file to read
156      * @param properties the destination
157      * @return a new {@link Properties}.
158      * @throws FileSystemException On error getting this file's content.
159      * @throws IOException On error getting this file's content.
160      * @since 2.4
161      */
162     public static Properties readProperties(final FileObject fileObject, final Properties properties)
163         throws FileSystemException, IOException {
164         if (fileObject == null) {
165             return properties;
166         }
167         try (InputStream inputStream = fileObject.getContent().getInputStream()) {
168             properties.load(inputStream);
169         }
170         return properties;
171     }
172 
173     /**
174      * Writes the content from a source file to a destination file.
175      *
176      * @param srcFile The source FileObject.
177      * @param destFile The target FileObject
178      * @throws IOException If an error occurs copying the file.
179      * @see FileContent#write(FileObject)
180      * @since 2.6.0
181      */
182     public static void writeContent(final FileObjectect.html#FileObject">FileObject srcFile, final FileObject destFile) throws IOException {
183         try (final FileContent content = srcFile.getContent()) {
184             content.write(destFile);
185         }
186     }
187 
188     /**
189      * Writes the content of a file to an OutputStream.
190      *
191      * @param file The FileObject to write.
192      * @param output The OutputStream to write to.
193      * @throws IOException if an error occurs writing the file.
194      * @see FileContent#write(OutputStream)
195      * @since 2.6.0
196      */
197     public static void writeContent(final FileObject file, final OutputStream output) throws IOException {
198         try (final FileContent content = file.getContent()) {
199             content.write(output);
200         }
201     }
202 
203     private FileObjectUtils() {
204         // noop
205     }
206 }