KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > FileUtil


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs;
17
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /**
23  * Utility methods for dealng with FileObjects.
24  *
25  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
26  */

27 public class FileUtil
28 {
29     private FileUtil()
30     {
31     }
32
33     /**
34      * Returns the content of a file, as a byte array.
35      *
36      * @param file The file to get the content of.
37      */

38     public static byte[] getContent(final FileObject file)
39         throws IOException JavaDoc
40     {
41         final FileContent content = file.getContent();
42         final int size = (int) content.getSize();
43         final byte[] buf = new byte[size];
44
45         final InputStream JavaDoc in = content.getInputStream();
46         try
47         {
48             int read = 0;
49             for (int pos = 0; pos < size && read >= 0; pos += read)
50             {
51                 read = in.read(buf, pos, size - pos);
52             }
53         }
54         finally
55         {
56             in.close();
57         }
58
59         return buf;
60     }
61
62     /**
63      * Writes the content of a file to an OutputStream.
64      */

65     public static void writeContent(final FileObject file,
66                                     final OutputStream JavaDoc outstr)
67         throws IOException JavaDoc
68     {
69         final InputStream JavaDoc instr = file.getContent().getInputStream();
70         try
71         {
72             final byte[] buffer = new byte[1024];
73             while (true)
74             {
75                 final int nread = instr.read(buffer);
76                 if (nread < 0)
77                 {
78                     break;
79                 }
80                 outstr.write(buffer, 0, nread);
81             }
82         }
83         finally
84         {
85             instr.close();
86         }
87     }
88
89     /**
90      * Copies the content from a source file to a destination file.
91      */

92     public static void copyContent(final FileObject srcFile,
93                                    final FileObject destFile)
94         throws IOException JavaDoc
95     {
96         // Create the output stream via getContent(), to pick up the
97
// validation it does
98
final OutputStream JavaDoc outstr = destFile.getContent().getOutputStream();
99         try
100         {
101             writeContent(srcFile, outstr);
102         }
103         finally
104         {
105             outstr.close();
106         }
107     }
108
109 }
110
Popular Tags