View Javadoc
1 /* 2 * Copyright (c) 2001 by 3 * Siegfried GOESCHL <mailto:siegfried.goeschl@itserv.at>; 4 * 5 * This program is free software. 6 * 7 * You may redistribute it and/or modify it under the terms of the GNU 8 * General Public License as published by the Free Software Foundation. 9 * Version 2 of the license should be included with this distribution in 10 * the file LICENSE, as well as License.html. If the license is not 11 * included with this distribution, you may find a copy at the FSF web 12 * site at 'www.gnu.org' or 'www.fsf.org', or you may write to the 13 * Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA. 14 * 15 * THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND, 16 * NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR 17 * OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY 18 * CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR 19 * REDISTRIBUTION OF THIS SOFTWARE. 20 */ 21 22 package junit.extensions.util; 23 24 25 import java.io.File; 26 import java.io.InputStream; 27 import java.io.FileInputStream; 28 import java.io.FileOutputStream; 29 30 31 /*** 32 * This class provides some methods for load and saving data from/to a file 33 * 34 * @author Siegfried GOESCHL 35 * @version $Id: FileUtils.java,v 1.1 2002/08/29 14:06:44 wdsgoe Exp $ 36 */ 37 38 public class FileUtils { 39 40 /*** 41 * Load the given file and return the content. 42 * 43 * @param file the file to be loaded 44 * @return the content of the file 45 */ 46 47 public static byte[] loadBinary(File file) { 48 try { 49 FileInputStream fileinputstream = new FileInputStream(file); 50 byte[] is = new byte[fileinputstream.available()]; 51 52 fileinputstream.read(is); 53 fileinputstream.close(); 54 return is; 55 } catch (Exception e) { 56 throw new RuntimeException(e.getMessage()); 57 } 58 } 59 60 /*** 61 * Load the given file and return the content. 62 * 63 * @param parentDir the parent directory 64 * @param name the name of the file within the parentb directory 65 * @return the content of the file 66 */ 67 68 public static byte[] loadBinary(File parentDir, String name) { 69 return loadBinary(new File(parentDir, name)); 70 } 71 72 /*** 73 * Load the given file and return the content. 74 * 75 * @param name the name of the file to be loaded 76 * @return the content of the file 77 */ 78 79 public static byte[] loadBinary(String name) { 80 return loadBinary(new File(name)); 81 } 82 83 /*** 84 * Save the binary content to a file 85 * 86 * @param name the name of the file to store the data 87 * @param data the data to be stored 88 */ 89 90 public static void saveBinary(String name, byte[] data) { 91 saveBinary(new File(name), data); 92 } 93 94 /*** 95 * Save the binary content to a file 96 * 97 * @param parentDir the parent directory where to store the data 98 * @param name the name of the file to store the data 99 * @param data the data to be stored 100 */ 101 102 public static void saveBinary(File parentDir, String name, byte[] data) { 103 saveBinary(new File(parentDir, name), data); 104 } 105 106 /*** 107 * Save the binary content to a file 108 * 109 * @param file the file to store the data 110 * @param data the data to be stored 111 */ 112 113 public static void saveBinary(File file, byte[] data) { 114 try { 115 FileOutputStream fileoutputstream = new FileOutputStream(file); 116 117 fileoutputstream.write(data); 118 fileoutputstream.close(); 119 } catch (Exception e) { 120 throw new RuntimeException(e.getMessage()); 121 } 122 } 123 124 /*** 125 * Save the content of an input stream to a file 126 * 127 * @param name the name of the file to store the data 128 * @param is the input stream to write to a file 129 */ 130 131 public static void saveInputStream(String name, InputStream is) { 132 saveInputStream(new File(name), is); 133 } 134 135 /*** 136 * Save the content of an input stream to a file 137 * 138 * @param parentDir the parent directory where to store the data 139 * @param name the name of the file to store the data 140 * @param is the input stream to write to a file 141 */ 142 143 public static void saveInputStream(File parentDir, String name, InputStream is) { 144 saveInputStream(new File(parentDir, name), is); 145 } 146 147 /*** 148 * Save the content of an input stream to a file 149 * 150 * @param file the file to store the data 151 * @param is the input stream to write to a file 152 */ 153 154 public static void saveInputStream(File file, InputStream is) { 155 try { 156 byte[] data = new byte[is.available()]; 157 158 is.read(data); 159 saveBinary(file, data); 160 } catch (Exception e) { 161 throw new RuntimeException(e.getMessage()); 162 } 163 } 164 }

This page was automatically generated by Maven