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 */
017 package org.apache.commons.vfs2;
018
019 import java.io.IOException;
020 import java.io.InputStream;
021 import java.io.OutputStream;
022
023 /**
024 * Utility methods for dealng with FileObjects.
025 *
026 * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
027 */
028 public final class FileUtil
029 {
030
031 /** The buffer size */
032 private static final int BUFFER_SIZE = 1024;
033
034 private FileUtil()
035 {
036 }
037
038 /**
039 * Returns the content of a file, as a byte array.
040 *
041 * @param file The file to get the content of.
042 * @return The content as a byte array.
043 * @throws IOException if the file content cannot be accessed.
044 */
045 public static byte[] getContent(final FileObject file)
046 throws IOException
047 {
048 final FileContent content = file.getContent();
049 final int size = (int) content.getSize();
050 final byte[] buf = new byte[size];
051
052 final InputStream in = content.getInputStream();
053 try
054 {
055 int read = 0;
056 for (int pos = 0; pos < size && read >= 0; pos += read)
057 {
058 read = in.read(buf, pos, size - pos);
059 }
060 }
061 finally
062 {
063 in.close();
064 }
065
066 return buf;
067 }
068
069 /**
070 * Writes the content of a file to an OutputStream.
071 * @param file The FileObject to write.
072 * @param outstr The OutputStream to write to.
073 * @throws IOException if an error occurs writing the file.
074 */
075 public static void writeContent(final FileObject file,
076 final OutputStream outstr)
077 throws IOException
078 {
079 final InputStream instr = file.getContent().getInputStream();
080 try
081 {
082 final byte[] buffer = new byte[BUFFER_SIZE];
083 while (true)
084 {
085 final int nread = instr.read(buffer);
086 if (nread < 0)
087 {
088 break;
089 }
090 outstr.write(buffer, 0, nread);
091 }
092 }
093 finally
094 {
095 instr.close();
096 }
097 }
098
099 /**
100 * Copies the content from a source file to a destination file.
101 * @param srcFile The source FileObject.
102 * @param destFile The target FileObject
103 * @throws IOException If an error occurs copying the file.
104 */
105 public static void copyContent(final FileObject srcFile,
106 final FileObject destFile)
107 throws IOException
108 {
109 // Create the output stream via getContent(), to pick up the
110 // validation it does
111 final OutputStream outstr = destFile.getContent().getOutputStream();
112 try
113 {
114 writeContent(srcFile, outstr);
115 }
116 finally
117 {
118 outstr.close();
119 }
120 }
121
122 }