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 */
27 public class FileUtils {
28
29 private FileUtils() {
30 }
31
32 /**
33 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
34 *
35 * @param file the file to get the extension from
36 * @return the file extension
37 */
38 public static String getFileExtension(final File file) {
39 String fileName = file.getName();
40 int dotIndex = fileName.lastIndexOf('.');
41 return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
42 }
43
44 /**
45 * Returns the file extension without the '.' for the given filename, or the empty string if the file has no extension.
46 *
47 * @param file the file to get the extension from
48 * @return the file extension
49 */
50 public static String getFileExtension(final String file) {
51 return getFileExtension(new File(file));
52 }
53
54 /**
55 * Returns the filename without the extension or '.'.
56 *
57 * @param file the file remove the extension from
58 * @return the file name without its path or extension
59 */
60 public static String getNameWithoutExtension(final File file) {
61 String fileName = file.getName();
62 int dotIndex = fileName.lastIndexOf('.');
63 return (dotIndex == -1) ? fileName : fileName.substring(0, dotIndex);
64 }
65
66 /**
67 * Returns the filename without the extension or '.'.
68 *
69 * @param file the file remove the extension from
70 * @return the file name without its path or extension
71 */
72 public static String getNameWithoutExtension(final String file) {
73 return getNameWithoutExtension(new File(file));
74 }
75
76 /**
77 * Closes the InputStream if it is not null, swallowing any exceptions.
78 *
79 * @param inputStream the InputStream to close
80 */
81 public static void closeQuietly(final InputStream inputStream) {
82 if (inputStream != null) {
83 try {
84 inputStream.close();
85 } catch (IOException e) {
86 // do nothing, close quietly
87 }
88 }
89 }
90
91 /**
92 * Closes the OutputStream if it is not null, swallowing any exceptions.
93 *
94 * @param outputStream the OutputStream to close
95 */
96 public static void closeQuietly(final OutputStream outputStream) {
97 if (outputStream != null) {
98 try {
99 outputStream.close();
100 } catch (IOException e) {
101 // do nothing, close quietly
102 }
103 }
104 }
105
106 /**
107 * Closes the Reader if it is not null, swallowing any exceptions.
108 *
109 * @param reader the Reader to close
110 */
111 public static void closeQuietly(final Reader reader) {
112 if (reader != null) {
113 try {
114 reader.close();
115 } catch (IOException e) {
116 // do nothing, close quietly
117 }
118 }
119 }
120
121 /**
122 * Closes the Writer if it is not null, swallowing any exceptions.
123 *
124 * @param writer the Writer to close
125 */
126 public static void closeQuietly(final Writer writer) {
127 if (writer != null) {
128 try {
129 writer.close();
130 } catch (IOException e) {
131 // do nothing, close quietly
132 }
133 }
134 }
135
136 }