1 package com.bradmcevoy.io;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.BufferedReader;
6 import org.apache.commons.io.output.ByteArrayOutputStream;
7 import java.io.Closeable;
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileNotFoundException;
11 import java.io.FileOutputStream;
12 import java.io.FileReader;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.Method;
18
19 public class FileUtils {
20 public void copy( File source, File dest ) {
21 FileInputStream is = null;
22 FileOutputStream os = null;
23 try {
24 is = new FileInputStream(source);
25 os = new FileOutputStream(dest);
26 int i = is.read();
27 while( i >= 0 ) {
28 os.write( i );
29 i = is.read();
30 }
31 } catch (FileNotFoundException ex) {
32 ex.printStackTrace();
33 } catch (IOException ex) {
34 ex.printStackTrace();
35 } finally {
36 close(is);
37 close(os);
38 }
39 }
40
41 public static ByteArrayOutputStream readIn(InputStream is) throws IOException {
42 ByteArrayOutputStream os = new ByteArrayOutputStream();
43 StreamUtils.readTo(is, os, true,true);
44 return os;
45 }
46
47 @SuppressWarnings("unchecked")
48 public static String readResource(Class cl, String res) throws IOException {
49 InputStream in = cl.getResourceAsStream(res);
50 if( in == null ) {
51 throw new IOException( "Failed to read resource: " + res + " relative to class: " + cl.getCanonicalName());
52 }
53 ByteArrayOutputStream out = readIn(in);
54 return out.toString();
55 }
56
57 public static void close(InputStream in) {
58 try {
59 if( in == null ) return;
60 in.close();
61 } catch( IOException ex ) {
62 }
63 }
64
65 public static void close(Closeable in) {
66 try {
67 if( in == null ) return;
68 in.close();
69 } catch( IOException ex ) {
70 }
71 }
72
73 public static void close(Object o) {
74 if( o == null ) return ;
75
76 try {
77 Method m = o.getClass().getMethod("close");
78 m.invoke(o);
79 } catch (IllegalArgumentException ex) {
80 throw new RuntimeException(ex);
81 } catch (SecurityException ex) {
82 throw new RuntimeException(ex);
83 } catch (IllegalAccessException ex) {
84 throw new RuntimeException(ex);
85 } catch (InvocationTargetException ex) {
86 throw new RuntimeException(ex);
87 } catch (NoSuchMethodException ex) {
88 throw new RuntimeException(ex);
89 }
90 }
91
92 public InputStream openFile(File file) throws FileNotFoundException {
93 FileInputStream fin = null;
94 BufferedInputStream br = null;
95 fin = new FileInputStream(file);
96 br = new BufferedInputStream(fin);
97 return br;
98 }
99
100 public OutputStream openFileForWrite(File file) throws FileNotFoundException {
101 FileOutputStream fout = new FileOutputStream(file);
102 BufferedOutputStream bout = new BufferedOutputStream(fout);
103 return bout;
104 }
105
106
107 public String readFile(File file) throws FileNotFoundException {
108 FileReader fr = null;
109 BufferedReader br = null;
110 try {
111 fr = new FileReader(file);
112 br = new BufferedReader(fr);
113 StringBuilder sb = new StringBuilder();
114 String s = null;
115 while ((s = br.readLine()) != null) {
116 sb.append(s);
117 sb.append("\n");
118 }
119 return sb.toString();
120 } catch(FileNotFoundException e) {
121 throw e;
122 } catch (IOException ex) {
123 throw new RuntimeException(ex);
124 } finally {
125 close(br);
126 close( fr );
127 }
128 }
129
130 public String read(InputStream in) {
131 try {
132 BufferedInputStream bin = new BufferedInputStream(in);
133 int s;
134 byte[] buf = new byte[1024];
135 StringBuilder sb = new StringBuilder();
136 while( (s = bin.read(buf)) > -1 ) {
137 sb.append(new String(buf,0,s));
138 }
139 return sb.toString();
140 } catch (IOException ex) {
141 throw new RuntimeException(ex);
142 }
143 }
144
145
146 public File resolveRelativePath(File start, String path) {
147 String[] arr = path.split("/");
148 File f = start;
149 for( String s : arr ) {
150 if( s.equals("..") ) {
151 f = f.getParentFile();
152 } else {
153 f = new File(f,s);
154 }
155 }
156 return f;
157 }
158
159 public static String getExtension(File file) {
160 return getExtension(file.getName());
161 }
162
163 public static String getExtension(String nm) {
164 if( nm.indexOf(".") >= 0 ) {
165 String[] arr = nm.split("[.]");
166 return arr[arr.length-1];
167 } else {
168 return null;
169 }
170 }
171
172 public static String stripExtension(String nm) {
173 if( nm.indexOf(".") >= 0 ) {
174 String[] arr = nm.split("[.]");
175 StringBuilder sb = new StringBuilder();
176 for( int i=0; i<arr.length-1; i++ ) {
177 if(arr[i] != null ) {
178 if( i!=0 ) sb.append(".");
179 sb.append(arr[i]);
180 }
181 }
182 return sb.toString();
183 } else {
184 return nm;
185 }
186 }
187
188 public static String preprendExtension(String filename, String newExt) {
189 String ext = getExtension(filename);
190 filename = stripExtension(filename);
191 filename = filename + "." + newExt + "." + ext;
192 return filename;
193 }
194
195 public static String incrementFileName(String name, boolean isFirst) {
196 String mainName = stripExtension(name);
197 String ext = getExtension(name);
198 int count;
199 if( isFirst ) {
200 count = 1;
201 } else {
202 int pos = mainName.lastIndexOf("(");
203 if( pos > 0 ) {
204 String sNum = mainName.substring(pos+1, mainName.length()-1);
205 count = Integer.parseInt(sNum)+1;
206 mainName = mainName.substring(0,pos);
207 } else {
208 count = 1;
209 }
210 }
211 String s = mainName + "(" + count + ")";
212 if( ext != null) s = s + "." + ext;
213 return s;
214
215 }
216
217
218
219
220
221
222
223
224
225 public static String sanitiseName(String s) {
226 s = s.replaceAll("[ ]","_");
227 return s;
228 }
229
230 }