1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30
31 import org.eclipse.aether.spi.io.ChecksumProcessor;
32 import org.eclipse.aether.spi.io.PathProcessor;
33
34 import static java.util.Objects.requireNonNull;
35
36
37
38
39 @Singleton
40 @Named
41 public class DefaultChecksumProcessor implements ChecksumProcessor {
42 private final PathProcessor pathProcessor;
43
44 @Inject
45 public DefaultChecksumProcessor(PathProcessor pathProcessor) {
46 this.pathProcessor = requireNonNull(pathProcessor);
47 }
48
49 @Override
50 public String readChecksum(final Path checksumPath) throws IOException {
51
52 String checksum = "";
53 try (BufferedReader br = Files.newBufferedReader(checksumPath, StandardCharsets.UTF_8)) {
54 while (true) {
55 String line = br.readLine();
56 if (line == null) {
57 break;
58 }
59 line = line.trim();
60 if (!line.isEmpty()) {
61 checksum = line;
62 break;
63 }
64 }
65 }
66
67 if (checksum.matches(".+= [0-9A-Fa-f]+")) {
68 int lastSpacePos = checksum.lastIndexOf(' ');
69 checksum = checksum.substring(lastSpacePos + 1);
70 } else {
71 int spacePos = checksum.indexOf(' ');
72
73 if (spacePos != -1) {
74 checksum = checksum.substring(0, spacePos);
75 }
76 }
77
78 return checksum;
79 }
80
81 @Override
82 public void writeChecksum(Path target, String checksum) throws IOException {
83
84 pathProcessor.write(target, checksum);
85 }
86 }