| 1 | package org.europa.together.utils; | |
| 2 | ||
| 3 | import java.io.BufferedReader; | |
| 4 | import java.io.BufferedWriter; | |
| 5 | import java.io.ByteArrayOutputStream; | |
| 6 | import java.io.File; | |
| 7 | import java.io.FileInputStream; | |
| 8 | import java.io.FileNotFoundException; | |
| 9 | import java.io.FileOutputStream; | |
| 10 | import java.io.IOException; | |
| 11 | import java.io.InputStream; | |
| 12 | import java.io.InputStreamReader; | |
| 13 | import java.io.OutputStream; | |
| 14 | import java.net.MalformedURLException; | |
| 15 | import java.net.URL; | |
| 16 | import java.nio.charset.Charset; | |
| 17 | import java.nio.file.Files; | |
| 18 | import java.nio.file.Paths; | |
| 19 | import java.nio.file.StandardOpenOption; | |
| 20 | import java.util.HashSet; | |
| 21 | import java.util.Set; | |
| 22 | import org.europa.together.application.LogbackLogger; | |
| 23 | import org.europa.together.business.Logger; | |
| 24 | import org.europa.together.domain.ByteOrderMark; | |
| 25 | import org.europa.together.domain.LogLevel; | |
| 26 | import org.springframework.context.ApplicationContext; | |
| 27 | import org.springframework.context.support.ClassPathXmlApplicationContext; | |
| 28 | ||
| 29 | /** | |
| 30 | * Some helpful stuff for file handling. | |
| 31 | */ | |
| 32 | public final class FileUtils { | |
| 33 | ||
| 34 | private static final Logger LOGGER = new LogbackLogger(FileUtils.class); | |
| 35 | private static final Charset CHARSET = Charset.forName("UTF-8"); | |
| 36 | ||
| 37 | /** | |
| 38 | * Constructor. | |
| 39 | */ | |
| 40 | private FileUtils() { | |
| 41 | throw new UnsupportedOperationException(); | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Convert an InputStream to an ByteArray. | |
| 46 | * | |
| 47 | * @param input as InputStream | |
| 48 | * @return byte[] as Array | |
| 49 | * @throws java.io.IOException case of error | |
| 50 | */ | |
| 51 | public static byte[] inputStreamToByteArray(final InputStream input) | |
| 52 | throws IOException { | |
| 53 | byte[] byteArray = null; | |
| 54 | byteArray = input.readAllBytes(); | |
| 55 |
1
1. inputStreamToByteArray : replaced return value with null for org/europa/together/utils/FileUtils::inputStreamToByteArray → KILLED |
return byteArray; |
| 56 | } | |
| 57 | ||
| 58 | /** | |
| 59 | * Create from an HEX repersentation integers aa byte array. | |
| 60 | * | |
| 61 | * @param ints as Hex | |
| 62 | * @return byte as Array | |
| 63 | */ | |
| 64 | public static byte[] toBytes(final int... ints) { | |
| 65 | byte[] result = new byte[ints.length]; | |
| 66 |
2
1. toBytes : negated conditional → SURVIVED 2. toBytes : changed conditional boundary → KILLED |
for (int i = 0; i < ints.length; i++) { |
| 67 | result[i] = (byte) ints[i]; | |
| 68 | } | |
| 69 |
1
1. toBytes : replaced return value with null for org/europa/together/utils/FileUtils::toBytes → KILLED |
return result; |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * Write a String to a File.It is possible to create an empty file whitout | |
| 74 | * content. DestinationFile is a full qualitied path including the file | |
| 75 | * name. Sample: /home/user/file.txt | |
| 76 | * | |
| 77 | * @param content as String | |
| 78 | * @param destinationFile as String | |
| 79 | * @return true on success | |
| 80 | * @throws java.io.IOException case of error | |
| 81 | */ | |
| 82 | public static boolean writeStringToFile(final String content, final String destinationFile) | |
| 83 | throws IOException { | |
| 84 | boolean success = false; | |
| 85 | LOGGER.log("writeStringToFile() destination: " + destinationFile, LogLevel.DEBUG); | |
| 86 | BufferedWriter writer | |
| 87 | = Files.newBufferedWriter(Paths.get(destinationFile), CHARSET); | |
| 88 |
1
1. writeStringToFile : negated conditional → KILLED |
if (StringUtils.isEmpty(content)) { |
| 89 | LOGGER.log("File content is empty.", LogLevel.WARN); | |
| 90 | } else { | |
| 91 | LOGGER.log("Count of characters:" | |
| 92 | + content.length(), LogLevel.DEBUG); | |
| 93 | writer.append(content, 0, content.length()); | |
| 94 | } | |
| 95 |
1
1. writeStringToFile : removed call to java/io/BufferedWriter::close → KILLED |
writer.close(); |
| 96 | success = true; | |
| 97 |
2
1. writeStringToFile : replaced boolean return with true for org/europa/together/utils/FileUtils::writeStringToFile → SURVIVED 2. writeStringToFile : replaced boolean return with false for org/europa/together/utils/FileUtils::writeStringToFile → KILLED |
return success; |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Get the bytes of file. It is also possible to choose a dimension for the | |
| 102 | * file size. The following options for <b>dimension</b> are available: | |
| 103 | * kilo, mega, giga and tera. | |
| 104 | * | |
| 105 | * @param filePath as String | |
| 106 | * @param dimension as String | |
| 107 | * @return fileSize as double | |
| 108 | */ | |
| 109 | public static long getFileSize(final String filePath, final String dimension) { | |
| 110 | String mode = "default"; | |
| 111 |
1
1. getFileSize : negated conditional → KILLED |
if (!StringUtils.isEmpty(dimension)) { |
| 112 | mode = dimension; | |
| 113 | } | |
| 114 | File file = new File(filePath); | |
| 115 | long size = 0; | |
| 116 |
1
1. getFileSize : negated conditional → KILLED |
if (file.exists()) { |
| 117 | size = file.length(); | |
| 118 | switch (mode) { | |
| 119 | default: | |
| 120 | break; | |
| 121 | case "kilo": | |
| 122 |
1
1. getFileSize : Replaced long division with multiplication → KILLED |
size = size / Constraints.INT_1024; |
| 123 | break; | |
| 124 | case "mega": | |
| 125 |
2
1. getFileSize : Replaced long division with multiplication → KILLED 2. getFileSize : Replaced long division with multiplication → KILLED |
size = (size / Constraints.INT_1024) / Constraints.INT_1024; |
| 126 | break; | |
| 127 | case "giga": | |
| 128 |
3
1. getFileSize : Replaced long division with multiplication → SURVIVED 2. getFileSize : Replaced long division with multiplication → KILLED 3. getFileSize : Replaced long division with multiplication → KILLED |
size = ((size / Constraints.INT_1024) / Constraints.INT_1024) |
| 129 | / Constraints.INT_1024; | |
| 130 | break; | |
| 131 | case "tera": | |
| 132 |
4
1. getFileSize : Replaced long division with multiplication → SURVIVED 2. getFileSize : Replaced long division with multiplication → SURVIVED 3. getFileSize : Replaced long division with multiplication → SURVIVED 4. getFileSize : Replaced long division with multiplication → SURVIVED |
size = (((size / Constraints.INT_1024) / Constraints.INT_1024) |
| 133 | / Constraints.INT_1024) / Constraints.INT_1024; | |
| 134 | break; | |
| 135 | } | |
| 136 | LOGGER.log("Size of " + file.getName() + " is " + size + " " | |
| 137 | + mode + " Bytes.", LogLevel.DEBUG); | |
| 138 | } else { | |
| 139 | LOGGER.log("File " + filePath + " does not exists!", LogLevel.ERROR); | |
| 140 | } | |
| 141 |
1
1. getFileSize : replaced long return with 0 for org/europa/together/utils/FileUtils::getFileSize → KILLED |
return size; |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Read a file from the class path. | |
| 146 | * | |
| 147 | * @param path to the file | |
| 148 | * @return fielContent as String | |
| 149 | * @throws FileNotFoundException case of error | |
| 150 | */ | |
| 151 | public static String readFileFromClasspath(final String path) | |
| 152 | throws IOException { | |
| 153 | ||
| 154 | StringBuilder content = new StringBuilder(); | |
| 155 | ApplicationContext context = new ClassPathXmlApplicationContext(); | |
| 156 | BufferedReader reader = new BufferedReader( | |
| 157 | new InputStreamReader( | |
| 158 | context.getResource(path).getInputStream(), "UTF8") | |
| 159 | ); | |
| 160 | String line; | |
| 161 |
1
1. readFileFromClasspath : negated conditional → KILLED |
while ((line = reader.readLine()) != null) { |
| 162 | content.append(line + " \n"); | |
| 163 | } | |
| 164 |
1
1. readFileFromClasspath : removed call to java/io/BufferedReader::close → SURVIVED |
reader.close(); |
| 165 |
1
1. readFileFromClasspath : replaced return value with "" for org/europa/together/utils/FileUtils::readFileFromClasspath → KILLED |
return content.toString(); |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * Read a binary remote resource like Image or PDF from a remote source | |
| 170 | * (http / https) as a File object. | |
| 171 | * | |
| 172 | * @param uri as String | |
| 173 | * @return resource as File | |
| 174 | * @throws java.net.MalformedURLException case of error | |
| 175 | */ | |
| 176 | public static byte[] readRemoteResource(final URL uri) | |
| 177 | throws MalformedURLException, IOException { | |
| 178 | try { | |
| 179 | InputStream inputStream = uri.openStream(); | |
| 180 | ||
| 181 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
| 182 | byte[] buffer = new byte[Constraints.INT_1024]; | |
| 183 | ||
| 184 | int numRead; | |
| 185 |
1
1. readRemoteResource : negated conditional → KILLED |
while ((numRead = inputStream.read(buffer)) != -1) { |
| 186 |
1
1. readRemoteResource : removed call to java/io/ByteArrayOutputStream::write → KILLED |
outputStream.write(buffer, 0, numRead); |
| 187 | } | |
| 188 |
1
1. readRemoteResource : removed call to java/io/ByteArrayOutputStream::close → SURVIVED |
outputStream.close(); |
| 189 |
1
1. readRemoteResource : removed call to java/io/InputStream::close → SURVIVED |
inputStream.close(); |
| 190 | ||
| 191 |
1
1. readRemoteResource : replaced return value with null for org/europa/together/utils/FileUtils::readRemoteResource → KILLED |
return outputStream.toByteArray(); |
| 192 | ||
| 193 | } catch (Exception ex) { | |
| 194 | throw new RuntimeException(ex); | |
| 195 | } | |
| 196 | } | |
| 197 | ||
| 198 | /** | |
| 199 | * Reads the full content of a text file in UTF-8.The second parameter | |
| 200 | * charset is optional. | |
| 201 | * | |
| 202 | * @param file as File | |
| 203 | * @param charset as ByteOrderMark (Optional) | |
| 204 | * @return fileContent as String | |
| 205 | * @throws java.io.FileNotFoundException case of error | |
| 206 | * @throws java.io.IOException case of error | |
| 207 | */ | |
| 208 | public static String readFileStream(final File file, final ByteOrderMark... charset) | |
| 209 | throws FileNotFoundException, IOException { | |
| 210 | StringBuilder content = new StringBuilder(); | |
| 211 | InputStreamReader inputStreamReader = null; | |
| 212 | Charset encoding = CHARSET; | |
| 213 |
2
1. readFileStream : negated conditional → KILLED 2. readFileStream : changed conditional boundary → KILLED |
if (charset.length > 0) { |
| 214 | String encodingName = charset[0].toString(); | |
| 215 |
1
1. readFileStream : negated conditional → KILLED |
if (encodingName.equals("NONE")) { |
| 216 | LOGGER.log("ByteOrderMark.NONE converted to UTF-8.", LogLevel.DEBUG); | |
| 217 | } else { | |
| 218 | encoding = Charset.forName(encodingName); | |
| 219 | } | |
| 220 | } | |
| 221 | LOGGER.log("String encoded as " + encoding.displayName(), LogLevel.DEBUG); | |
| 222 | ||
| 223 | inputStreamReader = new InputStreamReader(new FileInputStream(file), encoding); | |
| 224 | int line; | |
| 225 |
1
1. readFileStream : negated conditional → KILLED |
while ((line = inputStreamReader.read()) != -1) { |
| 226 | content.append((char) line); | |
| 227 | } | |
| 228 |
1
1. readFileStream : removed call to java/io/InputStreamReader::close → SURVIVED |
inputStreamReader.close(); |
| 229 |
1
1. readFileStream : replaced return value with "" for org/europa/together/utils/FileUtils::readFileStream → KILLED |
return content.toString(); |
| 230 | } | |
| 231 | ||
| 232 | /** | |
| 233 | * Append content to a given text file at the end of the file in UTF-8. | |
| 234 | * | |
| 235 | * @param filePath as String | |
| 236 | * @param content as String | |
| 237 | * @throws java.io.IOException case of error | |
| 238 | */ | |
| 239 | public static void appendFile(final String filePath, final String content) | |
| 240 | throws IOException { | |
| 241 | Files.write(Paths.get(filePath), content.getBytes(CHARSET), StandardOpenOption.APPEND); | |
| 242 | LOGGER.log(filePath + " extended with " + content.length() + " characters", | |
| 243 | LogLevel.DEBUG); | |
| 244 | } | |
| 245 | ||
| 246 | /** | |
| 247 | * Copy a single File from a source to a destination. | |
| 248 | * | |
| 249 | * @param source as File | |
| 250 | * @param destination as File | |
| 251 | * @throws java.io.FileNotFoundException case of error | |
| 252 | * @throws java.io.IOException case of error | |
| 253 | */ | |
| 254 | public static void copyFile(final File source, final File destination) | |
| 255 | throws FileNotFoundException, IOException { | |
| 256 | OutputStream os; | |
| 257 | try (InputStream is = new FileInputStream(source)) { | |
| 258 | os = new FileOutputStream(destination); | |
| 259 | byte[] buffer = new byte[Constraints.INT_1024]; | |
| 260 | int length; | |
| 261 |
2
1. copyFile : changed conditional boundary → SURVIVED 2. copyFile : negated conditional → SURVIVED |
while ((length = is.read(buffer)) > 0) { |
| 262 |
1
1. copyFile : removed call to java/io/OutputStream::write → SURVIVED |
os.write(buffer, 0, length); |
| 263 | } | |
| 264 | } | |
| 265 |
1
1. copyFile : removed call to java/io/OutputStream::close → SURVIVED |
os.close(); |
| 266 | } | |
| 267 | ||
| 268 | /** | |
| 269 | * Discover a directory and all subdirectories to collect their the files in | |
| 270 | * a List. The list entries are the full path, the filename with file | |
| 271 | * extension. Empty directories will not listed. <br> | |
| 272 | * Example /usr/home/john/file.txt | |
| 273 | * | |
| 274 | * @param directory as File | |
| 275 | * @return a Set of Files | |
| 276 | */ | |
| 277 | public static Set<File> listFileTree(final File directory) { | |
| 278 | Set<File> fileTree = new HashSet<>(); | |
| 279 |
2
1. listFileTree : negated conditional → KILLED 2. listFileTree : negated conditional → KILLED |
if (directory != null && directory.exists() |
| 280 |
1
1. listFileTree : negated conditional → KILLED |
&& directory.isDirectory() |
| 281 |
1
1. listFileTree : negated conditional → KILLED |
&& directory.listFiles().length != 0) { |
| 282 | for (File entry : directory.listFiles()) { | |
| 283 |
1
1. listFileTree : negated conditional → KILLED |
if (entry.isFile()) { |
| 284 | fileTree.add(entry); | |
| 285 | } else { | |
| 286 | fileTree.addAll(listFileTree(entry)); | |
| 287 | } | |
| 288 | } | |
| 289 | } | |
| 290 |
1
1. listFileTree : replaced return value with Collections.emptySet for org/europa/together/utils/FileUtils::listFileTree → KILLED |
return fileTree; |
| 291 | } | |
| 292 | ||
| 293 | } | |
Mutations | ||
| 55 |
1.1 |
|
| 66 |
1.1 2.2 |
|
| 69 |
1.1 |
|
| 88 |
1.1 |
|
| 95 |
1.1 |
|
| 97 |
1.1 2.2 |
|
| 111 |
1.1 |
|
| 116 |
1.1 |
|
| 122 |
1.1 |
|
| 125 |
1.1 2.2 |
|
| 128 |
1.1 2.2 3.3 |
|
| 132 |
1.1 2.2 3.3 4.4 |
|
| 141 |
1.1 |
|
| 161 |
1.1 |
|
| 164 |
1.1 |
|
| 165 |
1.1 |
|
| 185 |
1.1 |
|
| 186 |
1.1 |
|
| 188 |
1.1 |
|
| 189 |
1.1 |
|
| 191 |
1.1 |
|
| 213 |
1.1 2.2 |
|
| 215 |
1.1 |
|
| 225 |
1.1 |
|
| 228 |
1.1 |
|
| 229 |
1.1 |
|
| 261 |
1.1 2.2 |
|
| 262 |
1.1 |
|
| 265 |
1.1 |
|
| 279 |
1.1 2.2 |
|
| 280 |
1.1 |
|
| 281 |
1.1 |
|
| 283 |
1.1 |
|
| 290 |
1.1 |