001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022
023/**
024 * Utility methods for dealing with FileObjects.
025 */
026public final class FileUtil
027{
028
029    private FileUtil()
030    {
031    }
032
033    /**
034     * Returns the content of a file, as a byte array.
035     *
036     * @param file The file to get the content of.
037     * @return The content as a byte array.
038     * @throws IOException if the file content cannot be accessed.
039     */
040    public static byte[] getContent(final FileObject file)
041        throws IOException
042    {
043        final FileContent content = file.getContent();
044        final int size = (int) content.getSize();
045        final byte[] buf = new byte[size];
046
047        final InputStream in = content.getInputStream();
048        try
049        {
050            int read = 0;
051            for (int pos = 0; pos < size && read >= 0; pos += read)
052            {
053                read = in.read(buf, pos, size - pos);
054            }
055        }
056        finally
057        {
058            in.close();
059        }
060
061        return buf;
062    }
063
064    /**
065     * Writes the content of a file to an OutputStream.
066     * @param file The FileObject to write.
067     * @param output The OutputStream to write to.
068     * @throws IOException if an error occurs writing the file.
069     * @see FileContent#write(OutputStream)
070     */
071    public static void writeContent(final FileObject file,
072                                    final OutputStream output)
073        throws IOException
074    {
075        file.getContent().write(output);
076    }
077
078    /**
079     * Copies the content from a source file to a destination file.
080     * @param srcFile The source FileObject.
081     * @param destFile The target FileObject
082     * @throws IOException If an error occurs copying the file.
083     * @see FileContent#write(FileContent)
084     * @see FileContent#write(FileObject)
085     */
086    public static void copyContent(final FileObject srcFile,
087                                   final FileObject destFile)
088        throws IOException
089    {
090        srcFile.getContent().write(destFile);
091    }
092
093}