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.*; | |
14 | import java.net.URL; | |
15 | import java.nio.charset.Charset; | |
16 | import java.util.ArrayList; | |
17 | import java.util.List; | |
18 | ||
19 | /** | |
20 | * Provides utilities to read and write to files. | |
21 | * | |
22 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
23 | * @version $Id$ | |
24 | */ | |
25 | 0 | public class FileUtils { |
26 | ||
27 | /** | |
28 | * @param filePath the path to the file. | |
29 | * @return the contents of the file in filePath. | |
30 | */ | |
31 | public static String read(String filePath) { | |
32 | 1 | return read(new File(filePath)); |
33 | } | |
34 | ||
35 | /** | |
36 | * @param file the file to read from. | |
37 | * @return the contents of the file. | |
38 | */ | |
39 | public static String read(File file) { | |
40 | try { | |
41 | 1 | return read(new FileInputStream(file)); |
42 | 0 | } catch (FileNotFoundException e) { |
43 | 0 | throw new RuntimeException(e); |
44 | } | |
45 | } | |
46 | ||
47 | /** | |
48 | * @param in the input stream to read from. | |
49 | * @return the contents of the inputstream. | |
50 | */ | |
51 | public static String read(InputStream in) { | |
52 | 6 | return read(new InputStreamReader(in, Charset.forName("UTF-8"))); |
53 | } | |
54 | ||
55 | /** | |
56 | * @param url the URL to read from. | |
57 | * @return the contents of the url. | |
58 | */ | |
59 | public static String read(URL url) { | |
60 | try { | |
61 | 5 | return read(url.openStream()); |
62 | 0 | } catch (IOException e) { |
63 | 0 | throw new RuntimeException(e); |
64 | } | |
65 | } | |
66 | ||
67 | /** | |
68 | * @param in the reader to read from. | |
69 | * @return the contents of the reader. | |
70 | */ | |
71 | public static String read(Reader in) { | |
72 | 6 | StringBuffer buffer = new StringBuffer(); |
73 | try { | |
74 | 359 | while (in.ready()) { |
75 | 347 | buffer.append((char) in.read()); |
76 | } | |
77 | 0 | } catch (IOException e) { |
78 | 0 | throw new RuntimeException(e); |
79 | 0 | } finally { |
80 | 6 | close(in); |
81 | 0 | } |
82 | 6 | return buffer.toString(); |
83 | } | |
84 | ||
85 | public static List<String> readlines(String file) { | |
86 | try { | |
87 | 0 | return readlines(new FileInputStream(file)); |
88 | 0 | } catch (FileNotFoundException e) { |
89 | 0 | throw new RuntimeException(e); |
90 | } | |
91 | } | |
92 | ||
93 | private static List<String> readlines(InputStream in) { | |
94 | 0 | return readlines(new InputStreamReader(in, Charset.forName("UTF-8"))); |
95 | } | |
96 | ||
97 | private static List<String> readlines(Reader in) { | |
98 | 0 | BufferedReader reader = new BufferedReader(in); |
99 | 0 | ArrayList<String> lines = new ArrayList<String>(); |
100 | ||
101 | try { | |
102 | 0 | while (reader.ready()) { |
103 | 0 | lines.add(reader.readLine()); |
104 | } | |
105 | 0 | } catch (IOException e) { |
106 | 0 | throw new RuntimeException(e); |
107 | 0 | } finally { |
108 | 0 | close(in); |
109 | 0 | } |
110 | 0 | return lines; |
111 | } | |
112 | ||
113 | /** | |
114 | * @param text the contents to write to the file. | |
115 | * @param file the file to write to. | |
116 | */ | |
117 | public static void write(String text, File file) { | |
118 | 1 | BufferedWriter w = null; |
119 | try { | |
120 | try { | |
121 | 1 | w = new BufferedWriter(new FileWriter(file)); |
122 | 1 | w.append(text); |
123 | 0 | } catch (IOException e) { |
124 | 0 | throw new RuntimeException(e); |
125 | } | |
126 | 0 | } finally { |
127 | 1 | close(w); |
128 | 0 | } |
129 | 1 | } |
130 | ||
131 | /** | |
132 | * Creates directory specified by destination, and all its parents if they don't exist. | |
133 | * | |
134 | * @param destination the directory to create. | |
135 | */ | |
136 | public static void mkdirs(String destination) { | |
137 | 0 | mkdirs(new File(destination)); |
138 | 0 | } |
139 | ||
140 | /** | |
141 | * Creates directory specified by destination, and all its parents if they don't exist. | |
142 | * | |
143 | * @param destination the directory to create. | |
144 | */ | |
145 | public static void mkdirs(File destination) { | |
146 | 0 | if (destination.exists()) { |
147 | 0 | return; |
148 | } | |
149 | 0 | if (!destination.mkdirs()) { |
150 | 0 | throw new RuntimeException("Unable to create directory [" + destination + "]."); |
151 | } | |
152 | 0 | } |
153 | ||
154 | private static void close(Closeable c) { | |
155 | 7 | if (c != null) { |
156 | try { | |
157 | 7 | c.close(); |
158 | 0 | } catch (IOException e) { |
159 | 0 | throw new RuntimeException(e); |
160 | } | |
161 | } | |
162 | 7 | } |
163 | ||
164 | } |