1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package org.xrn.ant;
55
56
57
58 import java.io.File;
59 import java.io.FileInputStream;
60 import java.io.FileNotFoundException;
61 import java.io.FileOutputStream;
62 import java.io.IOException;
63 import java.io.InputStream;
64
65 /***
66 * A helper static class, that enables, for instance to copy resources from the
67 * classpath.
68 *
69 * @author Edouard Mercier
70 * @version 1.0 : 2002.11.27
71 */
72 public abstract class JarHelper
73 {
74
75
76 private static final int BUFFER_SIZE = 1024;
77
78 /***
79 * Copies the content of the file that should be present in the jar, into
80 * the provided local file. If the file could not be found, it is searched
81 * for in the provided alternative directory.<br/>
82 * Note that the original file is not supposed to be a text file.<br/>
83 * If the directory path to the new file does not exist, it is created.
84 *
85 * @param fileNameInsideJar the resource file name of the file inside the
86 * jar
87 * @param alternativeDirectoryPath the directory path used if the file
88 * cannot be found within the jar; if set to null, this argument is ignored
89 * @param copyFile the file that will be the copy of the original one
90 * @return <code>true</code> if the copy was OK
91 * @see #copyInputStreamIntoFile
92 */
93 public static boolean copyFileFromJar(final String fileNameInsideJar,
94 final String alternativeDirectoryPath, final File copyFile)
95 {
96 ClassLoader classLoader = JarHelper.class.getClassLoader();
97 InputStream inputStream =
98 classLoader.getResourceAsStream(fileNameInsideJar);
99
100 if ((inputStream == null) && (alternativeDirectoryPath != null))
101 {
102
103 File alternativeDirectory = new File(alternativeDirectoryPath);
104
105 try
106 {
107 inputStream = new FileInputStream(new File(
108 alternativeDirectory, fileNameInsideJar));
109 }
110 catch (FileNotFoundException fileNotFoundException)
111 {
112
113
114
115 return false;
116 }
117 }
118
119 return copyInputStreamIntoFile(inputStream, copyFile);
120 }
121
122 /***
123 * Copies the content of the input stream into the given file. If the
124 * directory path to the new file does not exist, it is created.
125 *
126 * @param inputStream the input stream, from which the data are copied;
127 * <b>note that the input stream will be closed if this method returns
128 * <code>true</code></b>. The special cas when it is null is also handled
129 * @param copyFile the file in which the content of the input stream should
130 * be copied
131 * @return <code>true</code> if no problem during the copy occured
132 */
133 public static boolean copyInputStreamIntoFile(InputStream inputStream,
134 File copyFile)
135 {
136 if (inputStream == null)
137 {
138
139 return false;
140 }
141
142 try
143 {
144 int bufferSize = BUFFER_SIZE;
145 byte[] byteArray = new byte[bufferSize];
146 int readByteNumber = -1;
147 File parentDirectory = copyFile.getParentFile();
148
149
150 if (parentDirectory.isDirectory() == false)
151 {
152 if (parentDirectory.mkdirs() == true)
153 {
154
155 }
156 }
157
158 FileOutputStream fileOutputStream = new FileOutputStream(copyFile);
159
160 while ((readByteNumber = inputStream.read(byteArray)) != -1)
161 {
162 fileOutputStream.write(byteArray, 0, readByteNumber);
163 }
164
165 fileOutputStream.flush();
166 fileOutputStream.close();
167 inputStream.close();
168 }
169 catch (IOException ioException)
170 {
171
172 return false;
173 }
174
175 return true;
176 }
177 }