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.spi.io;
020
021import java.io.*;
022import java.nio.ByteBuffer;
023import java.nio.file.Files;
024import java.nio.file.NoSuchFileException;
025import java.nio.file.Path;
026
027/**
028 * A utility component to perform file-based operations.
029 *
030 * @since 2.0.0
031 */
032public interface PathProcessor {
033    /**
034     * Returns last modified of path in milliseconds, if exists.
035     *
036     * @param path The path, may be {@code null}.
037     * @throws UncheckedIOException If an I/O error occurs.
038     */
039    default long lastModified(Path path, long defValue) {
040        try {
041            return Files.getLastModifiedTime(path).toMillis();
042        } catch (NoSuchFileException e) {
043            return defValue;
044        } catch (IOException e) {
045            throw new UncheckedIOException(e);
046        }
047    }
048
049    /**
050     * Returns size of file, if exists.
051     *
052     * @param path The path, may be {@code null}.
053     * @throws UncheckedIOException If an I/O error occurs.
054     */
055    default long size(Path path, long defValue) {
056        try {
057            return Files.size(path);
058        } catch (NoSuchFileException e) {
059            return defValue;
060        } catch (IOException e) {
061            throw new UncheckedIOException(e);
062        }
063    }
064
065    /**
066     * Writes the given data to a file. UTF-8 is assumed as encoding for the data. Creates the necessary directories for
067     * the target file. In case of an error, the created directories will be left on the file system.
068     *
069     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
070     * @param data The data to write, may be {@code null}.
071     * @throws IOException If an I/O error occurs.
072     */
073    void write(Path target, String data) throws IOException;
074
075    /**
076     * Writes the given stream to a file. Creates the necessary directories for the target file. In case of an error,
077     * the created directories will be left on the file system.
078     *
079     * @param target The file to write to, must not be {@code null}. This file will be overwritten.
080     * @param source The stream to write to the file, must not be {@code null}.
081     * @throws IOException If an I/O error occurs.
082     */
083    void write(Path target, InputStream source) throws IOException;
084
085    /**
086     * Moves the specified source file to the given target file. If the target file already exists, it is overwritten.
087     * Creates the necessary directories for the target file. In case of an error, the created directories will be left
088     * on the file system.
089     *
090     * @param source The file to move from, must not be {@code null}.
091     * @param target The file to move to, must not be {@code null}.
092     * @throws IOException If an I/O error occurs.
093     */
094    void move(Path source, Path target) throws IOException;
095
096    /**
097     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
098     * In case of an error, the created directories will be left on the file system.
099     *
100     * @param source The file to copy from, must not be {@code null}.
101     * @param target The file to copy to, must not be {@code null}.
102     * @throws IOException If an I/O error occurs.
103     */
104    void copy(Path source, Path target) throws IOException;
105
106    /**
107     * Copies the specified source file to the given target file. Creates the necessary directories for the target file.
108     * In case of an error, the created directories will be left on the file system.
109     *
110     * @param source The file to copy from, must not be {@code null}.
111     * @param target The file to copy to, must not be {@code null}.
112     * @param listener The listener to notify about the copy progress, may be {@code null}.
113     * @return The number of copied bytes.
114     * @throws IOException If an I/O error occurs.
115     */
116    long copy(Path source, Path target, ProgressListener listener) throws IOException;
117
118    /**
119     * A listener object that is notified for every progress made while copying files.
120     *
121     * @see PathProcessor#copy(Path, Path, ProgressListener)
122     */
123    interface ProgressListener {
124
125        void progressed(ByteBuffer buffer) throws IOException;
126    }
127}