-
Notifications
You must be signed in to change notification settings - Fork 5
/
ModelTools.java
155 lines (140 loc) · 5.56 KB
/
ModelTools.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.genericsystem.cv.model;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import javax.xml.bind.DatatypeConverter;
/**
* This class contains only static methods, which can be used as general purpose tools.
*
* @author Pierrik Lassalas
*/
public class ModelTools {
/**
* Default {@code pattern} for formatting the {@link LocalDateTime} objects.
*/
public static final String DATE_TIME_FORMAT = "uuuu-MM-dd HH:mm:ss";
public static void main(String[] args) {
Path path = Paths.get(System.getProperty("user.home"), "Downloads", "photosafpa.zip");
File file = path.toFile();
System.out.println(file.getAbsolutePath());
if (!file.exists()) {
System.out.println("File does not exists");
System.exit(0);
}
String hash = hashCode(path);
String md5 = getHashFromFile(path, "md5");
String sha1 = getHashFromFile(path, "sha-1");
String sha256 = getHashFromFile(path, "sha-256");
String sha512 = getHashFromFile(path, "sha-512");
System.out.println("hash: " + hash + " - " + hash.length());
System.out.println("md5: " + md5 + " - " + md5.length());
System.out.println("sha-1: " + sha1 + " - " + sha1.length());
System.out.println("sha-256: " + sha256 + " - " + sha256.length());
System.out.println("sha-512: " + sha512 + " - " + sha512.length());
}
/**
* Generate a hash code from a file. Care must be taken to verify that the file exists before calling this method (otherwise an exception is thrown).
*
* @param path - the {@link Path} of the file
* @param algorithm - the desired algorithm used to generate the has code. Every implementation of the Java platform is required to support the following standard MessageDigest algorithms: MD5, SHA-1, SHA-256
* @return the computed {@code hash} as a hexadecimal String
* @throws RuntimeException when the specified algorithm is not found, or when the file could not be read
*/
public static String getHashFromFile(Path path, String algorithm) throws RuntimeException {
MessageDigest md;
try {
md = MessageDigest.getInstance(algorithm);
md.update(Files.readAllBytes(path));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unable to generate a hash code (no such algorithm)", e);
} catch (IOException e) {
throw new RuntimeException("Unable to generate a hash code (problem reading the file)", e);
}
byte[] hash = md.digest();
return DatatypeConverter.printHexBinary(hash);
}
/**
* Generate a simple hash from a file. Care must be taken to verify that the file exists before calling this method (otherwise an exception is thrown).
*
* @param path - the {@link Path} of the file
* @return the computed {@code hash} as a hexadecimal String
* @throws RuntimeException when the specified file could not be read
*/
private static String hashCode(Path path) throws RuntimeException {
int hashCode;
try {
hashCode = Arrays.hashCode(Files.readAllBytes(path));
} catch (IOException e) {
throw new RuntimeException("Unable to generate a hash code (problem reading the file", e);
}
return Integer.toHexString(hashCode).toUpperCase();
}
/**
* Return the current {@link LocalDateTime} as the number of milliseconds since Epoch.
*
* @return a {@code Long} representing the local timestamp
*/
public static Long getCurrentDate() {
LocalDateTime ldt = LocalDateTime.now();
return ldt.atZone(ZoneOffset.systemDefault()).toInstant().toEpochMilli();
}
/**
* Convert a {@code String} into a {@link LocalDateTime} using default pattern.
*
* @param date the date in a {@code String} format
* @return the corresponding date object
*/
public static LocalDateTime getCurrentDateFromString(String date) {
return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(DATE_TIME_FORMAT));
}
/**
* Convert a {@code String} into a {@link LocalDateTime} using a custom pattern.
*
* @param date - the date in a {@code String} format
* @param pattern - the pattern to be used for conversion
* @return the corresponding date object
*/
public static LocalDateTime getCurrentDateFromString(String date, String pattern) {
return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern));
}
/**
* Format a {@link LocalDateTime} using the default pattern.
*
* @param timestamp - the timestamp to convert
* @return a {@code String} representation of the timestamp
*/
public static String formatDate(Long timestamp) {
return formatDate(timestamp, DATE_TIME_FORMAT);
}
/**
* Format a {@link LocalDateTime} using a custom pattern.
*
* @param timestamp - the timestamp to convert
* @param pattern - the pattern to be used for conversion
* @return a {@code String} representation of the timestamp
*/
public static String formatDate(Long timestamp, String pattern) {
LocalDateTime ldt = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneOffset.systemDefault());
return ldt.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* Get the ImgClass name of a given image.
*
* @param imag ePath - the Path of the image
* @return a String representing the ImgClass name
*/
public static String getImgClass(final Path imagePath) {
final Path imgClassDirectory = imagePath.getParent();
final String docType = imgClassDirectory.getName(imgClassDirectory.getNameCount() - 1).toString();
return docType;
}
}