View Javadoc

1   /*
2   This file is part of the xframe software package
3   hosted at http://xframe.sourceforge.net
4   
5   Copyright (c) 2003 Kurt Riede.
6   
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11  
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16  
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21  package net.sf.xframe.xsddoc.util;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  
30  /***
31   * Static file utility methods.
32   *
33   * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
34   */
35  public final class FileUtil {
36  
37      /*** Size of buffer when copying files. */
38      private static final int COPY_BUFFER_SIZE = 8192;
39  
40      /*** file separator from <code>File.separator</code>. */
41      private static final String FILE_SEP = File.separator;
42  
43      /***
44       * Private default constructor to prevent instantiation.
45       */
46      private FileUtil() {
47      }
48  
49      /***
50       * Copy a file.
51       *
52       * @param in <code>InputStream</code>
53       * @param destFile out as <code>File</code>
54       * @throws IOException if file cannot ne copied
55       */
56      public static void copyFile(final InputStream in, final File destFile) throws IOException {
57          if (destFile.exists() && destFile.isFile()) {
58              destFile.delete();
59          }
60          FileOutputStream out = null;
61          try {
62              out = new FileOutputStream(destFile);
63              copyFile(in, out);
64          } finally {
65              if (out != null) {
66                  out.close();
67              }
68          }
69      }
70  
71      /***
72       * Copy a file.
73       *
74       * @param in <code>InputStream</code>
75       * @param out as <code>OutputStream</code>
76       * @throws IOException if file cannot ne copied
77       */
78      public static void copyFile(final InputStream in, final OutputStream out) throws IOException {
79          final byte[] buffer = new byte[COPY_BUFFER_SIZE];
80          int count = 0;
81          do {
82              out.write(buffer, 0, count);
83              count = in.read(buffer, 0, buffer.length);
84          } while (count != -1);
85      }
86  
87      /***
88       * Copy a file.
89       *
90       * @param in <code>InputStream</code>
91       * @param destFile out file name
92       * @throws IOException if file cannot ne copied
93       */
94      public static void copyFile(final InputStream in, final String destFile) throws IOException {
95          copyFile(in, new File(destFile));
96      }
97  
98      /***
99       * Copy a file.
100      *
101      * @param inFile source file name
102      * @param destFile destination file name
103      * @throws IOException if file cannot ne copied
104      */
105     public static void copyFile(final String inFile, final String destFile) throws IOException {
106         FileInputStream in = null;
107         try {
108             in = new FileInputStream(inFile);
109             copyFile(in, destFile);
110         } finally {
111             if (in != null) {
112                 in.close();
113             }
114         }
115     }
116 
117     /***
118      * Returns the location of a file relative to a base file.
119      *
120      * @param baseFile the base file
121      * @param file the file to be resolved
122      * @return resolved location
123      */
124     public static String getLocation(final String baseFile, final String file) {
125         if (baseFile == null || file.indexOf(':') > 0) {
126             return file;
127         }
128         final String parent = new File(baseFile).getParent();
129         if (parent == null) {
130             return file;
131         }
132         return parent + FILE_SEP + file;
133     }
134 }