001/* 002 * Copyright (C) 2009-2011 Mathias Doenitz 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.parboiled.common; 018 019import java.io.BufferedInputStream; 020import java.io.BufferedOutputStream; 021import java.io.BufferedReader; 022import java.io.BufferedWriter; 023import java.io.ByteArrayInputStream; 024import java.io.ByteArrayOutputStream; 025import java.io.CharArrayReader; 026import java.io.CharArrayWriter; 027import java.io.File; 028import java.io.FileInputStream; 029import java.io.FileNotFoundException; 030import java.io.FileOutputStream; 031import java.io.IOException; 032import java.io.InputStream; 033import java.io.InputStreamReader; 034import java.io.OutputStream; 035import java.io.OutputStreamWriter; 036import java.io.Reader; 037import java.io.StringReader; 038import java.io.StringWriter; 039import java.io.Writer; 040import java.nio.charset.Charset; 041 042public final class FileUtils { 043 044 private FileUtils() {} 045 046 public static String readAllTextFromResource(String resource) { 047 Preconditions.checkArgNotNull(resource, "resource"); 048 return readAllText(FileUtils.class.getClassLoader().getResourceAsStream(resource)); 049 } 050 051 public static String readAllTextFromResource(String resource, Charset charset) { 052 Preconditions.checkArgNotNull(resource, "resource"); 053 Preconditions.checkArgNotNull(charset, "charset"); 054 return readAllText(FileUtils.class.getClassLoader().getResourceAsStream(resource), charset); 055 } 056 057 public static String readAllText(String filename) { 058 Preconditions.checkArgNotNull(filename, "filename"); 059 return readAllText(new File(filename)); 060 } 061 062 public static String readAllText(String filename, Charset charset) { 063 Preconditions.checkArgNotNull(filename, "filename"); 064 Preconditions.checkArgNotNull(charset, "charset"); 065 return readAllText(new File(filename), charset); 066 } 067 068 public static String readAllText(File file) { 069 Preconditions.checkArgNotNull(file, "file"); 070 return readAllText(file, Charset.forName("UTF8")); 071 } 072 073 public static String readAllText(File file, Charset charset) { 074 Preconditions.checkArgNotNull(file, "file"); 075 Preconditions.checkArgNotNull(charset, "charset"); 076 try { 077 return readAllText(new FileInputStream(file), charset); 078 } 079 catch (FileNotFoundException e) { 080 return null; 081 } 082 } 083 084 public static String readAllText(InputStream stream) { 085 return readAllText(stream, Charset.forName("UTF8")); 086 } 087 088 public static String readAllText(InputStream stream, Charset charset) { 089 Preconditions.checkArgNotNull(charset, "charset"); 090 if (stream == null) return null; 091 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset)); 092 StringWriter writer = new StringWriter(); 093 copyAll(reader, writer); 094 return writer.toString(); 095 } 096 097 public static char[] readAllCharsFromResource(String resource) { 098 Preconditions.checkArgNotNull(resource, "resource"); 099 return readAllChars(FileUtils.class.getClassLoader().getResourceAsStream(resource)); 100 } 101 102 public static char[] readAllCharsFromResource(String resource, Charset charset) { 103 Preconditions.checkArgNotNull(resource, "resource"); 104 Preconditions.checkArgNotNull(charset, "charset"); 105 return readAllChars(FileUtils.class.getClassLoader().getResourceAsStream(resource), charset); 106 } 107 108 public static char[] readAllChars(String filename) { 109 Preconditions.checkArgNotNull(filename, "filename"); 110 return readAllChars(new File(filename)); 111 } 112 113 public static char[] readAllChars(String filename, Charset charset) { 114 Preconditions.checkArgNotNull(filename, "filename"); 115 Preconditions.checkArgNotNull(charset, "charset"); 116 return readAllChars(new File(filename), charset); 117 } 118 119 public static char[] readAllChars(File file) { 120 Preconditions.checkArgNotNull(file, "file"); 121 return readAllChars(file, Charset.forName("UTF8")); 122 } 123 124 public static char[] readAllChars(File file, Charset charset) { 125 Preconditions.checkArgNotNull(file, "file"); 126 Preconditions.checkArgNotNull(charset, "charset"); 127 try { 128 return readAllChars(new FileInputStream(file), charset); 129 } 130 catch (FileNotFoundException e) { 131 return null; 132 } 133 } 134 135 public static char[] readAllChars(InputStream stream) { 136 return readAllChars(stream, Charset.forName("UTF8")); 137 } 138 139 public static char[] readAllChars(InputStream stream, Charset charset) { 140 Preconditions.checkArgNotNull(charset, "charset"); 141 if (stream == null) return null; 142 BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset)); 143 CharArrayWriter writer = new CharArrayWriter(); 144 copyAll(reader, writer); 145 return writer.toCharArray(); 146 } 147 148 public static byte[] readAllBytesFromResource(String resource) { 149 Preconditions.checkArgNotNull(resource, "resource"); 150 return readAllBytes(FileUtils.class.getClassLoader().getResourceAsStream(resource)); 151 } 152 153 public static byte[] readAllBytes(String filename) { 154 Preconditions.checkArgNotNull(filename, "filename"); 155 return readAllBytes(new File(filename)); 156 } 157 158 public static byte[] readAllBytes(File file) { 159 Preconditions.checkArgNotNull(file, "file"); 160 try { 161 return readAllBytes(new FileInputStream(file)); 162 } 163 catch (FileNotFoundException e) { 164 return null; 165 } 166 } 167 168 public static byte[] readAllBytes(InputStream stream) { 169 if (stream == null) return null; 170 BufferedInputStream in = new BufferedInputStream(stream); 171 ByteArrayOutputStream out = new ByteArrayOutputStream(); 172 copyAll(in, out); 173 return out.toByteArray(); 174 } 175 176 public static void writeAllText(String text, String filename) { 177 Preconditions.checkArgNotNull(filename, "filename"); 178 writeAllText(text, new File(filename)); 179 } 180 181 public static void writeAllText(String text, String filename, Charset charset) { 182 Preconditions.checkArgNotNull(filename, "filename"); 183 Preconditions.checkArgNotNull(charset, "charset"); 184 writeAllText(text, new File(filename), charset); 185 } 186 187 public static void writeAllText(String text, File file) { 188 Preconditions.checkArgNotNull(file, "file"); 189 writeAllText(text, file, Charset.forName("UTF8")); 190 } 191 192 public static void writeAllText(String text, File file, Charset charset) { 193 Preconditions.checkArgNotNull(file, "file"); 194 Preconditions.checkArgNotNull(charset, "charset"); 195 try { 196 ensureParentDir(file); 197 writeAllText(text, new FileOutputStream(file), charset); 198 } 199 catch (FileNotFoundException e) { 200 throw new RuntimeException(e); 201 } 202 } 203 204 public static void writeAllText(String text, OutputStream stream) { 205 Preconditions.checkArgNotNull(stream, "stream"); 206 writeAllText(text, stream, Charset.forName("UTF8")); 207 } 208 209 public static void writeAllText(String text, OutputStream stream, Charset charset) { 210 Preconditions.checkArgNotNull(stream, "stream"); 211 Preconditions.checkArgNotNull(charset, "charset"); 212 StringReader reader = new StringReader(text != null ? text : ""); 213 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset)); 214 copyAll(reader, writer); 215 } 216 217 public static void writeAllChars(char[] chars, String filename) { 218 Preconditions.checkArgNotNull(filename, "filename"); 219 writeAllChars(chars, new File(filename)); 220 } 221 222 public static void writeAllChars(char[] chars, String filename, Charset charset) { 223 Preconditions.checkArgNotNull(filename, "filename"); 224 Preconditions.checkArgNotNull(charset, "charset"); 225 writeAllChars(chars, new File(filename), charset); 226 } 227 228 public static void writeAllChars(char[] chars, File file) { 229 Preconditions.checkArgNotNull(file, "file"); 230 writeAllChars(chars, file, Charset.forName("UTF8")); 231 } 232 233 public static void writeAllChars(char[] chars, File file, Charset charset) { 234 Preconditions.checkArgNotNull(file, "file"); 235 Preconditions.checkArgNotNull(charset, "charset"); 236 try { 237 ensureParentDir(file); 238 writeAllChars(chars, new FileOutputStream(file), charset); 239 } 240 catch (FileNotFoundException e) { 241 throw new RuntimeException(e); 242 } 243 } 244 245 public static void writeAllChars(char[] chars, OutputStream stream) { 246 Preconditions.checkArgNotNull(stream, "stream"); 247 writeAllChars(chars, stream, Charset.forName("UTF8")); 248 } 249 250 public static void writeAllChars(char[] chars, OutputStream stream, Charset charset) { 251 Preconditions.checkArgNotNull(stream, "stream"); 252 Preconditions.checkArgNotNull(charset, "charset"); 253 CharArrayReader reader = new CharArrayReader(chars != null ? chars : new char[0]); 254 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stream, charset)); 255 copyAll(reader, writer); 256 } 257 258 public static void writeAllBytes(byte[] data, String filename) { 259 Preconditions.checkArgNotNull(data, "data"); 260 Preconditions.checkArgNotNull(filename, "filename"); 261 writeAllBytes(data, new File(filename)); 262 } 263 264 public static void writeAllBytes(byte[] data, File file) { 265 Preconditions.checkArgNotNull(data, "data"); 266 Preconditions.checkArgNotNull(file, "file"); 267 try { 268 ensureParentDir(file); 269 writeAllBytes(data, new FileOutputStream(file)); 270 } 271 catch (FileNotFoundException e) { 272 throw new RuntimeException(e); 273 } 274 } 275 276 public static void writeAllBytes(byte[] data, OutputStream stream) { 277 Preconditions.checkArgNotNull(data, "data"); 278 Preconditions.checkArgNotNull(stream, "stream"); 279 ByteArrayInputStream in = new ByteArrayInputStream(data); 280 BufferedOutputStream out = new BufferedOutputStream(stream); 281 copyAll(in, out); 282 } 283 284 public static void copyAll(Reader reader, Writer writer) { 285 Preconditions.checkArgNotNull(reader, "reader"); 286 Preconditions.checkArgNotNull(writer, "writer"); 287 try { 288 char[] data = new char[4096]; // copy in chunks of 4K 289 int count; 290 while ((count = reader.read(data)) >= 0) writer.write(data, 0, count); 291 292 reader.close(); 293 writer.close(); 294 } 295 catch (IOException e) { 296 throw new RuntimeException(e); 297 } 298 } 299 300 public static void copyAll(InputStream in, OutputStream out) { 301 Preconditions.checkArgNotNull(in, "in"); 302 Preconditions.checkArgNotNull(out, "out"); 303 try { 304 byte[] data = new byte[4096]; // copy in chunks of 4K 305 int count; 306 while ((count = in.read(data)) >= 0) { 307 out.write(data, 0, count); 308 } 309 310 in.close(); 311 out.close(); 312 } 313 catch (IOException e) { 314 throw new RuntimeException(e); 315 } 316 } 317 318 public static void ensureParentDir(String filename) { 319 ensureParentDir(new File(filename)); 320 } 321 322 public static void ensureParentDir(File file) { 323 File parentDir = file.getParentFile(); 324 if (parentDir != null && !parentDir.exists()) { 325 try { 326 forceMkdir(parentDir); 327 } catch (IOException e) { 328 throw new RuntimeException(String.format("Could not create directory %s", parentDir), e); 329 } 330 } 331 } 332 333 public static void forceMkdir(File directory) throws IOException { 334 if (directory.exists()) { 335 if (directory.isFile()) { 336 throw new IOException( 337 "File '" + directory + "' exists and is not a directory. Unable to create directory."); 338 } 339 } else { 340 if (!directory.mkdirs()) { 341 throw new IOException("Unable to create directory " + directory); 342 } 343 } 344 } 345 346}