25
25
import java .net .URL ;
26
26
import java .nio .charset .StandardCharsets ;
27
27
import java .nio .file .Files ;
28
+ import java .nio .file .NoSuchFileException ;
28
29
import java .nio .file .Path ;
29
30
import java .nio .file .Paths ;
30
31
import java .nio .file .StandardCopyOption ;
@@ -80,6 +81,41 @@ public enum ModelType {
80
81
81
82
private static Map <String , Map <ModelType , String >> availableModels ;
82
83
84
+ /**
85
+ * Checks if a model of the specified {@code modelType} has been downloaded already
86
+ * for a particular {@code language}.
87
+ *
88
+ * @param language The ISO language code of the requested model.
89
+ * @param modelType The {@link DownloadUtil.ModelType type} of model.
90
+ * @return {@code true} if a model exists locally, {@code false} otherwise.
91
+ * @throws IOException Thrown if IO errors occurred or the model is invalid.
92
+ */
93
+ static boolean existsModel (String language , ModelType modelType ) throws IOException {
94
+ Map <ModelType , String > modelsByLanguage = getAvailableModels ().get (language );
95
+ if (modelsByLanguage == null ) {
96
+ return false ;
97
+ } else {
98
+ final String url = modelsByLanguage .get (modelType );
99
+ if (url != null ) {
100
+ final Path homeDirectory = Paths .get (System .getProperty ("OPENNLP_DOWNLOAD_HOME" ,
101
+ System .getProperty ("user.home" ))).resolve (".opennlp" );
102
+ final String filename = url .substring (url .lastIndexOf ("/" ) + 1 );
103
+ final Path localFile = Paths .get (homeDirectory .toString (), filename );
104
+ boolean exists ;
105
+ try {
106
+ // if this does not throw the requested model exists AND is valid!
107
+ validateModel (new URL (url + ".sha512" ), localFile );
108
+ exists = true ;
109
+ } catch (NoSuchFileException e ) {
110
+ exists = false ;
111
+ }
112
+ return exists ;
113
+ } else {
114
+ return false ;
115
+ }
116
+ }
117
+ }
118
+
83
119
/**
84
120
* Triggers a download for the specified {@link DownloadUtil.ModelType}.
85
121
*
@@ -94,7 +130,7 @@ public static <T extends BaseModel> T downloadModel(String language, ModelType m
94
130
Class <T > type ) throws IOException {
95
131
96
132
if (getAvailableModels ().containsKey (language )) {
97
- final String url = ( getAvailableModels ().get (language ).get (modelType ) );
133
+ final String url = getAvailableModels ().get (language ).get (modelType );
98
134
if (url != null ) {
99
135
return downloadModel (new URL (url ), type );
100
136
}
@@ -134,17 +170,14 @@ public static <T extends BaseModel> T downloadModel(URL url, Class<T> type) thro
134
170
final Path localFile = Paths .get (homeDirectory .toString (), filename );
135
171
136
172
if (!Files .exists (localFile )) {
137
- logger .debug ("Downloading model from {} to {}." , url , localFile );
173
+ logger .debug ("Downloading model to {}." , localFile );
138
174
139
175
try (final InputStream in = url .openStream ()) {
140
176
Files .copy (in , localFile , StandardCopyOption .REPLACE_EXISTING );
141
177
}
142
-
143
178
validateModel (new URL (url + ".sha512" ), localFile );
144
-
145
179
logger .debug ("Download complete." );
146
180
} else {
147
- System .out .println ("Model file already exists. Skipping download." );
148
181
logger .debug ("Model file '{}' already exists. Skipping download." , filename );
149
182
}
150
183
@@ -167,7 +200,7 @@ public static Map<String, Map<ModelType, String>> getAvailableModels() {
167
200
}
168
201
169
202
/**
170
- * Validates the downloaded model.
203
+ * Validates a downloaded model via the specified {@link Path downloadedModel path} .
171
204
*
172
205
* @param sha512 the url to get the sha512 hash
173
206
* @param downloadedModel the model file to check
@@ -187,8 +220,8 @@ private static void validateModel(URL sha512, Path downloadedModel) throws IOExc
187
220
// Validate SHA512 checksum
188
221
final String actualChecksum = calculateSHA512 (downloadedModel );
189
222
if (!actualChecksum .equalsIgnoreCase (expectedChecksum )) {
190
- throw new IOException ("SHA512 checksum validation failed. Expected: "
191
- + expectedChecksum + ", but got: " + actualChecksum );
223
+ throw new IOException ("SHA512 checksum validation failed for " + downloadedModel . getFileName () +
224
+ ". Expected: " + expectedChecksum + ", but got: " + actualChecksum );
192
225
}
193
226
}
194
227
@@ -198,6 +231,7 @@ private static String calculateSHA512(Path file) throws IOException {
198
231
try (InputStream fis = Files .newInputStream (file );
199
232
DigestInputStream dis = new DigestInputStream (fis , digest )) {
200
233
byte [] buffer = new byte [4096 ];
234
+ //noinspection StatementWithEmptyBody
201
235
while (dis .read (buffer ) != -1 ) {
202
236
// Reading the file to update the digest
203
237
}
0 commit comments