-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIoHelp.java
673 lines (628 loc) · 23.7 KB
/
IoHelp.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/*
* Copyright (C) 2017-2023 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.core.util;
import com.here.naksha.lib.core.NakshaVersion;
import com.here.naksha.lib.core.util.json.JsonSerializable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;
import org.jetbrains.annotations.ApiStatus.AvailableSince;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* IO helper class.
*/
@SuppressWarnings("unused")
public final class IoHelp {
/**
* The default thread safe IO helper instance to be used by the static methods.
*/
public static volatile @NotNull IoHelp defaultInstance = new IoHelp();
/**
* Find a resource from the class-path.
*
* @param resource The resource name, which should start with a slash.
* @return The URL of the resource.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public @NotNull URL findResource(@NotNull String resource) throws UncheckedIOException {
final URL url = ClassLoader.getSystemResource(resource);
if (url == null) {
throw new UncheckedIOException(new FileNotFoundException(resource));
}
return url;
}
/**
* Find a resource from the class-path.
*
* @param resource The resource name.
* @param relativeTo The class relative to which to search for the resource.
* @return The URL of the resource.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public @NotNull URL findResource(@NotNull String resource, @NotNull Class<?> relativeTo)
throws UncheckedIOException {
final URL url = relativeTo.getResource(resource);
if (url == null) {
throw new UncheckedIOException(new FileNotFoundException(resource));
}
return url;
}
/**
* Read a resource from the class-path.
*
* @param resource The resource name, which should start with a slash.
* @return The input stream to the resource.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static @NotNull InputStream openResource(@NotNull String resource) throws UncheckedIOException {
final IoHelp defaultInstance = IoHelp.defaultInstance;
return defaultInstance.openResource(defaultInstance.findResource(resource));
}
/**
* Read a resource from the class-path.
*
* @param resource The resource location.
* @return The input stream to the resource.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public @NotNull InputStream openResource(@NotNull URL resource) throws UncheckedIOException {
try {
return resource.openStream();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Read a resource from the JAR.
*
* @param resource The resource name, which should start with a slash.
* @return The resource as string.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static @NotNull String readResource(@NotNull String resource) throws UncheckedIOException {
final IoHelp defaultInstance = IoHelp.defaultInstance;
return defaultInstance.readResource(defaultInstance.findResource(resource));
}
/**
* Read a resource from the JAR.
*
* @param resource The resource URL.
* @return The resource as string.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public @NotNull String readResource(@NotNull URL resource) throws UncheckedIOException {
final InputStream is = openResource(resource);
try (final BufferedReader buffer = new BufferedReader(new InputStreamReader(is))) {
return buffer.lines().collect(Collectors.joining("\n"));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Read a file from the resources.
*
* @param filename The filename of the file to read, e.g. "foo.json".
* @return The loaded file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static byte @NotNull [] readResourceBytes(@NotNull String filename) throws UncheckedIOException {
final IoHelp defaultInstance = IoHelp.defaultInstance;
return defaultInstance.readResourceBytes(defaultInstance.findResource(filename));
}
/**
* Read a file from the resources.
*
* @param location The location of the file to read, e.g. "foo.json".
* @return The loaded file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public byte @NotNull [] readResourceBytes(@NotNull URL location) throws UncheckedIOException {
try (final InputStream is = openResource(location)) {
return readNBytes(is, Integer.MAX_VALUE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
private static final int DEFAULT_BUFFER_SIZE = 8192;
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
/**
* Reads up to a specified number of bytes from the input stream. This method blocks until the requested number of bytes has been read,
* end of stream is detected, or an exception is thrown. This method does not close the input stream.
*
* <p>The length of the returned array equals the number of bytes read from the stream. If {@code
* len} is zero, then no bytes are read and an empty byte array is returned. Otherwise, up to {@code len} bytes are read from the stream.
* Fewer than {@code len} bytes may be read if end of stream is encountered.
*
* <p>When this stream reaches end of stream, further invocations of this method will return an
* empty byte array.
*
* <p>Note that this method is intended for simple cases where it is convenient to read the
* specified number of bytes into a byte array. The total amount of memory allocated by this method is proportional to the number of bytes
* read from the stream which is bounded by {@code len}. Therefore, the method may be safely called with very large values of {@code len}
* provided sufficient memory is available.
*
* <p>The behavior for the case where the input stream is <i>asynchronously closed</i>, or the
* thread interrupted during the read, is highly input stream specific, and therefore not specified.
*
* <p>If an I/O error occurs reading from the input stream, then it may do so after some, but not
* all, bytes have been read. Consequently the input stream may not be at end of stream and may be in an inconsistent state. It is
* strongly recommended that the stream be promptly closed if an I/O error occurs.
*
* @param is the input stream to read from.
* @param len the maximum number of bytes to read
* @return a byte array containing the bytes read from this input stream
* @throws IllegalArgumentException if {@code length} is negative.
* @throws UncheckedIOException if an I/O error occurs.
* @throws OutOfMemoryError if an array of the required size cannot be allocated.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static byte @NotNull [] readNBytes(final @NotNull InputStream is, int len) {
if (len < 0) {
throw new IllegalArgumentException("len < 0");
}
try {
List<byte[]> bufs = null;
byte[] result = null;
int total = 0;
int remaining = len;
int n;
do {
byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
int nread = 0;
// read to EOF which may read more or less than buffer size
while ((n = is.read(buf, nread, Math.min(buf.length - nread, remaining))) > 0) {
nread += n;
remaining -= n;
}
if (nread > 0) {
if (MAX_BUFFER_SIZE - total < nread) {
throw new OutOfMemoryError("Required array size too large");
}
if (nread < buf.length) {
buf = Arrays.copyOfRange(buf, 0, nread);
}
total += nread;
if (result == null) {
result = buf;
} else {
if (bufs == null) {
bufs = new ArrayList<>();
bufs.add(result);
}
bufs.add(buf);
}
}
// if the last call to read returned -1 or the number of bytes
// requested have been read then break
} while (n >= 0 && remaining > 0);
if (bufs == null) {
if (result == null) {
return new byte[0];
}
return result.length == total ? result : Arrays.copyOf(result, total);
}
result = new byte[total];
int offset = 0;
remaining = total;
for (byte[] b : bufs) {
int count = Math.min(b.length, remaining);
System.arraycopy(b, 0, result, offset, count);
offset += count;
remaining -= count;
}
return result;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* The loaded bytes.
*/
public static class LoadedBytes {
private final @NotNull String path;
private final byte @NotNull [] bytes;
/**
* @param path The path loaded.
* @param bytes The bytes loaded.
*/
protected LoadedBytes(@NotNull String path, byte @NotNull [] bytes) {
this.path = path;
this.bytes = bytes;
}
public @NotNull String getPath() {
return path;
}
public byte @NotNull [] getBytes() {
return bytes;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LoadedBytes that = (LoadedBytes) o;
return Objects.equals(path, that.path) && Arrays.equals(bytes, that.bytes);
}
@Override
public int hashCode() {
int result = Objects.hash(path);
result = 31 * result + Arrays.hashCode(bytes);
return result;
}
}
/**
* The loaded config.
*
* @param <CONFIG> The config-type.
*/
public static class LoadedConfig<CONFIG> {
private final @NotNull CONFIG config;
private final @NotNull String path;
/**
* @param path The path loaded.
* @param config The loaded config.
*/
protected LoadedConfig(@NotNull String path, @NotNull CONFIG config) {
this.path = path;
this.config = config;
}
public @NotNull CONFIG getConfig() {
return config;
}
public @NotNull String getPath() {
return path;
}
}
/**
* Read a file either from the given search paths or when not found there from "~/.config/{appName}", and eventually from the resources.
*
* @param filename The filename of the file to read, e.g. "auth/jwt.key".
* @param tryWorkingDirectory If the filename should be used relative to the working directory (or as absolute file path).
* @param appName The name of the application, when searching the default location (~/.config/{appName}).
* @param searchPaths Optional paths to search along, before trying the default location.
* @return The loaded configuration file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static <CONFIG> @NotNull LoadedConfig<CONFIG> readConfigFromHomeOrResource(
@NotNull String filename,
boolean tryWorkingDirectory,
@NotNull String appName,
@NotNull Class<CONFIG> configClass,
@Nullable String... searchPaths)
throws UncheckedIOException {
final IoHelp defaultInstance = IoHelp.defaultInstance;
return defaultInstance.readConfigFromHomeOrResource(
configClass, filename, appName, tryWorkingDirectory, searchPaths);
}
/**
* Read a file either from the given search paths or when not found there from "~/.config/{appName}", and eventually from the resources.
*
* @param filename The filename of the file to read, e.g. "auth/jwt.key".
* @param tryWorkingDirectory If the filename should be used relative to the working directory (or as absolute file path).
* @param appName The name of the application, when searching the default location (~/.config/{appName}).
* @param searchPaths Optional paths to search along, before trying the default location.
* @return The loaded configuration file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public <CONFIG> @NotNull LoadedConfig<CONFIG> readConfigFromHomeOrResource(
@NotNull Class<CONFIG> configClass,
@NotNull String filename,
@NotNull String appName,
boolean tryWorkingDirectory,
@Nullable String... searchPaths)
throws UncheckedIOException {
final LoadedBytes loadedBytes =
readBytesFromHomeOrResource(filename, tryWorkingDirectory, appName, searchPaths);
final CONFIG config = JsonSerializable.deserialize(loadedBytes.bytes, configClass);
if (config == null) {
throw new UncheckedIOException(new FileNotFoundException(filename));
}
return new LoadedConfig<>(loadedBytes.path, config);
}
/**
* Read a file either from the given search paths or when not found there from "~/.config/{appName}", and eventually from the resources.
*
* @param filename The filename of the file to read, e.g. "auth/jwt.key".
* @param tryWorkingDirectory If the filename should be used relative to the working directory (or as absolute file path).
* @param appName The name of the application, when searching the default location (~/.config/{appName}).
* @param searchPaths Optional paths to search along, before trying the default location.
* @return The loaded configuration file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static @NotNull LoadedBytes readBytesFromHomeOrResource(
@NotNull String filename,
boolean tryWorkingDirectory,
@NotNull String appName,
@Nullable String... searchPaths) {
final IoHelp defaultInstance = IoHelp.defaultInstance;
return defaultInstance.readBytesFromHomeOrResource(filename, appName, tryWorkingDirectory, searchPaths);
}
/**
* Read a file either from the given search paths or when not found there from "~/.config/{appName}", and eventually from the resources.
*
* @param filename The filename of the file to read, e.g. "auth/jwt.key".
* @param tryWorkingDirectory If the filename should be used relative to the working directory (or as absolute file path).
* @param appName The name of the application, when searching the default location (~/.config/{appName}).
* @param searchPaths Optional paths to search along, before trying the default location.
* @return The loaded configuration file.
* @throws UncheckedIOException If any error occurred.
*/
@AvailableSince(NakshaVersion.v2_0_10)
public @NotNull LoadedBytes readBytesFromHomeOrResource(
@NotNull String filename,
@NotNull String appName,
boolean tryWorkingDirectory,
@Nullable String... searchPaths) {
try {
//noinspection ConstantConditions
if (filename == null) {
throw new FileNotFoundException("null");
}
Path filePath;
if (tryWorkingDirectory) {
filePath = Paths.get(filename).toAbsolutePath();
final File file = filePath.toFile();
if (file.exists() && file.isFile() && file.canRead()) {
return new LoadedBytes(filePath.toString(), Files.readAllBytes(filePath));
}
}
final char first = filename.charAt(0);
if (first == '/' || first == '\\') {
filename = filename.substring(1);
}
for (final String path : searchPaths) {
if (path == null) {
continue;
}
filePath = Paths.get(path, filename).toAbsolutePath();
final File file = filePath.toFile();
if (file.exists() && file.isFile() && file.canRead()) {
return new LoadedBytes(filePath.toString(), Files.readAllBytes(filePath));
}
}
final String userHome = System.getProperty("user.home");
if (userHome != null) {
filePath = Paths.get(userHome, ".config", appName, filename).toAbsolutePath();
} else {
filePath = null;
}
if (filePath != null) {
final File file = filePath.toFile();
if (file.exists() && file.isFile() && file.canRead()) {
return new LoadedBytes(filePath.toString(), Files.readAllBytes(filePath));
}
}
final URL url = findResource(filename);
try (final InputStream is = url.openStream()) {
return new LoadedBytes(url.toString(), readNBytes(is, Integer.MAX_VALUE));
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Formatting helper.
*
* @param format The format string.
* @param args The arguments.
* @return The formatted string.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static @NotNull String format(@NotNull String format, @Nullable Object... args) {
return String.format(Locale.US, format, args);
}
/**
* Simplified toString implementation.
*
* @param o The object to stringify.
* @return The stringified version.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static @NotNull String asString(@Nullable Object o) {
if (o == null) {
return "null";
}
return o.toString();
}
/**
* Parses the given value.
*
* @param value The value to parse.
* @param type The class of the return-type.
* @param <T> The type to return.
* @return The parsed value, or the default value.
* @throws IllegalArgumentException If the value does not match the expected type or is {@code null}.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static <T> @NotNull T parseValue(@Nullable Object value, @NotNull Class<T> type) {
final T v = parseNullableValue(value, type, null, false);
if (v == null) {
throw new IllegalArgumentException("Value must not be null");
}
return v;
}
/**
* Parses the given value.
*
* @param value The value to parse.
* @param type The class of the return-type.
* @param defaultValue The default value, when no value given.
* @param ignoreInvalid if {@code false}, than an {@link IllegalArgumentException} is thrown, if the value is of an invalid type.
* @param <T> The type to return.
* @return The parsed value, or the default value.
* @throws IllegalArgumentException If {@code ignoreInvalid} if {@code false} and the value does not match the expected type.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static <T> @NotNull T parseValue(
@Nullable Object value, @NotNull Class<T> type, @NotNull T defaultValue, boolean ignoreInvalid) {
final T v = parseNullableValue(value, type, defaultValue, ignoreInvalid);
assert v != null;
return v;
}
/**
* Parses the given value.
*
* @param value The value to parse.
* @param type The class of the return-type.
* @param defaultValue The default value, when no value given.
* @param ignoreInvalid if {@code false}, than an {@link IllegalArgumentException} is thrown, if the value is of an invalid type.
* @param <T> The type to return.
* @return The parsed value, or the default value.
* @throws IllegalArgumentException If {@code ignoreInvalid} if {@code false} and the value does not match the expected type.
*/
@AvailableSince(NakshaVersion.v2_0_5)
public static <T> @Nullable T parseNullableValue(
@Nullable Object value, @NotNull Class<T> type, @Nullable T defaultValue, boolean ignoreInvalid) {
if (value == null) {
return defaultValue;
}
if (type.isAssignableFrom(value.getClass())) {
return type.cast(value);
}
if (value instanceof String) {
String string = (String) value;
if (type == Boolean.class) {
if ("true".equalsIgnoreCase(string)) {
return type.cast(Boolean.TRUE);
}
if ("false".equalsIgnoreCase(string)) {
return type.cast(Boolean.FALSE);
}
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected boolean");
}
if (type == Byte.class) {
try {
return type.cast(Byte.parseByte(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected byte");
}
}
if (type == Short.class) {
try {
return type.cast(Short.parseShort(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected short");
}
}
if (type == Integer.class) {
try {
return type.cast(Integer.parseInt(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected int");
}
}
if (type == Long.class) {
try {
return type.cast(Long.parseLong(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected long");
}
}
if (type == Float.class) {
try {
return type.cast(Float.parseFloat(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected float");
}
}
if (type == Double.class) {
try {
return type.cast(Double.parseDouble(string));
} catch (NumberFormatException e) {
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected double");
}
}
}
if (value instanceof Number) {
Number number = (Number) value;
if (type == Byte.class) {
return type.cast(number.byteValue());
}
if (type == Short.class) {
return type.cast(number.shortValue());
}
if (type == Integer.class) {
return type.cast(number.intValue());
}
if (type == Long.class) {
return type.cast(number.longValue());
}
if (type == Float.class) {
return type.cast(number.floatValue());
}
if (type == Double.class) {
return type.cast(number.doubleValue());
}
}
if (ignoreInvalid) {
return defaultValue;
}
throw new IllegalArgumentException("Invalid type, expected " + type.getName());
}
}