1 /*
2 * Copyright (C) 2014 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.codehaus.gmavenplus.util;
18
19 import java.io.*;
20
21
22 /**
23 * A collection of file utility methods taken from Guava so we don't have to depend on Guava.
24 *
25 * @author Keegan Witt
26 * @since 1.2
27 */
28 public class FileUtils {
29
30 private FileUtils() {
31 }
32
33 /**
34 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
35 *
36 * @param file the file to get the extension from
37 * @return the file extension
38 */
39 public static String getFileExtension(final File file) {
40 String fileName = file.getName();
41 int dotIndex = fileName.lastIndexOf('.');
42 return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
43 }
44
45 /**
46 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
47 *
48 * @param file the file to get the extension from
49 * @return the file extension
50 */
51 public static String getFileExtension(final String file) {
52 return getFileExtension(new File(file));
53 }
54
55 /**
56 * Returns the filename without the extension or '.'.
57 *
58 * @param file the file remove the extension from
59 * @return the file name without its path or extension
60 */
61 public static String getNameWithoutExtension(final File file) {
62 String fileName = file.getName();
63 int dotIndex = fileName.lastIndexOf('.');
64 return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
65 }
66
67 /**
68 * Returns the filename without the extension or '.'.
69 *
70 * @param file the file remove the extension from
71 * @return the file name without its path or extension
72 */
73 public static String getNameWithoutExtension(final String file) {
74 return getNameWithoutExtension(new File(file));
75 }
76
77 /**
78 * Closes the InputStream if it is not null, swallowing any exceptions.
79 *
80 * @param inputStream the InputStream to close
81 */
82 public static void closeQuietly(final InputStream inputStream) {
83 if (inputStream != null) {
84 try {
85 inputStream.close();
86 } catch (IOException e) {
87 // do nothing, close quietly
88 }
89 }
90 }
91
92 /**
93 * Closes the OutputStream if it is not null, swallowing any exceptions.
94 *
95 * @param outputStream the OutputStream to close
96 */
97 public static void closeQuietly(final OutputStream outputStream) {
98 if (outputStream != null) {
99 try {
100 outputStream.close();
101 } catch (IOException e) {
102 // do nothing, close quietly
103 }
104 }
105 }
106
107 /**
108 * Closes the Reader if it is not null, swallowing any exceptions.
109 *
110 * @param reader the Reader to close
111 */
112 public static void closeQuietly(final Reader reader) {
113 if (reader != null) {
114 try {
115 reader.close();
116 } catch (IOException e) {
117 // do nothing, close quietly
118 }
119 }
120 }
121
122 /**
123 * Closes the Writer if it is not null, swallowing any exceptions.
124 *
125 * @param writer the Writer to close
126 */
127 public static void closeQuietly(final Writer writer) {
128 if (writer != null) {
129 try {
130 writer.close();
131 } catch (IOException e) {
132 // do nothing, close quietly
133 }
134 }
135 }
136
137 }