1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34 public final class FileObjectUtils {
35
36
37
38
39
40
41
42
43
44 public static boolean exists(final FileObject fileObject) throws FileSystemException {
45 return fileObject != null && fileObject.exists();
46 }
47
48
49
50
51
52
53
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
73
74
75
76
77
78
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
88
89
90
91
92
93
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
103
104
105
106
107
108
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
118
119
120
121
122
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
140
141
142
143
144
145
146
147
148 public static Properties readProperties(final FileObject fileObject) throws FileSystemException, IOException {
149 return readProperties(fileObject, new Properties());
150 }
151
152
153
154
155
156
157
158
159
160
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
175
176
177
178
179
180
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
190
191
192
193
194
195
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
205 }
206 }