Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileUtils |
|
| 3.0;3 |
1 | /******************************************************************************* | |
2 | * Copyright (c) 2008-2009 SWTBot Committers and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Ketan Padegaonkar - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.swtbot.swt.finder.utils; | |
12 | ||
13 | import java.io.BufferedWriter; | |
14 | import java.io.Closeable; | |
15 | import java.io.File; | |
16 | import java.io.FileInputStream; | |
17 | import java.io.FileNotFoundException; | |
18 | import java.io.FileWriter; | |
19 | import java.io.IOException; | |
20 | import java.io.InputStream; | |
21 | import java.io.InputStreamReader; | |
22 | import java.io.Reader; | |
23 | import java.net.URL; | |
24 | import java.nio.charset.Charset; | |
25 | ||
26 | /** | |
27 | * Provides utilities to read and write to files. | |
28 | * | |
29 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
30 | * @version $Id$ | |
31 | */ | |
32 | 0 | public class FileUtils { |
33 | ||
34 | /** | |
35 | * @param filePath the path to the file. | |
36 | * @return the contents of the file in filePath. | |
37 | */ | |
38 | public static String read(String filePath) { | |
39 | 1 | return read(new File(filePath)); |
40 | } | |
41 | ||
42 | /** | |
43 | * @param file the file to read from. | |
44 | * @return the contents of the file. | |
45 | */ | |
46 | public static String read(File file) { | |
47 | try { | |
48 | 1 | return read(new FileInputStream(file)); |
49 | 0 | } catch (FileNotFoundException e) { |
50 | 0 | throw new RuntimeException(e); |
51 | } | |
52 | } | |
53 | ||
54 | /** | |
55 | * @param in the input stream to read from. | |
56 | * @return the contents of the inputstream. | |
57 | */ | |
58 | public static String read(InputStream in) { | |
59 | 6 | return read(new InputStreamReader(in, Charset.forName("UTF-8"))); |
60 | } | |
61 | ||
62 | /** | |
63 | * @param url the URL to read from. | |
64 | * @return the contents of the url. | |
65 | */ | |
66 | public static String read(URL url) { | |
67 | try { | |
68 | 5 | return read(url.openStream()); |
69 | 0 | } catch (IOException e) { |
70 | 0 | throw new RuntimeException(e); |
71 | } | |
72 | } | |
73 | ||
74 | /** | |
75 | * @param in the reader to read from. | |
76 | * @return the contents of the reader. | |
77 | */ | |
78 | public static String read(Reader in) { | |
79 | 6 | StringBuffer buffer = new StringBuffer(); |
80 | try { | |
81 | 359 | while (in.ready()) { |
82 | 347 | buffer.append((char) in.read()); |
83 | } | |
84 | 0 | } catch (IOException e) { |
85 | 0 | throw new RuntimeException(e); |
86 | 0 | } finally { |
87 | 6 | close(in); |
88 | 0 | } |
89 | 6 | return buffer.toString(); |
90 | } | |
91 | ||
92 | /** | |
93 | * @param text the contents to write to the file. | |
94 | * @param file the file to write to. | |
95 | */ | |
96 | public static void write(String text, File file) { | |
97 | 1 | BufferedWriter w = null; |
98 | try { | |
99 | try { | |
100 | 1 | w = new BufferedWriter(new FileWriter(file)); |
101 | 1 | w.append(text); |
102 | 0 | } catch (IOException e) { |
103 | 0 | throw new RuntimeException(e); |
104 | } | |
105 | 0 | } finally { |
106 | 1 | close(w); |
107 | 0 | } |
108 | 1 | } |
109 | ||
110 | /** | |
111 | * Creates directory specified by destination, and all its parents if they don't exist. | |
112 | * | |
113 | * @param destination the directory to create. | |
114 | */ | |
115 | public static void mkdirs(String destination) { | |
116 | 0 | mkdirs(new File(destination)); |
117 | 0 | } |
118 | ||
119 | /** | |
120 | * Creates directory specified by destination, and all its parents if they don't exist. | |
121 | * | |
122 | * @param destination the directory to create. | |
123 | */ | |
124 | public static void mkdirs(File destination) { | |
125 | 0 | if (destination.exists()) { |
126 | 0 | return; |
127 | } | |
128 | 0 | if (!destination.mkdirs()) { |
129 | 0 | throw new RuntimeException("Unable to create directory [" + destination + "]."); |
130 | } | |
131 | 0 | } |
132 | ||
133 | private static void close(Closeable c) { | |
134 | 7 | if (c != null) { |
135 | try { | |
136 | 7 | c.close(); |
137 | 0 | } catch (IOException e) { |
138 | 0 | throw new RuntimeException(e); |
139 | } | |
140 | } | |
141 | 7 | } |
142 | ||
143 | } |