|
@@ -71,44 +71,52 @@ |
71 |
71 |
|
/** |
72 |
72 |
|
* Reads to the end of the input stream and writes the bytes to the output stream. |
73 |
73 |
|
*/ |
74 |
|
- |
public static void copyStream( InputStream input, OutputStream output ) |
|
74 |
+ |
public static void copyStreamAndCloseEm( InputStream input, OutputStream output ) |
75 |
75 |
|
{ |
76 |
|
- |
assertNotNull( "input", input ); |
77 |
|
- |
assertNotNull( "output", output ); |
78 |
76 |
|
try |
79 |
77 |
|
{ |
80 |
|
- |
byte[] buffer = new byte[4096]; |
81 |
|
- |
while ( true ) |
82 |
|
- |
{ |
83 |
|
- |
int length = input.read( buffer ); |
84 |
|
- |
if ( length == -1 ) |
85 |
|
- |
{ |
86 |
|
- |
break; |
87 |
|
- |
} |
88 |
|
- |
output.write( buffer, 0, length ); |
89 |
|
- |
} |
|
78 |
+ |
writeStream( input, output ); |
|
79 |
+ |
} |
|
80 |
+ |
finally |
|
81 |
+ |
{ |
|
82 |
+ |
dispose( input ); |
|
83 |
+ |
} |
|
84 |
+ |
} |
|
85 |
+ |
|
|
86 |
+ |
public static void writeStream( InputStream input, OutputStream output ) |
|
87 |
+ |
{ |
|
88 |
+ |
try |
|
89 |
+ |
{ |
|
90 |
+ |
append( input, output ); |
90 |
91 |
|
Closeable zCloseable = output; |
91 |
92 |
|
output = null; |
92 |
93 |
|
close( zCloseable ); |
93 |
94 |
|
} |
94 |
|
- |
catch ( IOException e ) |
95 |
|
- |
{ |
96 |
|
- |
throw new WrappedIOException( e ); |
97 |
|
- |
} |
98 |
95 |
|
finally |
99 |
96 |
|
{ |
100 |
97 |
|
dispose( output ); |
101 |
|
- |
dispose( input ); |
102 |
98 |
|
} |
103 |
99 |
|
} |
104 |
100 |
|
|
105 |
|
- |
/** |
106 |
|
- |
* Copies a file, overwriting any existing file at the destination. |
107 |
|
- |
*/ |
108 |
|
- |
public static String copyFile( String in, String out ) |
|
101 |
+ |
public static void append( InputStream input, OutputStream output ) |
109 |
102 |
|
{ |
110 |
|
- |
copyFile( new File( assertNotEmpty( "in", in ) ), new File( out = assertNotEmpty( "out", out ) ) ); |
111 |
|
- |
return out; |
|
103 |
+ |
assertNotNull( "input", input ); |
|
104 |
+ |
assertNotNull( "output", output ); |
|
105 |
+ |
byte[] buf = new byte[1024]; |
|
106 |
+ |
try |
|
107 |
+ |
{ |
|
108 |
+ |
for ( int len; (len = input.read( buf )) > -1; ) |
|
109 |
+ |
{ |
|
110 |
+ |
if ( len != 0 ) |
|
111 |
+ |
{ |
|
112 |
+ |
output.write( buf, 0, len ); |
|
113 |
+ |
} |
|
114 |
+ |
} |
|
115 |
+ |
} |
|
116 |
+ |
catch ( IOException e ) |
|
117 |
+ |
{ |
|
118 |
+ |
throw new WrappedIOException( e ); |
|
119 |
+ |
} |
112 |
120 |
|
} |
113 |
121 |
|
|
114 |
122 |
|
/** |
|
@@ -148,11 +156,20 @@ |
148 |
156 |
|
} |
149 |
157 |
|
|
150 |
158 |
|
/** |
|
159 |
+ |
* Copies a file, overwriting any existing file at the destination. |
|
160 |
+ |
*/ |
|
161 |
+ |
public static String copyFile( String in, String out ) |
|
162 |
+ |
{ |
|
163 |
+ |
copyFile( new File( assertNotEmpty( "in", in ) ), new File( out = assertNotEmpty( "out", out ) ) ); |
|
164 |
+ |
return out; |
|
165 |
+ |
} |
|
166 |
+ |
|
|
167 |
+ |
/** |
151 |
168 |
|
* Moves a file, overwriting any existing file at the destination. |
152 |
169 |
|
*/ |
153 |
170 |
|
static public String moveFile( String in, String out ) |
154 |
171 |
|
{ |
155 |
|
- |
copyFile( in = assertNotEmpty( "in", in ), out = assertNotEmpty( "out", out ) ); |
|
172 |
+ |
copyFile( in = assertNotEmpty( "in", in ), out ); |
156 |
173 |
|
delete( in ); |
157 |
174 |
|
return out; |
158 |
175 |
|
} |
|
@@ -185,7 +202,7 @@ |
185 |
202 |
|
|
186 |
203 |
|
public static BufferedOutputStream createBufferedFileOutputStream( String filePath ) |
187 |
204 |
|
{ |
188 |
|
- |
return createBufferedFileOutputStream( new File( filePath ) ); |
|
205 |
+ |
return createBufferedFileOutputStream( new File( filePath ) ); |
189 |
206 |
|
} |
190 |
207 |
|
|
191 |
208 |
|
public static BufferedOutputStream createBufferedFileOutputStream( File out ) |
|
@@ -195,7 +212,7 @@ |
195 |
212 |
|
|
196 |
213 |
|
public static FileOutputStream createFileOutputStream( File out ) |
197 |
214 |
|
{ |
198 |
|
- |
out.getParentFile().mkdirs(); |
|
215 |
+ |
mkdir( out.getParentFile() ); |
199 |
216 |
|
try |
200 |
217 |
|
{ |
201 |
218 |
|
return new FileOutputStream( out ); |
|
@@ -233,15 +250,25 @@ |
233 |
250 |
|
/** |
234 |
251 |
|
* Creates the directories in the specified path. |
235 |
252 |
|
*/ |
236 |
|
- |
public static String mkdir( String path ) |
|
253 |
+ |
public static File mkdir( File path ) |
237 |
254 |
|
{ |
238 |
|
- |
if ( new File( path = assertNotEmpty( "path", path ) ).mkdirs() ) |
|
255 |
+ |
assertNotNull( "path", path ); |
|
256 |
+ |
if ( path.mkdirs() ) |
239 |
257 |
|
{ |
240 |
|
- |
LOGGER.trace.log( "Created directory: ", path ); |
|
258 |
+ |
LOGGER.trace.log( "Created directory: ", path.getPath() ); |
241 |
259 |
|
} |
242 |
260 |
|
return path; |
243 |
261 |
|
} |
244 |
262 |
|
|
|
263 |
+ |
/** |
|
264 |
+ |
* Creates the directories in the specified path. |
|
265 |
+ |
*/ |
|
266 |
+ |
public static String mkdir( String path ) |
|
267 |
+ |
{ |
|
268 |
+ |
mkdir( new File( path = assertNotEmpty( "path", path ) ) ); |
|
269 |
+ |
return path; |
|
270 |
+ |
} |
|
271 |
+ |
|
245 |
272 |
|
/** |
246 |
273 |
|
* Deletes a directory and all files and directories it contains. |
247 |
274 |
|
*/ |