001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.io.BufferedReader;
026import java.io.IOException;
027import java.nio.charset.StandardCharsets;
028import java.nio.file.Files;
029import java.nio.file.Path;
030
031import org.eclipse.aether.spi.io.ChecksumProcessor;
032import org.eclipse.aether.spi.io.PathProcessor;
033
034import static java.util.Objects.requireNonNull;
035
036/**
037 * A utility class helping with file-based operations.
038 */
039@Singleton
040@Named
041public class DefaultChecksumProcessor implements ChecksumProcessor {
042    private final PathProcessor pathProcessor;
043
044    @Inject
045    public DefaultChecksumProcessor(PathProcessor pathProcessor) {
046        this.pathProcessor = requireNonNull(pathProcessor);
047    }
048
049    @Override
050    public String readChecksum(final Path checksumPath) throws IOException {
051        // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
052        String checksum = "";
053        try (BufferedReader br = Files.newBufferedReader(checksumPath, StandardCharsets.UTF_8)) {
054            while (true) {
055                String line = br.readLine();
056                if (line == null) {
057                    break;
058                }
059                line = line.trim();
060                if (!line.isEmpty()) {
061                    checksum = line;
062                    break;
063                }
064            }
065        }
066
067        if (checksum.matches(".+= [0-9A-Fa-f]+")) {
068            int lastSpacePos = checksum.lastIndexOf(' ');
069            checksum = checksum.substring(lastSpacePos + 1);
070        } else {
071            int spacePos = checksum.indexOf(' ');
072
073            if (spacePos != -1) {
074                checksum = checksum.substring(0, spacePos);
075            }
076        }
077
078        return checksum;
079    }
080
081    @Override
082    public void writeChecksum(Path target, String checksum) throws IOException {
083        // for now do exactly same as happened before, but FileProcessor is a component and can be replaced
084        pathProcessor.write(target, checksum);
085    }
086}