View Javadoc

1   package org.sf.jlaunchpad.util;
2   
3   import java.nio.channels.ReadableByteChannel;
4   import java.nio.channels.FileChannel;
5   import java.nio.channels.Channels;
6   import java.nio.ByteBuffer;
7   import java.util.jar.Manifest;
8   import java.util.jar.JarFile;
9   import java.util.zip.ZipEntry;
10  import java.io.IOException;
11  import java.io.File;
12  import java.io.InputStream;
13  import java.io.OutputStream;
14  import java.io.FileInputStream;
15  import java.io.FileOutputStream;
16  import java.io.ByteArrayOutputStream;
17  import java.io.BufferedInputStream;
18  import java.io.BufferedOutputStream;
19  
20  /**
21   * This is the class for holding file utilities.
22   *
23   * @author Alexander Shvets
24   * @version 1.0 05/14/2004
25   */
26  public final class FileUtil {
27  
28    /**
29     * Copies the stream to the temporary file.
30     *
31     * @param is   the input stream
32     * @param file the file
33     * @return the temporary file
34     * @throws IOException I/O Exception
35     */
36    public static File copyToFile(final InputStream is, final File file)
37            throws IOException {
38      FileOutputStream fos = null;
39      ReadableByteChannel source = null;
40      FileChannel target = null;
41  
42      try {
43        fos = new FileOutputStream(file);
44  
45        source = Channels.newChannel(is);
46        target = fos.getChannel();
47  
48        final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
49  
50        while (source.read(buffer) != -1) {
51          // prepare the buffer to be drained
52          buffer.flip();
53          // make sure the buffer was fully drained.
54          while (buffer.hasRemaining()) {
55            target.write(buffer);
56          }
57  
58          // make the buffer empty, ready for filling
59          buffer.clear();
60        }
61      }
62      finally {
63        if (source != null) {
64          source.close();
65        }
66  
67        if (target != null) {
68          target.close();
69        }
70  
71        if (fos != null) {
72          fos.close();
73        }
74      }
75  
76      return file;
77    }
78  
79    public static void copyFile(String fileName, OutputStream out)
80        throws IOException {
81      InputStream is = null;
82      OutputStream os = null;
83  
84      try {
85        is = new BufferedInputStream(new FileInputStream(fileName));
86        os = new BufferedOutputStream(out);
87  
88        copy(is, os);
89      }
90      finally {
91        if (is != null) {
92          is.close();
93        }
94  
95        if (os != null) {
96          os.close();
97        }
98      }
99    }
100 
101   public static void copyFile(InputStream in, String fileName)
102       throws IOException {
103     InputStream is = null;
104     OutputStream os = null;
105 
106     try {
107       is = new BufferedInputStream(in);
108       os = new BufferedOutputStream(new FileOutputStream(fileName));
109 
110       copy(is, os);
111     }
112     finally {
113       if (is != null) {
114         is.close();
115       }
116 
117       if (os != null) {
118         os.close();
119       }
120     }
121   }
122 
123   public static void copy(InputStream is, OutputStream os) throws IOException {
124     byte[] buffer = new byte[2048];
125     int bytesRead;
126     // Simple read/write loop.
127     while (-1 != (bytesRead = is.read(buffer, 0, buffer.length))) {
128       os.write(buffer, 0, bytesRead);
129     }
130   }
131 
132   /**
133    * Copies the stream to the temporary file.
134    *
135    * @param is     the input stream
136    * @param prefix the prefix part of the file name
137    * @param suffix the suffix part of the file name
138    * @return the temporary file
139    * @throws java.io.IOException I/O Exception
140    */
141   public static File copyToTempFile(final InputStream is, final String prefix, final String suffix)
142           throws IOException {
143     return copyToFile(is, File.createTempFile(prefix, suffix));
144   }
145 
146   /**
147    * Gets the manifest object from jar file.
148    *
149    * @param jarFileName the jar file name
150    * @return the manifest object
151    * @throws IOException I/O Exception
152    */
153   public static Manifest getManifest(final String jarFileName) throws IOException {
154     final JarFile jarFile = new JarFile(jarFileName);
155 
156     final Manifest manifest = getManifest(jarFile);
157 
158     jarFile.close();
159 
160     return manifest;
161   }
162 
163   /**
164    * Gets the manifest object from jar file.
165    *
166    * @param jarFile the jar file
167    * @return the manifest object
168    * @throws IOException I/O Exception
169    */
170   public static Manifest getManifest(final JarFile jarFile) throws IOException {
171     Manifest manifest = null;
172 
173     ZipEntry zipEntry = jarFile.getEntry("META-INF/MANIFEST.MF");
174 
175     if (zipEntry == null) {
176       zipEntry = jarFile.getEntry("meta-inf/manifest.mf");
177     }
178 
179     if (zipEntry != null) {
180       final InputStream is = jarFile.getInputStream(zipEntry);
181 
182       manifest = new Manifest(is);
183 
184       is.close();
185     }
186 
187     return manifest;
188   }
189 
190   /**
191    * Converst input stream into the byte array.
192    *
193    * @param is the input stream
194    * @return the byte array
195    * @throws IOException the I/O exception
196    */
197   public static byte[] getStreamAsBytes(final InputStream is) throws IOException {
198     BufferedInputStream bis = null;
199     ByteArrayOutputStream baos = null;
200 
201     try {
202       bis = new BufferedInputStream(is);
203       baos = new ByteArrayOutputStream();
204 
205       final byte[] buffer = new byte[2048];
206 
207       boolean ok = false;
208       while (!ok) {
209         final int n = bis.read(buffer);
210 
211         if (n == -1) {
212           ok = true;
213         } else {
214           baos.write(buffer, 0, n);
215         }
216       }
217     }
218     finally {
219       if (bis != null) {
220         bis.close();
221       }
222 
223       if (baos != null) {
224         baos.close();
225       }
226     }
227 
228     return baos.toByteArray();
229   }
230 
231   /**
232    * Gets the Ant project file located inside jar file.
233    *
234    * @param jarFileName the name of jar file
235    * @param location the location
236    * @param prefix the prefic
237    * @param suffix the suffix
238    * @return Ant project file
239    * @throws IOException I/O Exception
240    */
241   public static File getFileFromArchive(final String jarFileName, final String location,
242                                         final String prefix, final String suffix) throws IOException {
243     File projectFile = null;
244 
245     final JarFile jarFile = new JarFile(jarFileName);
246 
247     final ZipEntry zipEntry = jarFile.getEntry(location);
248 
249     if (zipEntry != null) {
250       projectFile =
251               FileUtil.copyToTempFile(jarFile.getInputStream(zipEntry), prefix, suffix);
252     }
253 
254     return projectFile;
255   }
256 
257   /**
258    * Conveinience method that returns the "dot" extension for the given file.
259    *
260    * @param file the file
261    * @return the extension
262    */
263   public static String getExtension(File file) {
264     if(file != null) {
265       return getExtension(file.getName());
266     }
267 
268     return null;
269   }
270 
271   /**
272    * Conveinience method that returns the "dot" extension for the given file.
273    *
274    * @param fileName the file name
275    * @return the extension
276    */
277   public static String getExtension(String fileName) {
278     String extension = null;
279 
280     int pos = fileName.lastIndexOf('.');
281 
282     if(pos > 0 && pos < fileName.length()-1) {
283       extension = fileName.substring(pos+1).toLowerCase();
284     }
285 
286     return extension;
287   }
288   
289 }