| 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 |
|
|
| 31 |
|
|
| |
|
| 93.8% |
Uncovered Elements: 8 (130) |
Complexity: 33 |
Complexity Density: 0.35 |
|
| 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 |
|
|
| 39 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 40 |
1 |
private FileUtils() {... |
| 41 |
1 |
throw new UnsupportedOperationException(); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@param |
| 48 |
|
@return |
| 49 |
|
@throws |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 51 |
2 |
public static byte[] inputStreamToByteArray(final InputStream input)... |
| 52 |
|
throws IOException { |
| 53 |
2 |
byte[] byteArray = null; |
| 54 |
2 |
byteArray = input.readAllBytes(); |
| 55 |
1 |
return byteArray; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
@param |
| 62 |
|
@return |
| 63 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 64 |
1 |
public static byte[] toBytes(final int... ints) {... |
| 65 |
1 |
byte[] result = new byte[ints.length]; |
| 66 |
70 |
for (int i = 0; i < ints.length; i++) { |
| 67 |
69 |
result[i] = (byte) ints[i]; |
| 68 |
|
} |
| 69 |
1 |
return result; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| 76 |
|
|
| 77 |
|
@param |
| 78 |
|
@param |
| 79 |
|
@return |
| 80 |
|
@throws |
| 81 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 82 |
7 |
public static boolean writeStringToFile(final String content, final String destinationFile)... |
| 83 |
|
throws IOException { |
| 84 |
7 |
boolean success = false; |
| 85 |
7 |
LOGGER.log("writeStringToFile() destination: " + destinationFile, LogLevel.DEBUG); |
| 86 |
7 |
BufferedWriter writer |
| 87 |
|
= Files.newBufferedWriter(Paths.get(destinationFile), CHARSET); |
| 88 |
5 |
if (StringUtils.isEmpty(content)) { |
| 89 |
1 |
LOGGER.log("File content is empty.", LogLevel.WARN); |
| 90 |
|
} else { |
| 91 |
4 |
LOGGER.log("Count of characters:" |
| 92 |
|
+ content.length(), LogLevel.DEBUG); |
| 93 |
4 |
writer.append(content, 0, content.length()); |
| 94 |
|
} |
| 95 |
5 |
writer.close(); |
| 96 |
5 |
success = true; |
| 97 |
5 |
return success; |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
|
@param |
| 106 |
|
@param |
| 107 |
|
@return |
| 108 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 7 |
Complexity Density: 0.28 |
|
| 109 |
10 |
public static long getFileSize(final String filePath, final String dimension) {... |
| 110 |
10 |
String mode = "default"; |
| 111 |
10 |
if (!StringUtils.isEmpty(dimension)) { |
| 112 |
5 |
mode = dimension; |
| 113 |
|
} |
| 114 |
10 |
File file = new File(filePath); |
| 115 |
10 |
long size = 0; |
| 116 |
10 |
if (file.exists()) { |
| 117 |
9 |
size = file.length(); |
| 118 |
9 |
switch (mode) { |
| 119 |
5 |
default: |
| 120 |
5 |
break; |
| 121 |
1 |
case "kilo": |
| 122 |
1 |
size = size / Constraints.INT_1024; |
| 123 |
1 |
break; |
| 124 |
1 |
case "mega": |
| 125 |
1 |
size = (size / Constraints.INT_1024) / Constraints.INT_1024; |
| 126 |
1 |
break; |
| 127 |
1 |
case "giga": |
| 128 |
1 |
size = ((size / Constraints.INT_1024) / Constraints.INT_1024) |
| 129 |
|
/ Constraints.INT_1024; |
| 130 |
1 |
break; |
| 131 |
1 |
case "tera": |
| 132 |
1 |
size = (((size / Constraints.INT_1024) / Constraints.INT_1024) |
| 133 |
|
/ Constraints.INT_1024) / Constraints.INT_1024; |
| 134 |
1 |
break; |
| 135 |
|
} |
| 136 |
9 |
LOGGER.log("Size of " + file.getName() + " is " + size + " " |
| 137 |
|
+ mode + " Bytes.", LogLevel.DEBUG); |
| 138 |
|
} else { |
| 139 |
1 |
LOGGER.log("File " + filePath + " does not exists!", LogLevel.ERROR); |
| 140 |
|
} |
| 141 |
10 |
return size; |
| 142 |
|
} |
| 143 |
|
|
| 144 |
|
|
| 145 |
|
|
| 146 |
|
|
| 147 |
|
@param |
| 148 |
|
@return |
| 149 |
|
@throws |
| 150 |
|
|
| |
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 151 |
1 |
public static String readFileFromClasspath(final String path)... |
| 152 |
|
throws IOException { |
| 153 |
|
|
| 154 |
1 |
StringBuilder content = new StringBuilder(); |
| 155 |
1 |
ApplicationContext context = new ClassPathXmlApplicationContext(); |
| 156 |
1 |
BufferedReader reader = new BufferedReader( |
| 157 |
|
new InputStreamReader( |
| 158 |
|
context.getResource(path).getInputStream(), "UTF8") |
| 159 |
|
); |
| 160 |
1 |
String line; |
| 161 |
? |
while ((line = reader.readLine()) != null) { |
| 162 |
1 |
content.append(line + " \n"); |
| 163 |
|
} |
| 164 |
1 |
reader.close(); |
| 165 |
1 |
return content.toString(); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| 169 |
|
|
| 170 |
|
|
| 171 |
|
|
| 172 |
|
@param |
| 173 |
|
@return |
| 174 |
|
@throws |
| 175 |
|
|
| |
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 176 |
2 |
public static byte[] readRemoteResource(final URL uri)... |
| 177 |
|
throws MalformedURLException, IOException { |
| 178 |
2 |
try { |
| 179 |
2 |
InputStream inputStream = uri.openStream(); |
| 180 |
|
|
| 181 |
1 |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 182 |
1 |
byte[] buffer = new byte[Constraints.INT_1024]; |
| 183 |
|
|
| 184 |
1 |
int numRead; |
| 185 |
? |
while ((numRead = inputStream.read(buffer)) != -1) { |
| 186 |
1 |
outputStream.write(buffer, 0, numRead); |
| 187 |
|
} |
| 188 |
1 |
outputStream.close(); |
| 189 |
1 |
inputStream.close(); |
| 190 |
|
|
| 191 |
1 |
return outputStream.toByteArray(); |
| 192 |
|
|
| 193 |
|
} catch (Exception ex) { |
| 194 |
1 |
throw new RuntimeException(ex); |
| 195 |
|
} |
| 196 |
|
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
|
| 200 |
|
|
| 201 |
|
|
| 202 |
|
@param |
| 203 |
|
@param |
| 204 |
|
@return |
| 205 |
|
@throws |
| 206 |
|
@throws |
| 207 |
|
|
| |
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 4 |
Complexity Density: 0.27 |
|
| 208 |
36 |
public static String readFileStream(final File file, final ByteOrderMark... charset)... |
| 209 |
|
throws FileNotFoundException, IOException { |
| 210 |
36 |
StringBuilder content = new StringBuilder(); |
| 211 |
36 |
InputStreamReader inputStreamReader = null; |
| 212 |
36 |
Charset encoding = CHARSET; |
| 213 |
36 |
if (charset.length > 0) { |
| 214 |
7 |
String encodingName = charset[0].toString(); |
| 215 |
7 |
if (encodingName.equals("NONE")) { |
| 216 |
1 |
LOGGER.log("ByteOrderMark.NONE converted to UTF-8.", LogLevel.DEBUG); |
| 217 |
|
} else { |
| 218 |
6 |
encoding = Charset.forName(encodingName); |
| 219 |
|
} |
| 220 |
|
} |
| 221 |
36 |
LOGGER.log("String encoded as " + encoding.displayName(), LogLevel.DEBUG); |
| 222 |
|
|
| 223 |
36 |
inputStreamReader = new InputStreamReader(new FileInputStream(file), encoding); |
| 224 |
33 |
int line; |
| 225 |
? |
while ((line = inputStreamReader.read()) != -1) { |
| 226 |
16202 |
content.append((char) line); |
| 227 |
|
} |
| 228 |
33 |
inputStreamReader.close(); |
| 229 |
33 |
return content.toString(); |
| 230 |
|
} |
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
@param |
| 236 |
|
@param |
| 237 |
|
@throws |
| 238 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 239 |
2 |
public static void appendFile(final String filePath, final String content)... |
| 240 |
|
throws IOException { |
| 241 |
2 |
Files.write(Paths.get(filePath), content.getBytes(CHARSET), StandardOpenOption.APPEND); |
| 242 |
1 |
LOGGER.log(filePath + " extended with " + content.length() + " characters", |
| 243 |
|
LogLevel.DEBUG); |
| 244 |
|
} |
| 245 |
|
|
| 246 |
|
|
| 247 |
|
|
| 248 |
|
|
| 249 |
|
@param |
| 250 |
|
@param |
| 251 |
|
@throws |
| 252 |
|
@throws |
| 253 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 4 |
Complexity Density: 0.44 |
|
| 254 |
4 |
public static void copyFile(final File source, final File destination)... |
| 255 |
|
throws FileNotFoundException, IOException { |
| 256 |
4 |
OutputStream os; |
| 257 |
4 |
try (InputStream is = new FileInputStream(source)) { |
| 258 |
3 |
os = new FileOutputStream(destination); |
| 259 |
3 |
byte[] buffer = new byte[Constraints.INT_1024]; |
| 260 |
3 |
int length; |
| 261 |
? |
while ((length = is.read(buffer)) > 0) { |
| 262 |
9 |
os.write(buffer, 0, length); |
| 263 |
|
} |
| 264 |
|
} |
| 265 |
3 |
os.close(); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
|
|
| 269 |
|
|
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
@param |
| 275 |
|
@return |
| 276 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 6 |
Complexity Density: 0.86 |
|
| 277 |
8 |
public static Set<File> listFileTree(final File directory) {... |
| 278 |
8 |
Set<File> fileTree = new HashSet<>(); |
| 279 |
8 |
if (directory != null && directory.exists() |
| 280 |
|
&& directory.isDirectory() |
| 281 |
|
&& directory.listFiles().length != 0) { |
| 282 |
4 |
for (File entry : directory.listFiles()) { |
| 283 |
6 |
if (entry.isFile()) { |
| 284 |
3 |
fileTree.add(entry); |
| 285 |
|
} else { |
| 286 |
3 |
fileTree.addAll(listFileTree(entry)); |
| 287 |
|
} |
| 288 |
|
} |
| 289 |
|
} |
| 290 |
8 |
return fileTree; |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
} |