PageRenderTime 25ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/src/play/libs/IO.java

https://github.com/hsablonniere/play
Java | 368 lines | 261 code | 22 blank | 85 comment | 22 complexity | c4f5cdeeb0a41ef1a45ea38b33adf47d MD5 | raw file
  1. package play.libs;
  2. import java.io.BufferedReader;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.OutputStream;
  11. import java.io.OutputStreamWriter;
  12. import java.io.PrintWriter;
  13. import java.io.StringWriter;
  14. import java.util.List;
  15. import java.util.Properties;
  16. import org.apache.commons.io.IOUtils;
  17. import play.exceptions.UnexpectedException;
  18. import play.utils.OrderSafeProperties;
  19. /**
  20. * IO utils
  21. */
  22. public class IO {
  23. /**
  24. * Read a properties file with the utf-8 encoding
  25. * @param is Stream to properties file
  26. * @return The Properties object
  27. */
  28. public static Properties readUtf8Properties(InputStream is) {
  29. Properties properties = new OrderSafeProperties();
  30. try {
  31. properties.load(is);
  32. is.close();
  33. } catch (Exception e) {
  34. throw new RuntimeException(e);
  35. }
  36. return properties;
  37. }
  38. /**
  39. * Read the Stream content as a string (use utf-8)
  40. * @param is The stream to read
  41. * @return The String content
  42. */
  43. public static String readContentAsString(InputStream is) {
  44. return readContentAsString(is, "utf-8");
  45. }
  46. /**
  47. * Read the Stream content as a string
  48. * @param is The stream to read
  49. * @return The String content
  50. */
  51. public static String readContentAsString(InputStream is, String encoding) {
  52. String res = null;
  53. try {
  54. res = IOUtils.toString(is, encoding);
  55. } catch(Exception e) {
  56. throw new RuntimeException(e);
  57. } finally {
  58. try {
  59. is.close();
  60. } catch(Exception e) {
  61. //
  62. }
  63. }
  64. return res;
  65. }
  66. /**
  67. * Read file content to a String (always use utf-8)
  68. * @param file The file to read
  69. * @return The String content
  70. */
  71. public static String readContentAsString(File file) {
  72. return readContentAsString(file, "utf-8");
  73. }
  74. /**
  75. * Read file content to a String
  76. * @param file The file to read
  77. * @return The String content
  78. */
  79. public static String readContentAsString(File file, String encoding) {
  80. InputStream is = null;
  81. try {
  82. is = new FileInputStream(file);
  83. StringWriter result = new StringWriter();
  84. PrintWriter out = new PrintWriter(result);
  85. BufferedReader reader = new BufferedReader(new InputStreamReader(is, encoding));
  86. String line = null;
  87. while ((line = reader.readLine()) != null) {
  88. out.println(line);
  89. }
  90. return result.toString();
  91. } catch(IOException e) {
  92. throw new UnexpectedException(e);
  93. } finally {
  94. if(is != null) {
  95. try {
  96. is.close();
  97. } catch(Exception e) {
  98. //
  99. }
  100. }
  101. }
  102. }
  103. public static List<String> readLines(InputStream is) {
  104. List<String> lines = null;
  105. try {
  106. lines = IOUtils.readLines(is);
  107. } catch (IOException ex) {
  108. throw new UnexpectedException(ex);
  109. }
  110. return lines;
  111. }
  112. public static List<String> readLines(File file, String encoding) {
  113. List<String> lines = null;
  114. InputStream is = null;
  115. try {
  116. is = new FileInputStream(file);
  117. lines = IOUtils.readLines(is, encoding);
  118. } catch (IOException ex) {
  119. throw new UnexpectedException(ex);
  120. } finally {
  121. if(is != null) {
  122. try {
  123. is.close();
  124. } catch(Exception e) {
  125. //
  126. }
  127. }
  128. }
  129. return lines;
  130. }
  131. public static List<String> readLines(File file) {
  132. return readLines(file, "utf-8");
  133. }
  134. /**
  135. * Read binary content of a file (warning does not use on large file !)
  136. * @param file The file te read
  137. * @return The binary data
  138. */
  139. public static byte[] readContent(File file) {
  140. InputStream is = null;
  141. try {
  142. is = new FileInputStream(file);
  143. byte[] result = new byte[(int) file.length()];
  144. is.read(result);
  145. return result;
  146. } catch(IOException e) {
  147. throw new UnexpectedException(e);
  148. } finally {
  149. if(is != null) {
  150. try {
  151. is.close();
  152. } catch(Exception e) {
  153. //
  154. }
  155. }
  156. }
  157. }
  158. /**
  159. * Read binary content of a stream (warning does not use on large file !)
  160. * @param is The stream to read
  161. * @return The binary data
  162. */
  163. public static byte[] readContent(InputStream is) {
  164. try {
  165. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  166. int read = 0;
  167. byte[] buffer = new byte[8096];
  168. while ((read = is.read(buffer)) > 0) {
  169. baos.write(buffer, 0, read);
  170. }
  171. return baos.toByteArray();
  172. } catch(IOException e) {
  173. throw new UnexpectedException(e);
  174. }
  175. }
  176. /**
  177. * Write String content to a stream (always use utf-8)
  178. * @param content The content to write
  179. * @param os The stream to write
  180. */
  181. public static void writeContent(CharSequence content, OutputStream os) {
  182. writeContent(content, os, "utf-8");
  183. }
  184. /**
  185. * Write String content to a stream (always use utf-8)
  186. * @param content The content to write
  187. * @param os The stream to write
  188. */
  189. public static void writeContent(CharSequence content, OutputStream os, String encoding) {
  190. try {
  191. PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(os, encoding));
  192. printWriter.println(content);
  193. printWriter.flush();
  194. os.flush();
  195. } catch(IOException e) {
  196. throw new UnexpectedException(e);
  197. } finally {
  198. try {
  199. os.close();
  200. } catch(Exception e) {
  201. //
  202. }
  203. }
  204. }
  205. /**
  206. * Write String content to a file (always use utf-8)
  207. * @param content The content to write
  208. * @param file The file to write
  209. */
  210. public static void writeContent(CharSequence content, File file) {
  211. writeContent(content, file, "utf-8");
  212. }
  213. /**
  214. * Write String content to a file (always use utf-8)
  215. * @param content The content to write
  216. * @param file The file to write
  217. */
  218. public static void writeContent(CharSequence content, File file, String encoding) {
  219. OutputStream os = null;
  220. try {
  221. os = new FileOutputStream(file);
  222. PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(os, encoding));
  223. printWriter.println(content);
  224. printWriter.flush();
  225. os.flush();
  226. } catch(IOException e) {
  227. throw new UnexpectedException(e);
  228. } finally {
  229. try {
  230. if(os != null) os.close();
  231. } catch(Exception e) {
  232. //
  233. }
  234. }
  235. }
  236. /**
  237. * Write binay data to a file
  238. * @param data The binary data to write
  239. * @param file The file to write
  240. */
  241. public static void write(byte[] data, File file) {
  242. OutputStream os = null;
  243. try {
  244. os = new FileOutputStream(file);
  245. os.write(data);
  246. os.flush();
  247. } catch(IOException e) {
  248. throw new UnexpectedException(e);
  249. } finally {
  250. try {
  251. if(os != null) os.close();
  252. } catch(Exception e) {
  253. //
  254. }
  255. }
  256. }
  257. /**
  258. * Copy an stream to another one.
  259. */
  260. public static void copy(InputStream is, OutputStream os) {
  261. try {
  262. int read = 0;
  263. byte[] buffer = new byte[8096];
  264. while ((read = is.read(buffer)) > 0) {
  265. os.write(buffer, 0, read);
  266. }
  267. } catch(IOException e) {
  268. throw new UnexpectedException(e);
  269. } finally {
  270. try {
  271. is.close();
  272. } catch(Exception e) {
  273. //
  274. }
  275. }
  276. }
  277. /**
  278. * Copy an stream to another one.
  279. */
  280. public static void write(InputStream is, OutputStream os) {
  281. try {
  282. int read = 0;
  283. byte[] buffer = new byte[8096];
  284. while ((read = is.read(buffer)) > 0) {
  285. os.write(buffer, 0, read);
  286. }
  287. } catch(IOException e) {
  288. throw new UnexpectedException(e);
  289. } finally {
  290. try {
  291. is.close();
  292. } catch(Exception e) {
  293. //
  294. }
  295. try {
  296. os.close();
  297. } catch(Exception e) {
  298. //
  299. }
  300. }
  301. }
  302. /**
  303. * Copy an stream to another one.
  304. */
  305. public static void write(InputStream is, File f) {
  306. OutputStream os = null;
  307. try {
  308. os = new FileOutputStream(f);
  309. int read = 0;
  310. byte[] buffer = new byte[8096];
  311. while ((read = is.read(buffer)) > 0) {
  312. os.write(buffer, 0, read);
  313. }
  314. } catch(IOException e) {
  315. throw new UnexpectedException(e);
  316. } finally {
  317. try {
  318. is.close();
  319. } catch(Exception e) {
  320. //
  321. }
  322. try {
  323. if(os != null) os.close();
  324. } catch(Exception e) {
  325. //
  326. }
  327. }
  328. }
  329. // If targetLocation does not exist, it will be created.
  330. public static void copyDirectory(File source, File target) {
  331. if (source.isDirectory()) {
  332. if (!target.exists()) {
  333. target.mkdir();
  334. }
  335. for (String child: source.list()) {
  336. copyDirectory(new File(source, child), new File(target, child));
  337. }
  338. } else {
  339. try {
  340. write(new FileInputStream(source), new FileOutputStream(target));
  341. } catch (IOException e) {
  342. throw new UnexpectedException(e);
  343. }
  344. }
  345. }
  346. }