1 package org.codehaus.gmavenplus.util;
2
3 import java.io.*;
4
5
6 /**
7 * A collection of file utility methods taken from Guava so we don't have to depend on Guava.
8 *
9 * @author Keegan Witt
10 */
11 public class FileUtils {
12
13 private FileUtils() {
14 }
15
16 /**
17 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
18 *
19 * @param file the file to get the extension from
20 * @return the file extension
21 */
22 public static String getFileExtension(final File file) {
23 String fileName = file.getName();
24 int dotIndex = fileName.lastIndexOf('.');
25 return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
26 }
27
28 /**
29 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
30 *
31 * @param file the file to get the extension from
32 * @return the file extension
33 */
34 public static String getFileExtension(final String file) {
35 return getFileExtension(new File(file));
36 }
37
38 /**
39 * Returns the filename without the extension or '.'.
40 *
41 * @param file the file remove the extension from
42 * @return the file name without its path or extension
43 */
44 public static String getNameWithoutExtension(final File file) {
45 String fileName = file.getName();
46 int dotIndex = fileName.lastIndexOf('.');
47 return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
48 }
49
50 /**
51 * Returns the filename without the extension or '.'.
52 *
53 * @param file the file remove the extension from
54 * @return the file name without its path or extension
55 */
56 public static String getNameWithoutExtension(final String file) {
57 return getNameWithoutExtension(new File(file));
58 }
59
60 /**
61 * Closes the InputStream if it is not null, swallowing any exceptions.
62 *
63 * @param inputStream the InputStream to close
64 */
65 public static void closeQuietly(final InputStream inputStream) {
66 if (inputStream != null) {
67 try {
68 inputStream.close();
69 } catch (IOException e) {
70 // do nothing, close quietly
71 }
72 }
73 }
74
75 /**
76 * Closes the OutputStream if it is not null, swallowing any exceptions.
77 *
78 * @param outputStream the OutputStream to close
79 */
80 public static void closeQuietly(final OutputStream outputStream) {
81 if (outputStream != null) {
82 try {
83 outputStream.close();
84 } catch (IOException e) {
85 // do nothing, close quietly
86 }
87 }
88 }
89
90 /**
91 * Closes the Reader if it is not null, swallowing any exceptions.
92 *
93 * @param reader the Reader to close
94 */
95 public static void closeQuietly(final Reader reader) {
96 if (reader != null) {
97 try {
98 reader.close();
99 } catch (IOException e) {
100 // do nothing, close quietly
101 }
102 }
103 }
104
105 /**
106 * Closes the Writer if it is not null, swallowing any exceptions.
107 *
108 * @param writer the Writer to close
109 */
110 public static void closeQuietly(final Writer writer) {
111 if (writer != null) {
112 try {
113 writer.close();
114 } catch (IOException e) {
115 // do nothing, close quietly
116 }
117 }
118 }
119
120 }