View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A utility class helping with file-based operations.
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          // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
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          // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
84          pathProcessor.write(target, checksum);
85      }
86  }