/projects/TropixCommons/src/main/edu/umn/msi/tropix/common/io/FileUtilsImpl.java

https://github.com/jmchilton/TINT · Java · 303 lines · 234 code · 48 blank · 21 comment · 3 complexity · 23cc42f17fef6bb3a9d4657cc04a251c MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright 2009 Regents of the University of Minnesota. All rights
  3. * reserved.
  4. * Copyright 2009 Mayo Foundation for Medical Education and Research.
  5. * All rights reserved.
  6. *
  7. * This program is made available under the terms of the Eclipse
  8. * Public License v1.0 which accompanies this distribution,
  9. * and is available at http://www.eclipse.org/legal/epl-v10.html
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
  14. * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
  15. * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A
  16. * PARTICULAR PURPOSE. See the License for the specific language
  17. * governing permissions and limitations under the License.
  18. *
  19. * Contributors:
  20. * Minnesota Supercomputing Institute - initial API and implementation
  21. ******************************************************************************/
  22. package edu.umn.msi.tropix.common.io;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileNotFoundException;
  26. import java.io.FileOutputStream;
  27. import java.io.FileReader;
  28. import java.io.FileWriter;
  29. import java.io.IOException;
  30. import java.io.InputStream;
  31. import java.util.Collection;
  32. import java.util.Iterator;
  33. import java.util.UUID;
  34. import javax.annotation.Nullable;
  35. import javax.annotation.WillClose;
  36. import org.apache.commons.io.FileUtils;
  37. import org.apache.commons.io.IOUtils;
  38. class FileUtilsImpl implements edu.umn.msi.tropix.common.io.FileUtils {
  39. public byte[] readFileToByteArray(final String path) {
  40. return this.readFileToByteArray(new File(path));
  41. }
  42. public byte[] readFileToByteArray(final File file) {
  43. try {
  44. return FileUtils.readFileToByteArray(file);
  45. } catch(final IOException e) {
  46. throw new IORuntimeException(e);
  47. }
  48. }
  49. public void writeStringToFile(final File file, final String data) {
  50. try {
  51. FileUtils.writeStringToFile(file, data);
  52. } catch(final IOException e) {
  53. throw new IORuntimeException(e);
  54. }
  55. }
  56. public void writeStringToFile(final String path, final String data) {
  57. this.writeStringToFile(new File(path), data);
  58. }
  59. public Collection<File> listFiles(final File directory, final String[] extensions, final boolean recursive) {
  60. @SuppressWarnings("unchecked")
  61. final Collection<File> results = FileUtils.listFiles(directory, extensions, recursive);
  62. return results;
  63. }
  64. public Collection<File> listFiles(final String directoryPath, final String[] extensions, final boolean recursive) {
  65. return this.listFiles(new File(directoryPath), extensions, recursive);
  66. }
  67. public Collection<File> listFiles(final String directoryPath) {
  68. return this.listFiles(new File(directoryPath));
  69. }
  70. public Collection<File> listFiles(final File directory) {
  71. return this.listFiles(directory, null, false);
  72. }
  73. public Iterator<File> iterateFiles(final File directory, final String[] extensions, final boolean recursive) {
  74. @SuppressWarnings("unchecked")
  75. final Iterator<File> fileIterator = FileUtils.iterateFiles(directory, extensions, recursive);
  76. return fileIterator;
  77. }
  78. public void touch(final File file) {
  79. try {
  80. FileUtils.touch(file);
  81. } catch(final IOException e) {
  82. throw new IORuntimeException(e);
  83. }
  84. }
  85. public void touch(final String path) {
  86. this.touch(new File(path));
  87. }
  88. public boolean mkdir(final File file) {
  89. return file.mkdir();
  90. }
  91. public boolean mkdir(final String path) {
  92. return this.mkdir(new File(path));
  93. }
  94. public boolean mkdirs(final File file) {
  95. return file.mkdirs();
  96. }
  97. public boolean mkdirs(final String path) {
  98. return this.mkdirs(new File(path));
  99. }
  100. public void writeStreamToFile(final String path, @WillClose final InputStream inputStream) {
  101. writeStreamToFile(new File(path), inputStream);
  102. }
  103. public void writeStreamToFile(final File file, @WillClose final InputStream inputStream) {
  104. final FileOutputStream outputStream = getFileOutputStream(file);
  105. try {
  106. IOUtils.copyLarge(inputStream, outputStream);
  107. } catch(final IOException e) {
  108. throw new IORuntimeException(e);
  109. } finally {
  110. IOUtils.closeQuietly(outputStream);
  111. IOUtils.closeQuietly(inputStream);
  112. }
  113. }
  114. public void writeByteArrayToFile(final File file, final byte[] contents) {
  115. try {
  116. FileUtils.writeByteArrayToFile(file, contents);
  117. } catch(final IOException e) {
  118. throw new IORuntimeException(e);
  119. }
  120. }
  121. public void writeByteArrayToFile(final String path, final byte[] contents) {
  122. this.writeByteArrayToFile(new File(path), contents);
  123. }
  124. public void deleteDirectory(final File directory) {
  125. try {
  126. FileUtils.deleteDirectory(directory);
  127. } catch(final IOException e) {
  128. throw new IORuntimeException(e);
  129. }
  130. }
  131. public void deleteDirectory(final String directoryPath) {
  132. this.deleteDirectory(new File(directoryPath));
  133. }
  134. public boolean deleteDirectoryQuietly(@Nullable final File file) {
  135. try {
  136. FileUtils.deleteDirectory(file);
  137. return true;
  138. } catch(final Exception e) {
  139. return false;
  140. }
  141. }
  142. public boolean deleteDirectoryQuietly(@Nullable final String path) {
  143. final File file = getFile(path);
  144. return this.deleteDirectoryQuietly(file);
  145. }
  146. @Nullable
  147. private File getFile(@Nullable final String path) {
  148. try {
  149. return new File(path);
  150. } catch(final RuntimeException e) {
  151. return null;
  152. }
  153. }
  154. public boolean deleteQuietly(@Nullable final File file) {
  155. try {
  156. if(file != null) {
  157. return file.delete();
  158. } else {
  159. return false;
  160. }
  161. } catch(final Exception e) {
  162. return false;
  163. }
  164. }
  165. public boolean deleteQuietly(@Nullable final String path) {
  166. final File file = getFile(path);
  167. return this.deleteQuietly(file);
  168. }
  169. public FileInputStream getFileInputStream(final File file) {
  170. try {
  171. return new FileInputStream(file);
  172. } catch(final FileNotFoundException e) {
  173. throw new IORuntimeException(e);
  174. }
  175. }
  176. public FileInputStream getFileInputStream(final String path) {
  177. return this.getFileInputStream(new File(path));
  178. }
  179. public FileReader getFileReader(final File file) {
  180. try {
  181. return new FileReader(file);
  182. } catch(final FileNotFoundException e) {
  183. throw new IORuntimeException(e);
  184. }
  185. }
  186. public FileReader getFileReader(final String path) {
  187. return this.getFileReader(new File(path));
  188. }
  189. public FileOutputStream getFileOutputStream(final File file) {
  190. try {
  191. return new FileOutputStream(file);
  192. } catch(final FileNotFoundException e) {
  193. throw new IORuntimeException(e);
  194. }
  195. }
  196. public FileOutputStream getFileOutputStream(final String path) {
  197. return this.getFileOutputStream(new File(path));
  198. }
  199. public FileWriter getFileWriter(final File file) {
  200. try {
  201. return new FileWriter(file);
  202. } catch(final IOException e) {
  203. throw new IORuntimeException(e);
  204. }
  205. }
  206. public FileWriter getFileWriter(final String path) {
  207. return this.getFileWriter(new File(path));
  208. }
  209. public File createTempFile(final String prefix, final String suffix) {
  210. try {
  211. return File.createTempFile(prefix, suffix);
  212. } catch(final IOException e) {
  213. throw new IORuntimeException(e);
  214. }
  215. }
  216. public String readFileToString(final File file) {
  217. try {
  218. return FileUtils.readFileToString(file);
  219. } catch(final IOException e) {
  220. throw new IORuntimeException(e);
  221. }
  222. }
  223. public String readFileToString(final String path) {
  224. return this.readFileToString(new File(path));
  225. }
  226. public void moveFile(final File sourceFile, final File destFile) {
  227. try {
  228. FileUtils.moveFile(sourceFile, destFile);
  229. } catch(final IOException e) {
  230. throw new IORuntimeException(e);
  231. }
  232. }
  233. public File createTempDirectory() {
  234. @SuppressWarnings("serial")
  235. final File tempDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()) {
  236. @Override
  237. public void finalize() throws Throwable {
  238. FileUtilsImpl.this.deleteDirectoryQuietly(this);
  239. super.finalize();
  240. }
  241. };
  242. this.mkdirs(tempDir);
  243. return tempDir;
  244. }
  245. public void copyFile(final File fromFile, final File toFile) {
  246. try {
  247. FileUtils.copyFile(fromFile, toFile);
  248. } catch(final IOException e) {
  249. throw new IORuntimeException(e);
  250. }
  251. }
  252. public File createTempFile() {
  253. return createTempFile("tpx", "");
  254. }
  255. }