| Line | Hits | Source |
|---|---|---|
| 1 | /******************************************************************************* | |
| 2 | *InternetCafe is a software solution that helps the management of Cybercafes | |
| 3 | * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005. | |
| 4 | * Copyright (C) 2006 Guido Angelo Ingenito | |
| 5 | ||
| 6 | * This program is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU General Public License | |
| 8 | * as published by the Free Software Foundation; either version 2 | |
| 9 | * of the License, or (at your option) any later version. | |
| 10 | ||
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
| 19 | *******************************************************************************/ | |
| 20 | package base.util; | |
| 21 | ||
| 22 | import java.io.File; | |
| 23 | import java.io.FileInputStream; | |
| 24 | import java.io.FileOutputStream; | |
| 25 | import java.io.IOException; | |
| 26 | import java.io.InputStream; | |
| 27 | import java.security.MessageDigest; | |
| 28 | import java.security.NoSuchAlgorithmException; | |
| 29 | import java.util.Vector; | |
| 30 | import java.util.zip.CRC32; | |
| 31 | ||
| 32 | import org.apache.log4j.Logger; | |
| 33 | import org.bouncycastle.util.encoders.Hex; | |
| 34 | ||
| 35 | /** | |
| 36 | * This class provides some basic methods to manage files and folders. | |
| 37 | * | |
| 38 | * @author Guido Angelo Ingenito | |
| 39 | 0 | */ |
| 40 | 0 | public class FileUtil { |
| 41 | 0 | |
| 42 | 0 | private static final transient Logger logger = Logger |
| 43 | .getLogger(FileUtil.class.getName()); | |
| 44 | ||
| 45 | /** | |
| 46 | * This method computes the specified file occupied space on the HD. If the | |
| 47 | * input file is a simple file will be returned its size otherwise if the | |
| 48 | * file is a directory on local HD will be recursivelly computed the | |
| 49 | * occupied space of subfolders and contained files. | |
| 50 | * | |
| 51 | * @param file | |
| 52 | 0 | * The file or directory whose size must be computed. |
| 53 | 0 | * @return A Byte rappresentation of the file'size. |
| 54 | 0 | */ |
| 55 | 0 | public static long fileSizeInByte(File file) { |
| 56 | 0 | long size = 0; |
| 57 | 0 | |
| 58 | 0 | if (file.isDirectory()) { |
| 59 | 0 | File[] filelist = file.listFiles(); |
| 60 | 0 | for (int i = 0; i < filelist.length; i++) { |
| 61 | 0 | if (filelist[i].isDirectory()) { |
| 62 | 0 | size += fileSizeInByte(filelist[i]); |
| 63 | 0 | } else { |
| 64 | 0 | size += filelist[i].length(); |
| 65 | 0 | } |
| 66 | 0 | } |
| 67 | 0 | } else |
| 68 | 0 | size += file.length(); |
| 69 | ||
| 70 | 0 | return size; |
| 71 | } | |
| 72 | ||
| 73 | /** | |
| 74 | * This method computes the specified file occupied space on the HD. If the | |
| 75 | * input file is a simple file will be returned its size otherwise if the | |
| 76 | * file is a directory on local HD will be recursivelly computed the | |
| 77 | * occupied space of subfolders and contained files. | |
| 78 | 0 | * |
| 79 | 0 | * @param file |
| 80 | * The file or directory whose size must be computed. | |
| 81 | * @return A Kylo Byte rappresentation of the file'size. | |
| 82 | */ | |
| 83 | public static long fileSizeInKB(File file) { | |
| 84 | 0 | return fileSizeInByte(file) / 1024; |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * This method computes the specified file occupied space on the HD. If the | |
| 89 | * input file is a simple file will be returned its size otherwise if the | |
| 90 | 0 | * file is a directory on local HD will be recursivelly computed the |
| 91 | 0 | * occupied space of subfolders and contained files. |
| 92 | * | |
| 93 | * @param file | |
| 94 | * The file or directory whose size must be computed. | |
| 95 | * @return A Mega Byte rappresentation of the file'size. | |
| 96 | */ | |
| 97 | public static long fileSizeInMB(File file) { | |
| 98 | 0 | return fileSizeInByte(file) / 1048576; |
| 99 | } | |
| 100 | ||
| 101 | /** | |
| 102 | 0 | * This method computes the specified file occupied space on the HD. If the |
| 103 | 0 | * input file is a simple file will be returned its size otherwise if the |
| 104 | * file is a directory on local HD will be recursivelly computed the | |
| 105 | * occupied space of subfolders and contained files. | |
| 106 | * | |
| 107 | * @param file | |
| 108 | * The file or directory whose size must be computed. | |
| 109 | * @return A Giga Byte rappresentation of the file'size. | |
| 110 | */ | |
| 111 | public static long fileSizeInGB(File file) { | |
| 112 | 0 | return fileSizeInByte(file) / 1073741824; |
| 113 | 0 | } |
| 114 | 0 | |
| 115 | 0 | /** |
| 116 | 0 | * This method creates a CRC32 Checksum relative to the file passed as |
| 117 | 0 | * formal parameter. |
| 118 | 0 | * |
| 119 | 0 | * @param file |
| 120 | 0 | * The file whose checksum must be created. |
| 121 | 0 | * @return The value of the CRC32 checksum- |
| 122 | 0 | * @throws IOException |
| 123 | 0 | * If can't be created the InputStream associated to the input |
| 124 | 0 | * File. |
| 125 | 0 | */ |
| 126 | public static Long createCRC32Checksum(File file) throws IOException { | |
| 127 | 0 | logger.debug("createCRC32Checksum(" + file.toString() + ")"); |
| 128 | ||
| 129 | 0 | InputStream inputStream = new FileInputStream(file); |
| 130 | 0 | CRC32 checksum = new CRC32(); |
| 131 | 0 | checksum.reset(); |
| 132 | 0 | byte[] buffer = new byte[1024]; |
| 133 | int bytesRead; | |
| 134 | 0 | while ((bytesRead = inputStream.read(buffer)) >= 0) { |
| 135 | 0 | checksum.update(buffer, 0, bytesRead); |
| 136 | 0 | } |
| 137 | 0 | inputStream.close(); |
| 138 | 0 | logger.debug("CRC32 Checksum value: " + checksum.getValue()); |
| 139 | 0 | |
| 140 | 0 | return new Long(checksum.getValue()); |
| 141 | } | |
| 142 | ||
| 143 | /** | |
| 144 | * This method checks if the computed CRC32 Checksum of the input file is | |
| 145 | * equals to the provided crc32Checksum passed as formal argument. | |
| 146 | * | |
| 147 | * @param file | |
| 148 | * The file whose checksum must be compared with the input | |
| 149 | * crc32Checksum. | |
| 150 | * @param crc32Checksum | |
| 151 | * The comparation parameter. | |
| 152 | 0 | * @return True if the computed checksum is queals to the crc32Checksum, |
| 153 | * false otherwise. | |
| 154 | 0 | * @throws IOException |
| 155 | 0 | * If can't be created the InputStream associated to the input |
| 156 | 0 | * File. |
| 157 | 0 | */ |
| 158 | 0 | public static boolean checkCRC32Checksum(File file, Long crc32Checksum) |
| 159 | 0 | throws IOException { |
| 160 | 0 | logger.debug("checkCRC32Checksum(" + file.toString() + "," |
| 161 | 0 | + crc32Checksum + ")"); |
| 162 | 0 | Long checksum = createCRC32Checksum(file); |
| 163 | 0 | return checksum.equals(crc32Checksum); |
| 164 | } | |
| 165 | 0 | |
| 166 | 0 | /** |
| 167 | * This method builds an array of files. In the case the passed formal | |
| 168 | 0 | * parameter named "file" is a simple file it is returned otherwise if it is |
| 169 | * a directory all sub-files and sub-directories are navigated recursivelly | |
| 170 | * and are added to the resultant array. | |
| 171 | * | |
| 172 | * @param file | |
| 173 | * The file or folder from which the resultant array is built. | |
| 174 | * @return An array of one file if the formal parameter named "file" is a | |
| 175 | * simple file a list of sub-files or sub-directories built | |
| 176 | * recursivelly if the formal parameter is a directory. | |
| 177 | 0 | */ |
| 178 | 0 | public static File[] allFileContent(File file) { |
| 179 | 0 | Vector<File> result = new Vector<File>(); |
| 180 | 0 | |
| 181 | 0 | if (file.isDirectory()) { |
| 182 | 0 | File[] filelist = file.listFiles(); |
| 183 | 0 | for (int i = 0; i < filelist.length; i++) { |
| 184 | 0 | if (filelist[i].isDirectory()) { |
| 185 | 0 | File[] content = allFileContent(filelist[i]); |
| 186 | 0 | for (int k = 0; k < content.length; k++) |
| 187 | 0 | result.add(content[k]); |
| 188 | 0 | } else { |
| 189 | 0 | result.add(filelist[i]); |
| 190 | } | |
| 191 | } | |
| 192 | 0 | } else |
| 193 | 0 | result.add(file); |
| 194 | ||
| 195 | 0 | return result.toArray(new File[0]); |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | 0 | * Deletes all files and subdirectories under directory. |
| 200 | 0 | * |
| 201 | * @param directory | |
| 202 | * The directory that must be deleted. | |
| 203 | 0 | * @return True if all deletions were successful. If a deletion fails, the |
| 204 | * method stops attempting to delete and returns false. | |
| 205 | 0 | */ |
| 206 | public static boolean deleteDirectory(File directory) { | |
| 207 | 0 | if (directory.isDirectory()) { |
| 208 | 0 | String[] children = directory.list(); |
| 209 | 0 | for (int i = 0; i < children.length; i++) { |
| 210 | 0 | File toBeDeleted = new File(directory, children[i]); |
| 211 | 0 | logger.debug("Deleting : " + toBeDeleted); |
| 212 | 0 | boolean success = deleteDirectory(toBeDeleted); |
| 213 | 0 | if (!success) { |
| 214 | 0 | return false; |
| 215 | 0 | } |
| 216 | } | |
| 217 | 0 | } |
| 218 | 0 | // The directory is now empty so delete it |
| 219 | 0 | return directory.delete(); |
| 220 | 0 | } |
| 221 | ||
| 222 | /** | |
| 223 | * This method builds an MD5 Digest of a provided input file. | |
| 224 | * | |
| 225 | * @param file | |
| 226 | * The file whose MD5 Digest must be build. | |
| 227 | * @return The files'MD5 Digest. | |
| 228 | * @throws IOException | |
| 229 | * If the method can't use the filesystem or the provided file | |
| 230 | * doesn't exists. | |
| 231 | 0 | */ |
| 232 | 0 | public static String createMD5Digest(File file) throws IOException { |
| 233 | 0 | String result = ""; |
| 234 | 0 | InputStream inputStream = new FileInputStream(file); |
| 235 | MessageDigest messageDigest; | |
| 236 | try { | |
| 237 | 0 | messageDigest = MessageDigest.getInstance("MD5"); |
| 238 | ||
| 239 | 0 | byte[] buffer = new byte[1024]; |
| 240 | int bytesRead; | |
| 241 | 0 | while ((bytesRead = inputStream.read(buffer)) >= 0) { |
| 242 | 0 | messageDigest.update(buffer, 0, bytesRead); |
| 243 | 0 | } |
| 244 | 0 | inputStream.close(); |
| 245 | 0 | result = new String(Hex.encode(messageDigest.digest())); |
| 246 | 0 | |
| 247 | 0 | logger.debug("The MD5 Digest for: " + file + " is: " + result); |
| 248 | 0 | |
| 249 | 0 | } catch (NoSuchAlgorithmException ex) { |
| 250 | 0 | // TODO Auto-generated catch block |
| 251 | 0 | logger.error(ex.getMessage()); |
| 252 | 0 | ex.printStackTrace(); |
| 253 | 0 | } |
| 254 | 0 | return result; |
| 255 | } | |
| 256 | 0 | |
| 257 | 0 | /** |
| 258 | 0 | * This method checks if the computed MD5 Digest of the input file is equals |
| 259 | * to the provided md5Digest passed as formal argument. | |
| 260 | 0 | * |
| 261 | 0 | * @param file |
| 262 | 0 | * The file whose md5 digest must be compared with the input |
| 263 | 0 | * md5Digest. |
| 264 | 0 | * @param md5Digest |
| 265 | 0 | * The comparation parameter. |
| 266 | 0 | * @return True if the computed md5 digest is queals to the md5Digest, false |
| 267 | * otherwise. | |
| 268 | * @throws IOException | |
| 269 | * If can't be created the InputStream associated to the input | |
| 270 | * File. | |
| 271 | */ | |
| 272 | public static boolean checkMD5Digest(File file, String md5Digest) | |
| 273 | throws IOException { | |
| 274 | 0 | logger.debug("checkMD5Digest(" + file.toString() + "," + md5Digest |
| 275 | + ")"); | |
| 276 | 0 | String digest = createMD5Digest(file); |
| 277 | 0 | return digest.equals(md5Digest); |
| 278 | } | |
| 279 | ||
| 280 | /** | |
| 281 | * This method reads all the lines of the input file returning its content | |
| 282 | * in form of a String. | |
| 283 | * | |
| 284 | * @param file | |
| 285 | * The file whose content must be retrieved. | |
| 286 | * @return The content of the input file. | |
| 287 | * @throws IOException | |
| 288 | * If can't work on the filesystem. | |
| 289 | */ | |
| 290 | public static String fileContent(File file) throws IOException { | |
| 291 | 0 | StringBuffer result = new StringBuffer(); |
| 292 | 0 | FileInputStream inputStream = new FileInputStream(file); |
| 293 | 0 | byte[] buffer = new byte[1024]; |
| 294 | // Read in the decrypted bytes and write the cleartext to out | |
| 295 | 0 | int numRead = 0; |
| 296 | 0 | while ((numRead = inputStream.read(buffer)) >= 0) { |
| 297 | 0 | result.append(new String(buffer).toCharArray(), 0, numRead); |
| 298 | // outputStream.write(buffer, 0, numRead); | |
| 299 | 0 | } |
| 300 | 0 | return result.toString(); |
| 301 | } | |
| 302 | ||
| 303 | public static void copyFile(File input, File output) throws IOException { | |
| 304 | 0 | FileInputStream inputStream = new FileInputStream(input); |
| 305 | 0 | FileOutputStream outputStream = new FileOutputStream(output); |
| 306 | 0 | byte[] buffer = new byte[1024]; |
| 307 | // Read in the decrypted bytes and write the cleartext to out | |
| 308 | 0 | int numRead = 0; |
| 309 | 0 | while ((numRead = inputStream.read(buffer)) >= 0) { |
| 310 | 0 | outputStream.write(buffer, 0, numRead); |
| 311 | 0 | } |
| 312 | 0 | inputStream.close(); |
| 313 | 0 | outputStream.close(); |
| 314 | 0 | } |
| 315 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |