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;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  
22  /**
23   * Utility methods for dealing with FileObjects.
24   */
25  public final class FileUtil {
26  
27      /**
28       * Copies the content from a source file to a destination file.
29       *
30       * @param srcFile  The source FileObject.
31       * @param destFile The target FileObject
32       * @throws IOException If an error occurs copying the file.
33       * @see FileContent#write(FileContent)
34       * @see FileContent#write(FileObject)
35       */
36      public static void copyContent(final FileObject.html#FileObject">FileObject srcFile, final FileObject destFile) throws IOException {
37          srcFile.getContent().write(destFile);
38      }
39  
40      /**
41       * Returns the content of a file, as a byte array.
42       *
43       * @param file The file to get the content of.
44       * @return The content as a byte array.
45       * @throws IOException if the file content cannot be accessed.
46       */
47      public static byte[] getContent(final FileObject file) throws IOException {
48          try (final FileContent content = file.getContent()) {
49              return content.getByteArray();
50          }
51      }
52  
53      /**
54       * Writes the content of a file to an OutputStream.
55       *
56       * @param file   The FileObject to write.
57       * @param output The OutputStream to write to.
58       * @throws IOException if an error occurs writing the file.
59       * @see FileContent#write(OutputStream)
60       */
61      public static void writeContent(final FileObject file, final OutputStream output) throws IOException {
62          file.getContent().write(output);
63      }
64  
65      private FileUtil() {
66      }
67  
68  }