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  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.impl.DecoratedFileObject;
28  import org.apache.commons.vfs2.provider.AbstractFileObject;
29  
30  /**
31   * Utility methods for {@link FileObject}.
32   */
33  public final class FileObjectUtils {
34  
35      /**
36       * Null-safe call to {@link FileObject#exists()}.
37       *
38       * @param fileObject the file object to test, may be null.
39       * @return false if {@code fileObject} is null, otherwise, see {@link FileObject#exists()}.
40       * @throws FileSystemException On error determining if this file exists.
41       * @since 2.4
42       */
43      public static boolean exists(final FileObject fileObject) throws FileSystemException {
44          return fileObject != null && fileObject.exists();
45      }
46  
47      /**
48       * Gets access to the base object even if decorated.
49       *
50       * @param fileObject The FileObject.
51       * @return The decorated FileObject or null.
52       * @throws FileSystemException if an error occurs.
53       */
54      public static AbstractFileObject getAbstractFileObject(final FileObject fileObject) throws FileSystemException {
55          Object searchObject = fileObject;
56          while (searchObject instanceof DecoratedFileObject) {
57              searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
58          }
59          if (searchObject instanceof AbstractFileObject) {
60              return (AbstractFileObject) searchObject;
61          }
62          if (searchObject == null) {
63              return null;
64          }
65  
66          throw new FileSystemException("vfs.util/find-abstract-file-object.error",
67              fileObject == null ? "null" : fileObject.getClass().getName());
68      }
69  
70      /**
71       * Gets the content of a file object, as a byte array.
72       *
73       * @param file Gets the contents of this file object.
74       * @return The content as a byte array.
75       * @throws IOException if the file content cannot be accessed.
76       *
77       * @since 2.6.0
78       */
79      public static byte[] getContentAsByteArray(final FileObject file) throws IOException {
80          try (final FileContent content = file.getContent()) {
81              return content.getByteArray();
82          }
83      }
84  
85      /**
86       * Gets the content of a file as a String.
87       *
88       * @param file Gets the contents of this file object.
89       * @param charset The file character set, may be null.
90       * @return The content as a string.
91       * @throws IOException if the file content cannot be accessed.
92       * @since 2.4
93       */
94      public static String getContentAsString(final FileObject file, final Charset charset) throws IOException {
95          try (final FileContent content = file.getContent()) {
96              return content.getString(charset);
97          }
98      }
99  
100     /**
101      * Returns the content of a file as a String.
102      *
103      * @param file Gets the contents of this file object.
104      * @param charset The file character set, may be null.
105      * @return The content as a string.
106      * @throws IOException if the file content cannot be accessed.
107      * @since 2.4
108      */
109     public static String getContentAsString(final FileObject file, final String charset) throws IOException {
110         try (final FileContent content = file.getContent()) {
111             return content.getString(charset);
112         }
113     }
114 
115     /**
116      * Checks if the given FileObject is instance of given class argument.
117      *
118      * @param fileObject The FileObject.
119      * @param wantedClass The Class to check.
120      * @return true if fileObject is an instance of the specified Class.
121      * @throws FileSystemException if an error occurs.
122      */
123     public static boolean isInstanceOf(final FileObject fileObject, final Class<?> wantedClass)
124         throws FileSystemException {
125         Object searchObject = fileObject;
126         while (searchObject instanceof DecoratedFileObject) {
127             if (wantedClass.isInstance(searchObject)) {
128                 return true;
129             }
130 
131             searchObject = ((DecoratedFileObject) searchObject).getDecoratedFileObject();
132         }
133 
134         if (wantedClass.isInstance(searchObject)) {
135             return true;
136         }
137 
138         return false;
139     }
140 
141     /**
142      * Reads the given file into a new {@link Properties}.
143      *
144      * @param fileObject the file to read
145      * @return a new {@link Properties}.
146      * @throws IOException On error getting this file's content.
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) throws FileSystemException, IOException {
152         return readProperties(fileObject, new Properties());
153     }
154 
155     /**
156      * Reads the given file into a new given {@link Properties}.
157      *
158      * @param fileObject the file to read
159      * @param properties the destination
160      * @return a new {@link Properties}.
161      * @throws FileSystemException On error getting this file's content.
162      * @throws IOException On error getting this file's content.
163      * @since 2.4
164      */
165     public static Properties readProperties(final FileObject fileObject, final Properties properties)
166         throws FileSystemException, IOException {
167         if (fileObject == null) {
168             return properties;
169         }
170         try (InputStream inputStream = fileObject.getContent().getInputStream()) {
171             properties.load(inputStream);
172         }
173         return properties;
174     }
175 
176     /**
177      * Writes the content of a file to an OutputStream.
178      *
179      * @param file The FileObject to write.
180      * @param output The OutputStream to write to.
181      * @throws IOException if an error occurs writing the file.
182      * @see FileContent#write(OutputStream)
183      * @since 2.6.0
184      */
185     public static void writeContent(final FileObject file, final OutputStream output) throws IOException {
186         try (final FileContent content = file.getContent()) {
187             content.write(output);
188         }
189     }
190 
191     /**
192      * Writes the content from a source file to a destination file.
193      *
194      * @param srcFile The source FileObject.
195      * @param destFile The target FileObject
196      * @throws IOException If an error occurs copying the file.
197      * @see FileContent#write(FileObject)
198      * @since 2.6.0
199      */
200     public static void writeContent(final FileObjectect.html#FileObject">FileObject srcFile, final FileObject destFile) throws IOException {
201         try (final FileContent content = srcFile.getContent()) {
202             content.write(destFile);
203         }
204     }
205 
206     private FileObjectUtils() {
207         // noop
208     }
209 }