001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: FileUtils.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import java.io.*;
011:
012: import com.uwyn.rife.tools.exceptions.FileUtilsErrorException;
013: import java.net.URL;
014: import java.net.URLConnection;
015: import java.util.ArrayList;
016: import java.util.Date;
017: import java.util.Enumeration;
018: import java.util.regex.Pattern;
019: import java.util.zip.ZipEntry;
020: import java.util.zip.ZipFile;
021:
022: public abstract class FileUtils {
023: public static ArrayList<String> getFileList(File file) {
024: return getFileList(file, null, null, true);
025: }
026:
027: public static ArrayList<String> getFileList(File file,
028: Pattern included, Pattern excluded) {
029: return getFileList(file, new Pattern[] { included },
030: new Pattern[] { excluded }, true);
031: }
032:
033: public static ArrayList<String> getFileList(File file,
034: Pattern[] included, Pattern[] excluded) {
035: return getFileList(file, included, excluded, true);
036: }
037:
038: private static ArrayList<String> getFileList(File file,
039: Pattern[] included, Pattern[] excluded, boolean root) {
040: if (null == file) {
041: return new ArrayList<String>();
042: }
043:
044: ArrayList<String> filelist = new ArrayList<String>();
045: if (file.isDirectory()) {
046: String[] list = file.list();
047: if (null != list) {
048: for (String list_entry : list) {
049: File next_file = new File(file.getAbsolutePath()
050: + File.separator + list_entry);
051: ArrayList<String> dir = getFileList(next_file,
052: included, excluded, false);
053:
054: for (String file_name : dir) {
055: if (root) {
056: // if the file is not accepted, don't process it further
057: if (!StringUtils.filter(file_name,
058: included, excluded)) {
059: continue;
060: }
061:
062: } else {
063: file_name = file.getName() + File.separator
064: + file_name;
065: }
066:
067: int filelist_size = filelist.size();
068: for (int j = 0; j < filelist_size; j++) {
069: if (filelist.get(j).compareTo(file_name) > 0) {
070: filelist.add(j, file_name);
071: break;
072: }
073: }
074: if (filelist.size() == filelist_size) {
075: filelist.add(file_name);
076: }
077: }
078: }
079: }
080: } else if (file.isFile()) {
081: String file_name = file.getName();
082:
083: if (root) {
084: if (StringUtils.filter(file_name, included, excluded)) {
085: filelist.add(file_name);
086: }
087: } else {
088: filelist.add(file_name);
089: }
090: }
091:
092: return filelist;
093: }
094:
095: public static void moveFile(File source, File target)
096: throws FileUtilsErrorException {
097: if (null == source)
098: throw new IllegalArgumentException("source can't be null.");
099: if (null == target)
100: throw new IllegalArgumentException("target can't be null.");
101:
102: if (!source.exists()) {
103: throw new FileUtilsErrorException("The source file '"
104: + source.getAbsolutePath() + "' does not exist.");
105: }
106:
107: // copy
108: copy(source, target);
109:
110: // then delete sourcefile
111: deleteFile(source);
112: }
113:
114: public static void moveDirectory(File source, File target)
115: throws FileUtilsErrorException {
116: if (null == source)
117: throw new IllegalArgumentException("source can't be null.");
118: if (null == target)
119: throw new IllegalArgumentException("target can't be null.");
120:
121: if (!source.exists()) {
122: throw new FileUtilsErrorException("The source directory '"
123: + source.getAbsolutePath() + "' does not exist.");
124: }
125:
126: // Create target if it does not exist already
127: if (!target.exists()) {
128: target.mkdirs();
129: }
130:
131: String[] filelist = source.list();
132:
133: for (String filelist_element : filelist) {
134: File current_file = new File(source.getAbsolutePath()
135: + File.separator + filelist_element);
136: File target_file = new File(target.getAbsolutePath()
137: + File.separator + filelist_element);
138:
139: if (current_file.isDirectory()) {
140: moveDirectory(current_file, target_file);
141: } else {
142: moveFile(current_file, target_file);
143: }
144: }
145:
146: // If we get here it means we're finished with this directory ... delete it.
147: deleteFile(source);
148:
149: }
150:
151: public static void deleteDirectory(File source)
152: throws FileUtilsErrorException {
153: if (null == source)
154: throw new IllegalArgumentException("source can't be null.");
155:
156: if (!source.exists()) {
157: throw new FileUtilsErrorException("The directory '"
158: + source.getAbsolutePath() + "' does not exist");
159: }
160:
161: String[] filelist = source.list();
162:
163: for (String filelist_element : filelist) {
164: File current_file = new File(source.getAbsolutePath()
165: + File.separator + filelist_element);
166:
167: if (current_file.isDirectory()) {
168: deleteDirectory(current_file);
169: } else {
170: deleteFile(current_file);
171: }
172: }
173:
174: // If we get here it means we're finished with this directory ... delete it.
175: deleteFile(source);
176: }
177:
178: public static void copy(InputStream inputStream,
179: OutputStream outputStream) throws FileUtilsErrorException {
180: if (null == inputStream)
181: throw new IllegalArgumentException(
182: "inputStream can't be null.");
183: if (null == outputStream)
184: throw new IllegalArgumentException(
185: "outputStream can't be null.");
186:
187: try {
188: byte[] buffer = new byte[1024];
189: int return_value = -1;
190:
191: return_value = inputStream.read(buffer);
192:
193: while (-1 != return_value) {
194: outputStream.write(buffer, 0, return_value);
195: return_value = inputStream.read(buffer);
196: }
197: } catch (IOException e) {
198: throw new FileUtilsErrorException(
199: "Error during the copying of streams.", e);
200: }
201: }
202:
203: public static void copy(InputStream inputStream, File target)
204: throws FileUtilsErrorException {
205: if (null == inputStream)
206: throw new IllegalArgumentException(
207: "inputStream can't be null.");
208: if (null == target)
209: throw new IllegalArgumentException("target can't be null.");
210:
211: try {
212: FileOutputStream file_output_stream = new FileOutputStream(
213: target);
214:
215: copy(inputStream, file_output_stream);
216:
217: file_output_stream.close();
218: } catch (IOException e) {
219: throw new FileUtilsErrorException(
220: "Error while copying an input stream to file '"
221: + target.getAbsolutePath() + "'.", e);
222: }
223: }
224:
225: public static void copy(File source, OutputStream outputStream)
226: throws FileUtilsErrorException {
227: if (null == source)
228: throw new IllegalArgumentException("source can't be null.");
229: if (null == outputStream)
230: throw new IllegalArgumentException(
231: "outputStream can't be null.");
232:
233: try {
234: FileInputStream file_input_stream = new FileInputStream(
235: source);
236:
237: copy(file_input_stream, outputStream);
238:
239: file_input_stream.close();
240: } catch (IOException e) {
241: throw new FileUtilsErrorException(
242: "Error while copying file '"
243: + source.getAbsolutePath()
244: + "' to an output stream.", e);
245: }
246: }
247:
248: public static void copy(File source, File target)
249: throws FileUtilsErrorException {
250: if (null == source)
251: throw new IllegalArgumentException("source can't be null.");
252: if (null == target)
253: throw new IllegalArgumentException("target can't be null.");
254:
255: try {
256: FileInputStream file_input_stream = new FileInputStream(
257: source);
258: FileOutputStream file_output_stream = new FileOutputStream(
259: target);
260:
261: copy(file_input_stream, file_output_stream);
262:
263: file_output_stream.close();
264: file_input_stream.close();
265: } catch (IOException e) {
266: throw new FileUtilsErrorException(
267: "Error while copying file '"
268: + source.getAbsolutePath() + "' to file '"
269: + target.getAbsolutePath() + "'.", e);
270: }
271: }
272:
273: public static ByteArrayOutputStream readStream(
274: InputStream inputStream) throws FileUtilsErrorException {
275: if (null == inputStream)
276: throw new IllegalArgumentException(
277: "inputStream can't be null.");
278:
279: try {
280: byte[] buffer = new byte[1024];
281: int return_value = -1;
282: ByteArrayOutputStream output_stream = new ByteArrayOutputStream(
283: buffer.length);
284:
285: return_value = inputStream.read(buffer);
286:
287: while (-1 != return_value) {
288: output_stream.write(buffer, 0, return_value);
289: return_value = inputStream.read(buffer);
290: }
291:
292: output_stream.close();
293:
294: inputStream.close();
295:
296: return output_stream;
297: } catch (IOException e) {
298: throw new FileUtilsErrorException(
299: "Error while reading the complete contents of an input stream.",
300: e);
301: }
302: }
303:
304: public static String readString(InputStream inputStream)
305: throws FileUtilsErrorException {
306: if (null == inputStream)
307: throw new IllegalArgumentException(
308: "inputStream can't be null.");
309:
310: return readStream(inputStream).toString();
311: }
312:
313: public static String readString(Reader reader)
314: throws FileUtilsErrorException {
315: if (null == reader)
316: throw new IllegalArgumentException("reader can't be null.");
317:
318: try {
319: char[] buffer = new char[1024];
320: StringBuilder result = new StringBuilder();
321:
322: int size = reader.read(buffer);
323: while (size != -1) {
324: result.append(buffer, 0, size);
325: size = reader.read(buffer);
326: }
327:
328: return result.toString();
329: } catch (IOException e) {
330: throw new FileUtilsErrorException(
331: "Error while reading the complete contents of an reader.",
332: e);
333: }
334: }
335:
336: public static String readString(InputStream inputStream,
337: String encoding) throws FileUtilsErrorException {
338: if (null == inputStream)
339: throw new IllegalArgumentException(
340: "inputStream can't be null.");
341:
342: try {
343: return readStream(inputStream).toString(encoding);
344: } catch (UnsupportedEncodingException e) {
345: throw new FileUtilsErrorException("Encoding '" + encoding
346: + "' is not supported.", e);
347: }
348: }
349:
350: public static byte[] readBytes(InputStream inputStream)
351: throws FileUtilsErrorException {
352: if (null == inputStream)
353: throw new IllegalArgumentException(
354: "inputStream can't be null.");
355:
356: return readStream(inputStream).toByteArray();
357: }
358:
359: public static String readString(URL source)
360: throws FileUtilsErrorException {
361: return readString(source, null);
362: }
363:
364: public static String readString(URL source, String encoding)
365: throws FileUtilsErrorException {
366: if (null == source)
367: throw new IllegalArgumentException("source can't be null.");
368:
369: try {
370: URLConnection connection = source.openConnection();
371: connection.setUseCaches(false);
372: InputStream input_stream = connection.getInputStream();
373: String content = null;
374: if (null == encoding) {
375: content = readString(input_stream);
376: } else {
377: content = readString(input_stream, encoding);
378: }
379: input_stream.close();
380: return content;
381: } catch (IOException e) {
382: throw new FileUtilsErrorException(
383: "Error while reading url '" + source.toString()
384: + ".", e);
385: }
386: }
387:
388: public static byte[] readBytes(URL source)
389: throws FileUtilsErrorException {
390: if (null == source)
391: throw new IllegalArgumentException("source can't be null.");
392:
393: try {
394: URLConnection connection = source.openConnection();
395: connection.setUseCaches(false);
396: InputStream input_stream = connection.getInputStream();
397: byte[] content = readBytes(input_stream);
398: input_stream.close();
399: return content;
400: } catch (IOException e) {
401: throw new FileUtilsErrorException(
402: "Error while reading url '" + source.toString()
403: + ".", e);
404: }
405: }
406:
407: public static String readString(File source)
408: throws FileUtilsErrorException {
409: return readString(source, null);
410: }
411:
412: public static String readString(File source, String encoding)
413: throws FileUtilsErrorException {
414: if (null == source)
415: throw new IllegalArgumentException("source can't be null.");
416:
417: try {
418: FileInputStream file_input_stream = new FileInputStream(
419: source);
420: String content = null;
421: if (null == encoding) {
422: content = readString(file_input_stream);
423: } else {
424: content = readString(file_input_stream, encoding);
425: }
426: file_input_stream.close();
427: return content;
428: } catch (IOException e) {
429: throw new FileUtilsErrorException(
430: "Error while reading url '"
431: + source.getAbsolutePath() + ".", e);
432: }
433: }
434:
435: public static byte[] readBytes(File source)
436: throws FileUtilsErrorException {
437: if (null == source)
438: throw new IllegalArgumentException("source can't be null.");
439:
440: try {
441: FileInputStream file_input_stream = new FileInputStream(
442: source);
443: byte[] content = readBytes(file_input_stream);
444: file_input_stream.close();
445: return content;
446: } catch (IOException e) {
447: throw new FileUtilsErrorException(
448: "Error while reading file '"
449: + source.getAbsolutePath() + ".", e);
450: }
451: }
452:
453: public static void writeBytes(byte[] content, File destination)
454: throws FileUtilsErrorException {
455: if (null == content)
456: throw new IllegalArgumentException("content can't be null.");
457: if (null == destination)
458: throw new IllegalArgumentException(
459: "destination can't be null.");
460:
461: try {
462: FileOutputStream file_output_stream = new FileOutputStream(
463: destination);
464: file_output_stream.write(content);
465: file_output_stream.flush();
466: file_output_stream.close();
467: } catch (IOException e) {
468: throw new FileUtilsErrorException(
469: "Error while write a string to '"
470: + destination.getAbsolutePath() + ".", e);
471: }
472: }
473:
474: public static void writeString(String content, File destination)
475: throws FileUtilsErrorException {
476: if (null == content)
477: throw new IllegalArgumentException("content can't be null.");
478: if (null == destination)
479: throw new IllegalArgumentException(
480: "destination can't be null.");
481:
482: try {
483: FileWriter file_writer = null;
484:
485: file_writer = new FileWriter(destination);
486:
487: file_writer.write(content, 0, content.length());
488: file_writer.flush();
489: file_writer.close();
490: } catch (IOException e) {
491: throw new FileUtilsErrorException(
492: "Error while write a string to '"
493: + destination.getAbsolutePath() + ".", e);
494: }
495: }
496:
497: public static String convertPathToSystemSeperator(String path) {
498: if (null == path)
499: throw new IllegalArgumentException("path can't be null.");
500:
501: ArrayList<String> path_parts = StringUtils.split(path, "/");
502: return StringUtils.join(path_parts, File.separator);
503: }
504:
505: public static void deleteFile(File file) {
506: if (null == file)
507: throw new IllegalArgumentException("file can't be null.");
508:
509: file.delete();
510: }
511:
512: public static String getUniqueFilename() {
513: Date current_date = new Date();
514:
515: return current_date.getTime() + "-"
516: + (long) (1000000 * Math.random());
517: }
518:
519: public static void unzipFile(File source, File destination)
520: throws FileUtilsErrorException {
521: if (null == source)
522: throw new IllegalArgumentException("source can't be null.");
523: if (null == destination)
524: throw new IllegalArgumentException(
525: "destination can't be null.");
526:
527: ZipFile zip_file = null;
528: Enumeration entries = null;
529:
530: try {
531: zip_file = new ZipFile(source);
532: } catch (IOException e) {
533: throw new FileUtilsErrorException(
534: "Error while creating the zipfile '"
535: + source.getAbsolutePath() + "'.", e);
536: }
537: entries = zip_file.entries();
538:
539: while (entries.hasMoreElements()) {
540: ZipEntry entry = null;
541: InputStream input_stream = null;
542: String output_filename = null;
543: File output_file = null;
544: StringBuilder output_file_directoryname = null;
545: File output_file_directory = null;
546: FileOutputStream file_output_stream = null;
547: byte[] buffer = new byte[1024];
548: int return_value = -1;
549:
550: entry = (ZipEntry) entries.nextElement();
551: try {
552: input_stream = zip_file.getInputStream(entry);
553: } catch (IOException e) {
554: throw new FileUtilsErrorException(
555: "Error while obtaining the inputstream for entry '"
556: + entry.getName() + "'.", e);
557: }
558:
559: output_filename = destination.getAbsolutePath()
560: + File.separator
561: + entry.getName().replace('/', File.separatorChar);
562: output_file = new File(output_filename);
563: output_file_directoryname = new StringBuilder(output_file
564: .getPath());
565: output_file_directoryname
566: .setLength(output_file_directoryname.length()
567: - output_file.getName().length()
568: - File.separator.length());
569: output_file_directory = new File(output_file_directoryname
570: .toString());
571: if (!output_file_directory.exists()) {
572: if (!output_file_directory.mkdirs()) {
573: throw new FileUtilsErrorException(
574: "Couldn't create directory '"
575: + output_file_directory
576: .getAbsolutePath()
577: + "' and its parents.");
578: }
579: } else {
580: if (!output_file_directory.isDirectory()) {
581: throw new FileUtilsErrorException("Destination '"
582: + output_file_directory.getAbsolutePath()
583: + "' exists and is not a directory.");
584: }
585: }
586:
587: try {
588: file_output_stream = new FileOutputStream(
589: output_filename);
590: } catch (IOException e) {
591: throw new FileUtilsErrorException(
592: "Error while creating the output stream for file '"
593: + output_filename + "'.", e);
594: }
595:
596: try {
597: return_value = input_stream.read(buffer);
598:
599: while (-1 != return_value) {
600: file_output_stream.write(buffer, 0, return_value);
601: return_value = input_stream.read(buffer);
602: }
603: } catch (IOException e) {
604: throw new FileUtilsErrorException(
605: "Error while uncompressing entry '"
606: + output_filename + "'.", e);
607: }
608:
609: try {
610: file_output_stream.close();
611: } catch (IOException e) {
612: throw new FileUtilsErrorException(
613: "Error while closing the output stream for file '"
614: + output_filename + "'.", e);
615: }
616: try {
617: input_stream.close();
618: } catch (IOException e) {
619: throw new FileUtilsErrorException(
620: "Error while closing the input stream for entry '"
621: + entry.getName() + "'.", e);
622: }
623: }
624:
625: try {
626: zip_file.close();
627: } catch (IOException e) {
628: throw new FileUtilsErrorException(
629: "Error while closing the zipfile '"
630: + source.getAbsolutePath() + "'.", e);
631: }
632: }
633:
634: public static String getBaseName(File file) {
635: return getBaseName(file.getName());
636: }
637:
638: public static String getBaseName(String fileName) {
639: if (null == fileName)
640: throw new IllegalArgumentException(
641: "fileName can't be null.");
642:
643: String basename = null;
644:
645: int index = fileName.lastIndexOf('.');
646: if (index > 0 && index < fileName.length() - 1) {
647: basename = fileName.substring(0, index);
648: }
649:
650: return basename;
651: }
652:
653: public static String getExtension(File file) {
654: return getExtension(file.getName());
655: }
656:
657: public static String getExtension(String fileName) {
658: if (null == fileName)
659: throw new IllegalArgumentException(
660: "fileName can't be null.");
661:
662: String ext = null;
663:
664: int index = fileName.lastIndexOf('.');
665: if (index > 0 && index < fileName.length() - 1) {
666: ext = fileName.substring(index + 1).toLowerCase();
667: }
668:
669: return ext;
670: }
671: }
|